using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Core.DependencyInjection.Request;
using CloudBuilder.Core.Policy;
using CloudBuilder.Security.Entity;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudBuilder.Security.Service.DependencyInjection
{
    public class SecurityMasterManager : ISecurityMasterManager
    {
        private readonly IApplicationService service;
        private readonly IContext context;

        public SecurityMasterManager(IApplicationService service, IContext context)
        {
          
            this.service = service;
            this.context = context;
        }


        public void InitSecurityMaster()
        {
            //暂时不处理缓存
            throw new NotImplementedException();
        }

        public string GetValue(string category, string subcategory, string codeItem)
        {
            SecurityMasterEntity securityMaster = GetSecurityMaster(category, subcategory, codeItem);
            if (securityMaster == null) return null;
            return securityMaster.Value;
        }

        public dynamic GetSecurityMaster(string category, string subcategory, string codeItem)
        {
            SecurityMasterEntityKey key = new SecurityMasterEntityKey()
            {
                MasterSubcategory = subcategory,
                MasterCategory = category,
                MasterCodeItem = codeItem
            };
            ISecurityMasterService securityMasterService = service.ServiceProvider.GetService<ISecurityMasterService>()!;
            return securityMasterService.FindByKey(key);

        }

        public void SetSecurityMaster(string category, string subcategory, string codeItem, string value)
        {
            SecurityMasterEntity securityMaster = new SecurityMasterEntity();
            securityMaster.MasterSubcategory = subcategory;
            securityMaster.MasterCodeItem = codeItem;
            securityMaster.MasterCategory = category;
            securityMaster.Value = value;
            securityMaster.ValidIndc = YesNoPolicy.YES;

            ISecurityMasterService securityMasterService = service.ServiceProvider.GetService<ISecurityMasterService>()!;
            securityMasterService.Insert(securityMaster);
        }
    }
}
