using CloudBuilder.Core.DependencyInjection.Policy;
using CloudBuilder.Core.DependencyInjection.Request;
using CloudBuilder.Core.Policy;
using CloudBuilder.Core.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;

namespace System
{
    public class AbstractPolicy : IPolicy
    {
        public ResourceManager rm = null;

        public AbstractPolicy(IContext context)
        {
            this.context = context;

            string[] res = Assembly.GetAssembly(this.GetType()).GetManifestResourceNames();

            if (res != null && res.Length > 0)
            {
                string baseName = this.GetType().Name + "_" + context.GetCulture();
                if (res.Where(x => x.Contains(baseName)).FirstOrDefault() != null)
                    rm = new ResourceManager(Path.GetFileNameWithoutExtension(res.Where(x => x.Contains(baseName)).FirstOrDefault()), Assembly.GetAssembly(this.GetType()));
                else
                    rm = new ResourceManager(Path.GetFileNameWithoutExtension(res.Where(x => x.Contains(this.GetType().Name)).FirstOrDefault()), Assembly.GetAssembly(this.GetType()));
            }
        }
        protected string[] values = null;
        private readonly IContext context;

        public string[] GetValues() { return this.values; }

        public string[] GetMessages()
        {
            if (values == null || values.Length == 0) return null;

            List<string> messages = new List<string>();
            foreach (string v in values)
            {
                messages.Add(ComposeMessage(v));
            }

            return messages.ToArray();
        }

        public BindOption[] GetBindOptions()
        {
            if (values == null || values.Length == 0) return null;

            List<BindOption> messages = new List<BindOption>();
            foreach (string v in values)
            {
                messages.Add(new BindOption() { BindValue = v, DisplayName = ComposeMessage(v) });
            }

            return messages.ToArray();
        }

        public SelectOption[] GetSelectOptions()
        {
            if (values == null || values.Length == 0) return null;

            List<SelectOption> messages = new List<SelectOption>();
            foreach (string v in values)
            {
                messages.Add(new SelectOption() { BindValue = v, DisplayName = ComposeMessage(v) });
            }

            return messages.ToArray();
        }

        public string ComposeMessage(string msgId, params object[] p)
        {
            return ComposeMessage(msgId, context.GetCulture(), p);
        }

        public string ComposeMessage(string msgId)
        {
            return ComposeMessage(msgId, context.GetCulture());
        }

        public string ComposeMessage(string msgId, string locale, params object[] p)
        {
            if (string.IsNullOrEmpty(msgId)) return null;
            if (rm == null) return null;

            string[] res = Assembly.GetAssembly(this.GetType()).GetManifestResourceNames();
            string baseName = this.GetType().Name + "_" + locale;
            if (res.Where(x => x.Contains(baseName)).FirstOrDefault() != null)
                rm = new ResourceManager(Path.GetFileNameWithoutExtension(res.Where(x => x.Contains(baseName)).FirstOrDefault()), Assembly.GetAssembly(this.GetType()));

            return string.Format((string)rm.GetString(msgId), p);
        }

        public string ComposeMessage(string msgId, string locale)
        {
            if (string.IsNullOrEmpty(msgId)) return null;
            if (rm == null) return null;

            string[] res = Assembly.GetAssembly(this.GetType()).GetManifestResourceNames();
            string baseName = this.GetType().Name + "_" + locale;
            if (res.Where(x => x.Contains(baseName)).FirstOrDefault() != null)
                rm = new ResourceManager(Path.GetFileNameWithoutExtension(res.Where(x => x.Contains(baseName)).FirstOrDefault()), Assembly.GetAssembly(this.GetType()));

            if (rm == null) return null;

            return string.Format((string)rm.GetString(msgId));
        }

        public TableName<T>[] GetTableName<T>(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(v.ToString(), context.GetCulture());
                tb.Value = v;
                list.Add(tb);
            }

            return list.ToArray();
        }

        public Dictionary<string, object> GetDictionary()
        {
            Dictionary<string, object> keyValues= new Dictionary<string, object>();
            foreach (string v in values)
            {
                keyValues.Add(v, ComposeMessage(v, context.GetCulture()));
            }

            return keyValues;
        }

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

            return list.ToArray();
        }

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

            return list.ToArray();
        }

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

            return list.ToArray();
        }

    }
}
