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

namespace CloudBuilder.Gui.DependencyInjection
{
    public class LocaleMessageManager : ILocaleMessageManager
    {
        public LocaleEntity[] LocaleEntities { get; set; } = null!;

        public static string SYSTEM = "system";

        public static string GUI_OK = "ok";
        public static string GUI_CANCEL = "cancel";
        public static string GUI_INFORMATION = "gui_information";
        public static string GUI_ERROR = "gui_error";
        private readonly IServiceProvider serviceProvider;
        private readonly IContext context;

        public LocaleMessageManager(IServiceProvider serviceProvider, IContext context)
        {
            this.serviceProvider = serviceProvider;
            this.context = context;
        }

        public void InitLocale()
        {
            LocaleRequest localeRequest = serviceProvider.GetRequiredKeyedService<LocaleRequest>(typeof(LocaleRequest).Name);

            LocaleEntities = localeRequest.FindAll().OrderBy(x => x.OrderBy).ToArray();
        }

        public string ComposeMessage(string groupName, string name)
        {
            if (LocaleEntities == null || LocaleEntities.Length == 0)
            {
                InitLocale();
            }

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

            return LocaleEntities.Where(x => x.GroupName == groupName && x.Name == name && x.Lang == context.GetCulture()).Select(x => x.Value).FirstOrDefault()!;
        }
        public TableName<string>[] GetDataSource(string groupName)
        {
            if (LocaleEntities == null || LocaleEntities.Length == 0)
            {
                InitLocale();
            }

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

            string[] values = LocaleEntities.Where(x => x.GroupName == groupName && x.Lang == context.GetCulture()).OrderBy(x => x.OrderBy).Select(x => x.Name).ToArray();

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

            return GetComposeMessages<string>(groupName, values);
        }

        public TableName<T>[] GetComposeMessages<T>(string groupName, T[] values)
        {
            if (values == null || values.Length == 0) return null;
            TableName<T> tb = null;
            List<TableName<T>> list = new List<TableName<T>>();
            foreach (T v in values)
            {
                tb = new TableName<T>();
                tb.DisplayName = ComposeMessage(groupName, v.ToString());
                tb.Value = v;
                list.Add(tb);
            }

            return list.ToArray();
        }
    }
}

/*
INSERT INTO [dbo].[locale]([created_datetime],[created_by],[updated_datetime],[updated_by],[group_name],[lang],[name],[value],[order])     VALUES
(GETDATE(),'patch',GETDATE(),'patch','system','zh-cn','gui_error',N'出错信息',0),
(GETDATE(),'patch',GETDATE(),'patch','system','en','gui_error',N'Error Message',0),
(GETDATE(),'patch',GETDATE(),'patch','system','zh-cn','gui_information',N'验证信息',0),
(GETDATE(),'patch',GETDATE(),'patch','system','en','gui_information',N'Verification Message',0)

="(GETDATE(),'patch',GETDATE(),'patch','"&$A$1&"','zh-cn','"&A2&"',N'"&B2&"',0),"
="(GETDATE(),'patch',GETDATE(),'patch','"&$A$1&"','en','"&A2&"',N'"&B2&"',0),"
 */
