using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;

using C4IT.FASD.Base;
using C4IT.FASD.Cockpit.Communication;
using C4IT.Logging;

using static C4IT.Logging.cLogManager;
using C4IT.Matrix42.WebClient;
using System.Collections;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace C4IT.FASD.Cockpit.Communication
{
    internal class cF4sdCockpitCommunicationM42Web : cF4sdCockpitCommunicationM42Base
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static string getServer()
        {
            return cCockpitConfiguration.Instance?.m42ServerConfiguration?.Server;
        }

        public override async Task<cF4sdCockpitM42BearerTokenInfo> ValidateLogonPassthrough()
        {
            var CM = MethodBase.GetCurrentMethod();
            LogMethodBegin(CM);
            try
            {
                var _m42Server = getServer();
                if (string.IsNullOrEmpty(_m42Server))
                    return null;

                var _userInfo = await cM42WebClient.LogonWin(_m42Server);
                if (_userInfo != null && _userInfo.Reason == cM42WebClient.eReason.ok)
                {
                    return new cF4sdCockpitM42BearerTokenInfo()
                    {
                        Token = _userInfo.BearerToken,
                        ValidUntil = _userInfo.BearerValidUntil
                    };
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                LogMethodEnd(CM);
            }

            return null;
        }

        public override async Task<cF4sdCockpitM42BearerTokenInfo> ValidateLogonBasic(string User, string Password)
        {
            var CM = MethodBase.GetCurrentMethod();
            LogMethodBegin(CM);
            try
            {
                var _m42Server = getServer();
                if (string.IsNullOrEmpty(_m42Server))
                    return null;

                NetworkCredential networkCredential = new NetworkCredential(User, Password);
                var _userInfo = await cM42WebClient.LogonWin(_m42Server, Credentials: networkCredential);
                if (_userInfo != null && _userInfo.Reason == cM42WebClient.eReason.ok)
                {
                    return new cF4sdCockpitM42BearerTokenInfo()
                    {
                        Token = _userInfo.BearerToken,
                        ValidUntil = _userInfo.BearerValidUntil
                    };
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                LogMethodEnd(CM);
            }

            return null;
        }

        public override async Task<cF4sdCockpitM42BearerTokenInfo> ValidateLogonToken(string Token)
        {
            var CM = MethodBase.GetCurrentMethod();
            LogMethodBegin(CM);
            try
            {
                var _m42Server = getServer();
                if (string.IsNullOrEmpty(_m42Server))
                    return null;

                var _userInfo = await cM42WebClient.LogonToken(_m42Server, Token);
                if (_userInfo != null && _userInfo.Reason == cM42WebClient.eReason.ok)
                {
                    return new cF4sdCockpitM42BearerTokenInfo()
                    {
                        Token = _userInfo.BearerToken,
                        ValidUntil = _userInfo.BearerValidUntil
                    };
                }

            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                LogMethodEnd(CM);
            }

            return null;
        }

    }


}
