using CloudBuilder.Core.Policy;
using CloudBuilder.Request.Security;
using CloudBuilder.Security.Entity;
using DocumentFormat.OpenXml.InkML;
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudBuilder.Gui.DependencyInjection
{
    public class SecurityMasterManager : ISecurityMasterManager
    {
        public SecurityMasterEntity[]? SecurityMasterEntities { get; set; }
        private readonly IServiceProvider serviceProvider;

        public SecurityMasterManager(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
        }

        public dynamic GetSecurityMaster(string category, string subcategory, string codeItem)
        {
            if (SecurityMasterEntities == null || SecurityMasterEntities.Length == 0)
            {
                InitSecurityMaster();
            }

            if (SecurityMasterEntities == null || SecurityMasterEntities.Length == 0) return null!;

            return SecurityMasterEntities.Where(x => x.MasterCategory == category && x.MasterSubcategory == subcategory && x.MasterCodeItem == codeItem).FirstOrDefault()!;
        }

        public string GetValue(string category, string subcategory, string codeItem)
        {
            if (SecurityMasterEntities == null || SecurityMasterEntities.Length == 0)
            {
                InitSecurityMaster();
            }

            if (SecurityMasterEntities == null || SecurityMasterEntities.Length == 0) return null!;

            return SecurityMasterEntities.Where(x => x.MasterCategory == category && x.MasterSubcategory == subcategory && x.MasterCodeItem == codeItem).Select(x => x.Value).FirstOrDefault()!;
        }

        public void InitSecurityMaster()
        {
            SecurityMasterRequest securityMasterRequest = serviceProvider.GetRequiredKeyedService<SecurityMasterRequest>(typeof(SecurityMasterRequest).Name);

            SecurityMasterEntities = securityMasterRequest.FindAll().ToArray();
        }

        public void SetSecurityMaster(string category, string subcategory, string codeItem, string value)
        {
            SecurityMasterRequest securityMasterRequest = serviceProvider.GetRequiredKeyedService<SecurityMasterRequest>(typeof(SecurityMasterRequest).Name);
            SecurityMasterEntity securityMasterEntity = new SecurityMasterEntity()
            {
                CreatedBy = string.Empty,
                UpdatedBy = string.Empty,
                CreatedDatetime = DateTime.Now,
                UpdatedDatetime = DateTime.Now
            };

            securityMasterEntity.MasterCategory = category;
            securityMasterEntity.MasterSubcategory = subcategory;
            securityMasterEntity.MasterCodeItem = codeItem;
            securityMasterEntity.Value = value;
            securityMasterEntity.ValidIndc = YesNoPolicy.YES;

            securityMasterRequest.Insert(securityMasterEntity);
        }
    }
}
