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

namespace CloudBuilder.Core.Policy
{
    public class SecurityMasterPolicy : IPolicy
    {
        public string Namespace { get; set; } = typeof(SecurityMasterPolicy).Namespace;

        public string Name { get; set; } = typeof(SecurityMasterPolicy).Name;

        private readonly IContext context;
        private readonly ISecurityMasterManager securityMasterManager;
        public SecurityMasterPolicy(IContext context, ISecurityMasterManager securityMasterManager)
        {
            this.context = context;
            this.securityMasterManager = securityMasterManager;
        }

        public dynamic GetSecurityMaster(string codeItem)
        {
            return securityMasterManager.GetSecurityMaster(Namespace, Name, codeItem);
        }

        public string GetValue(string codeItem)
        {
            return securityMasterManager.GetValue(Namespace, Name, codeItem);
        }

        public dynamic GetDefaultValue<T>(string codeItem, T t)
        {
            string value = securityMasterManager.GetValue(Namespace, Name, codeItem);
            if (value == null)
            {
                securityMasterManager.SetSecurityMaster(Namespace, Name, codeItem, t.ToString());
                securityMasterManager.InitSecurityMaster();
                return t;
            }

            if (t is string) return value;

            else if (t is int) return Convert.ToInt32(value);

            else if (t is decimal) return Convert.ToDecimal(value);

            else if (t is DateTime) return Convert.ToDateTime(value);

            return value;
        }
    }
}
