using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

using C4IT.FASD.Base;
using FasdCockpitBase.Models;

namespace C4IT.FASD.Cockpit.Communication
{
    public abstract class cFasdCockpitCommunicationBase
    {
        public static cFasdCockpitCommunicationBase Instance { get; set; }

        public static cF4sdUserInfo CockpitUserInfo = null;
        public static object CockpitUserInfoLock = new object();

        public Dictionary<Guid, cF4SDHealthCardRawData> CachedHealthCardRawData { get; set; } = new Dictionary<Guid, cF4SDHealthCardRawData>();

        public delegate Task CheckConnectionStatusDelegate();

        public CheckConnectionStatusDelegate CheckConnectionStatus;

        public cF4sdCockpitCommunicationM42Base M42 = new cF4sdCockpitCommunicationM42Base();

        public abstract bool IsDemo();

        public abstract bool CheckConnectionInfo();

        public abstract Task<cCheckConnectionResult> CheckConnection(Version minServerVersion);

        public abstract Task<Guid> GetUserIdByAccount(string userName, string userDomain);

        public abstract Task<cF4sdUserInfo> WinLogon();

        public abstract Task<cF4SdUserInfoChange> RegisterExternalTokenAsync(cF4SDTokenRegistration Token);

        public abstract Task<cF4SDAdditionalUserInfo> GetAdditionalUserInfo(enumAdditionalAuthentication Type);

        public abstract Task<bool> IsAnalyticsModuleActive();

        public abstract Task<bool> CreateUserSession(cF4SDUserSessionParameters sessionParameter);
        public abstract Task CloseUserSession(Guid sessionId);
        public abstract Task<bool> StartCase(cF4SDCaseParameters caseParameters);
        public abstract Task<bool> UpdateCase(cF4SDCaseStatusParameters caseStatusParameters, List<cF4SDCaseTime> caseTimes);
        public abstract Task<bool> KeepAliveCase(Guid caseId);
        public abstract Task<bool> KeepAliveSession(Guid sessionId);

        public abstract Task<bool> ReportQuickAction(cF4SDQuickActionParameters quickActionParameters);
        public abstract Task<cCollectorStatusInfo> CheckCollectorStatusAsync();
        public static bool Debug_apiValues = false;
        public static bool Debug_apiTiming = false;

        public abstract Task<cFasdBaseConfig> GetConfiguration(enumFasdConfigurationType configType);

        public abstract Task<Guid?> GetSearchResultsStart(string searchValue, CancellationToken cancellationToken);
        public abstract Task<cFasdApiSearchResultCollection> GetSearchResultsResult(Guid taskID, CancellationToken cancellationToken);
        public abstract Task GetSearchResultsStop(Guid taskID, CancellationToken cancellationToken);

        public abstract Task<cFasdApiSearchResultCollection> GetPhoneSearchResults(cPhoneSearchParameters searchInfo);
        public abstract Task<cFasdApiSearchResultCollection> GetComputerSearchResults(string Name, string Domain);
        public abstract Task<cFasdApiSearchResultCollection> GetUserSearchResults(string Name, List<string> SIDs);

        public abstract Task<cF4sdStagedSearchResultRelationTaskId> StartGatheringRelations(IEnumerable<cFasdApiSearchResultEntry> relatedTo, CancellationToken token);
        public abstract Task<cF4sdStagedSearchResultRelations> GetStagedRelations(Guid id, CancellationToken token);

        public abstract Task<List<cF4sdApiSearchResultRelation>> GetSearchResultRelations(enumF4sdSearchResultClass resultType, List<cFasdApiSearchResultEntry> searchResults);

        #region Ticketübersicht

        public abstract Task<List<cF4sdApiSearchResultRelation>> GetTicketOverviewRelations(string key, bool useRoleScope, int count);
        public abstract Task<Dictionary<string, int>> GetTicketOverviewCounts(IEnumerable<string> keys, bool useRoleScope);

