using BlazorBootstrap;
using Front.Services;
using Shared.DTOs;
using System.Net.Http.Json;
using System.Security.Cryptography;

namespace Front
{
    public class Fixedvalues
    {
        public string Domin { get; set; } = "http://195.88.208.142";
        public readonly HttpClientController _hc;
        private List<ForCustomerSearch>? Cus = null;
        private List<IdName<int>>? Patterns = null;
        private List<IdName<string>>? PaymentMethods = null;
        private List<IdName<int>>? Patternsbyinp = null;
        private List<IdName<int>>? BillTypes = null;
        private List<IdName<int>>? UnitsbyUnitTaxID = null;
        private List<IdName<int>>? Units = null;
        private List<CODIdName<int>>? Cods = null;
        private CompanyDTO? InfoCompany = null;
        public InvoiceDTO? invoice { get; set; } = null;
        public DashBoardDTO? dashBoard { get; set; } = null;

        private List<PromotionDto> Promotions { get; set; } = null;

        public Fixedvalues(HttpClientController hc)
        {
            _hc = hc;
        }
        public async Task<List<ForCustomerSearch>?> GetCustomers()
        {
            if (Cus == null)
            {
                var request = await _hc.Get($"Customer/GetAllForidName");
                if (request.IsSuccessStatusCode)
                {
                    Cus = await request.Content.ReadFromJsonAsync<List<ForCustomerSearch>>();
                }
            }

            return Cus;


        }
        public async Task<List<CODIdName<int>>?> GetCODs()
        {
            if (Cods == null)
            {
                var request = await _hc.Get($"COD/GetAllForidName");
                if (request.IsSuccessStatusCode)
                {
                    Cods = await request.Content.ReadFromJsonAsync<List<CODIdName<int>>>();
                }
            }

            return Cods;


        }
        public async Task<List<IdName<int>>?> GetPatterns()
        {
            if (Patterns == null)
            {
                var request = await _hc.Get($"Invoice/GetPatterns");
                if (request.IsSuccessStatusCode)
                {
                    Patterns = await request.Content.ReadFromJsonAsync<List<IdName<int>>>();
                }
            }


            return Patterns;
        }
        public async Task<List<IdName<string>>?> GetPaymentMethods()
        {
            if (PaymentMethods == null)
            {
                var request = await _hc.Get($"InvoicePayment/PaymentMethods");
                if (request.IsSuccessStatusCode)
                {
                    PaymentMethods = await request.Content.ReadFromJsonAsync<List<IdName<string>>>();
                }
            }


            return PaymentMethods;
        }
        public async Task<List<IdName<int>>?> GetPatternsbyinp()
        {
            if (Patternsbyinp == null)
            {
                var request = await _hc.Get($"TaxPayer/GetPatterns");
                if (request.IsSuccessStatusCode)
                {
                    Patternsbyinp = await request.Content.ReadFromJsonAsync<List<IdName<int>>>();
                }
            }


            return Patternsbyinp;
        }
        public async Task<List<IdName<int>>?> GetUnitsbyUnitTaxID()
        {
            if (UnitsbyUnitTaxID == null)
            {
                var request = await _hc.Get($"COD/GetUnits?UnitTaxID=true");
                if (request.IsSuccessStatusCode)
                {
                    UnitsbyUnitTaxID = await request.Content.ReadFromJsonAsync<List<IdName<int>>>();
                }
            }


            return UnitsbyUnitTaxID;
        }
        public async Task<List<IdName<int>>?> GetBillTypes()
        {
            if (BillTypes == null)
            {
                var request = await _hc.Get($"TaxPayer/GetBillTypes");
                if (request.IsSuccessStatusCode)
                {
                    BillTypes = await request.Content.ReadFromJsonAsync<List<IdName<int>>>();
                }
            }


            return BillTypes;
        }
        public async Task<CompanyDTO?> GetInfoCompany()
        {

            if (InfoCompany == null)
            {
                var rspCompany = await _hc.Get($"Company/GetCompany");
                if (rspCompany.IsSuccessStatusCode)
                    InfoCompany = await rspCompany.Content.ReadFromJsonAsync<CompanyDTO>();
            }


            return InfoCompany;
        }
        public async Task LoadDashBoard(bool run = false)
        {

            if (dashBoard == null || run)
            {
                var rsp = await _hc.Get("User/GetDashBoard");
                if (rsp.IsSuccessStatusCode)
                {
                    dashBoard = await rsp.Content.ReadFromJsonAsync<DashBoardDTO>();
                }
            }


        }
        public async Task<List<IdName<int>>> GetUnits()
        {
            if (Units == null)
            {
                var rsp = await _hc.Get("COD/GetUnits");
                if (rsp.IsSuccessStatusCode)
                {
                    Units = await rsp.Content.ReadFromJsonAsync<List<IdName<int>>>();
                }
            }
            return Units;
        }
        public async Task SetUlr(string Type)
        {
            await _hc.Post<UlrDto>("Base/Ulr", new UlrDto { Type = Type });



        }
        public async Task<List<PromotionDto>> GetPromotion()
        {
            if (Promotions == null)
            {
                List<PricingDto> Pricing  = new List<PricingDto>();
                var rsp = await _hc.Get($"Orders/GetAllPromotion");
                if (rsp.IsSuccessStatusCode)
                {
                    Promotions = await rsp.Content.ReadFromJsonAsync<List<PromotionDto>>() ?? new List<PromotionDto>();

                    rsp = await _hc.Get($"Orders/GetAllPricing");
                    if (rsp.IsSuccessStatusCode)
                        Pricing = await rsp.Content.ReadFromJsonAsync<List<PricingDto>>() ?? new List<PricingDto>();

                    if (Pricing.Count>0)
                    {
                        Promotions.Add(new PromotionDto()
                        {
                            ID = -1,
                            Name = "شخصی سازی",
                            promotionDetails = Pricing.Select(s => new PromotionDetailDto
                            {
                                ID = 0,
                                APrice = s.Price,
                                CreditAmount = 0,
                                PermissionID = s.PermissionID,
                                PermissionTitle = s.PermissionTitle
                            }).ToList()
                        });
                    }
                    
                }
            }
            return Promotions ?? new List<PromotionDto>();


        }
    }


}