using Back.Common;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using Shared.DTOs;
using TaxCollectData.Library.Business;
using TaxCollectData.Library.Dto.Config;
using TaxCollectData.Library.Dto.Content;
using TaxCollectData.Library.Dto.Properties;
using TaxCollectData.Library.Dto.Transfer;
using TaxCollectData.Library.Enums;

namespace Back.Services
{
    public class ActionTaxPayer
    {
        private string _UniqueMemory;
        private string _PrivateKey;
        private readonly servCompany _servCompany;
        public ActionTaxPayer(servCompany servCompany)
        {
            _servCompany = servCompany;
        }
        public string GenerateTaxid(string FactorNo, string InvoiceDate)
        {
            return "testTaxid";
            return TaxApiService.Instance.TaxIdGenerator.GenerateTaxId(_UniqueMemory,
                   Convert.ToInt64(FactorNo), InvoiceDate.ToMiladi());
        }
        public async Task<InquiryResultModel> GetResultByUid(int CompanyID, string uid)
        {
            //return new InquiryResultModel("18084a18-1eb4-41cd-8bd3-2cad73c45398", "0a4a4ab2-8047-4c31-b765-456ddf0e9c53", "SUCCESS",
            //   new DataInSendTaxDto()
            //   {
            //       error=new List<MessageInSendTaxDto>()
            //       {
            //           new MessageInSendTaxDto()
            //           {
            //               code="21001",message="یک خطای تست"
            //           }
            //       }
            //   }
            //    , "receive_invoice_confirm", "A2FFKZ");
            if (!await login(CompanyID))
                return null;
            var uidAndFiscalId = new UidAndFiscalId(uid, _UniqueMemory);
            var inquiryResultModels = TaxApiService.Instance.TaxApis.InquiryByUidAndFiscalId(new() { uidAndFiscalId });
            if (inquiryResultModels.Count > 0)
                return inquiryResultModels[0];
            return null;
        }
        public async Task<TaxCollectData.Library.Dto.HttpResponse<AsyncResponseModel>> SendInvoice(int CompanyID,InvoiceHeaderDto header, List<InvoiceBodyDto> InvoiceBody, List<PaymentDto> payments)
        {
            //return new TaxCollectData.Library.Dto.HttpResponse<AsyncResponseModel>
            //    ( body:  new AsyncResponseModel(
            //        1702299112
            //        ,new HashSet<PacketResponse>(new List<PacketResponse> { new PacketResponse("5d0c7198-e2fd-4cc1-8802-fe498d6ccf73", "a70444a4-1810-4cea-8bcc-7acb4bc75645", null,null)})
            //        ,new List<ErrorModel>()), 200);
         
            if (!await login(CompanyID))
                return null;
            return await TaxApiService.Instance.TaxApis.SendInvoicesAsync(new List<InvoiceDto>()
          {
              new()
              {
                  Header =header,Body =InvoiceBody,Payments  = payments
              }
          }
            , null);
        }
        public async Task<EconomicCodeModel?> GetEconomicCodeInformation(string Item)
        {
            return await TaxApiService.Instance.TaxApis.GetEconomicCodeInformationAsync(Item);
        }
        //-------------------internal
        public async Task<bool> login(int CompanyID)
        {
            return true;
            try
            {
                #region TokenTax
                var resquth = await _servCompany.GetTaxAuth(CompanyID);
                if (string.IsNullOrEmpty(resquth.UniqueMemory) || string.IsNullOrEmpty(resquth.PrivateKey))
                    return false;

                if (!string.IsNullOrEmpty(resquth.UniqueMemory) && !string.IsNullOrEmpty(resquth.PrivateKey))
                {
                    _UniqueMemory = resquth.UniqueMemory;
                    _PrivateKey = resquth.PrivateKey;
                    TaxApiService.Instance.Init(_UniqueMemory,
                                     new SignatoryConfig(_PrivateKey, null),
                                     new NormalProperties(ClientType.SELF_TSP), "https://tp.tax.gov.ir/req/api/");
                    await TaxApiService.Instance.TaxApis.GetServerInformationAsync();
                }
                #endregion

                if (TaxApiService.Instance.TaxApis.GetToken() is null)
                {
                    if (await TaxApiService.Instance.TaxApis.RequestTokenAsync() == null)
                        return false;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

       
    }
}