        #endregion

        public abstract Task<cF4SDHealthCardRawData> GetHealthCardData(cF4sdHealthCardRawDataRequest requestData);

        public abstract Task<cF4SDHealthCardRawData> GetHealthCardData(Guid healthCardId);

        public abstract Task<List<cF4SDHealthCardRawData.cHealthCardDetailsTable>> GetDetailsData(cF4sdHealthCardRawDataRequest requestData);

        public abstract Task<int> GetPagedDataCount(cF4sdHealthSelectionDataRequest requestData);

        public abstract Task<cF4SDHealthCardRawData.cHealthCardTable> GetPagedData(cF4sdHealthSelectionDataRequest requestData);

        public abstract Task<bool> UpdateHealthcardTableData(cF4SDWriteParameters dataParameter);
        public abstract Task<bool> Matrix42TicketFinalization(cApiM42Ticket ticketData);

        public abstract Task<List<List<object>>> GetQuickActionHistory(string QuickActionName, int OrgId, int DeviceId, int? UserId);

        public abstract Task<bool> GetAgentApiAccessInfo();

        public abstract Task<bool> GetCockpitConfiguration();

        public abstract Task<bool> GetAgentOnlineStatus(int AgentDeviceId, int? AgentUserId = null);

        public abstract Task<cF4sdAgentScript> GetQuickActionOfAgent(int ScriptId);

        public abstract Task<List<cF4sdAgentScript>> GetQuickActionsOfAgent();
        public abstract Task<List<string>> GetQuickActionsOfServer();

        public abstract Task<Guid> ActualizeAgentData(int AgentDeviceId, int? AgentUserId = null);

        public abstract Task<enumActualizeStatus> GetActualizeAgentDataStatus(Guid actualizeId, enumFasdInformationClass informationClass);

        public abstract Task<Guid?> TryActivateDirectConnection(int DeviceId);

        public abstract Task StopDirectConnection(Guid connectionId);

        public abstract Task<bool> IsDirectConnectionUp(Guid connectionId);

        public abstract Task<bool> DirectConnectionExtendDuration(Guid connectonId, int timeOutInSeconds);

        public abstract Task<cF4sdAgentScriptInformation> RunAgentScript(cF4sdQuickActionRemote Script, int OrgCode, int DeviceId, int? DeviceAccountId, string Parameters);
        public abstract Task<enumQuickActionStatus> GetAgentQuickActionStatus(int TaskId);
        public abstract Task<cF4sdQuickActionRevision> GetAgentRevision(int RevisionId, int OrgId);

        public abstract Task CancelAgentTask(int TaskId);

        public abstract Task InitializeAfterOnlineAsync();

        public abstract Task TerminateAsync();

        public abstract Uri GetMediaContentUri(string ContentRelPath);
    }


    public enum enumConnectionStatus
    {
        unknown = 0,
        serverNotFound,
        serverResponseError,
        incompatibleServerVersion,
        serverNotConfigured,
        serverStarting,
        connected,
    }

    public enum enumQuickActionStatus
    {
        Unknown = -1,
        ConnectingToClient = 1,
        WaitingForUserAcceptance = 2,
        Running = 3,
        Finished = 4,
        Cancelled = 5
    }

    public class cCheckConnectionResult
    {
        public enumConnectionStatus ConnectionStatus { get; set; }
        public cFasdApiConnectionInfo ApiConnectionInfo { get; set; }
    }

    public class cPhoneSearchParameters
    {
        public string phone { get; set; }
        public string name { get; set; } = null;
    }

    public class cComputerDomainSearchParameters
    {
        public string name { get; set; }
        public string domain { get; set; }
    }

    public class cUserSidSearchParameters
    {
        public string name { get; set; }
        public string sids { get; set; }
    }

    public class cTicketSearchParameters
    {
        public string ticketName { get; set; }
        public string ticketId { get; set; }
        public string userName { get; set; }
        public string sids { get; set; }

    }
}
