using CloudBuilder;
using CloudBuilder.Gui;
using CloudBuilder.Gui.DependencyInjection;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Krypton.Toolkit
{
    public static class KryptonTextBoxExtensions
    {
        public const string DATA_FROMAT = "yyyy-MM-dd";

        public static ExFormSnapshotEntry GetExFormSnapshotEntry(this KryptonTextBox kryptonTextBox)
        {
            string bindDataName = null;

            if (GetProvider().GetCustomAttribute(kryptonTextBox) != null)
                bindDataName = GetProvider().GetCustomAttribute(kryptonTextBox).BindDataName;

            return new ExFormSnapshotEntry(kryptonTextBox.Name, bindDataName, kryptonTextBox.Text, kryptonTextBox.Text);
        }

        public static void Set(this KryptonTextBox kryptonTextBox,
            BindingContextCustomAttribute bindingContext)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, bindingContext);
        }

        public static void Set(this KryptonTextBox kryptonTextBox,
            EnumEditModeColor editModeColor)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, editModeColor);
        }

        public static void Set(this KryptonTextBox kryptonTextBox, EnumEditModeColor editModeColor, string bindDisplayName)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, editModeColor, bindDisplayName);
        }

        public static void Set(this KryptonTextBox kryptonTextBox,
           string bindDataName,
           EnumEditModeColor editModeColor)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, bindDataName, editModeColor);
        }

        public static void Set(this KryptonTextBox kryptonTextBox,
            string bindDataName, string bindDisplayName, EnumEditModeColor editModeColor)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, bindDataName, bindDisplayName, editModeColor);
        }

        public static void Set(this KryptonTextBox kryptonTextBox,
           string bindDataName, string bindFormat, string bindDisplayName, EnumEditModeColor editModeColor)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, bindDataName, bindFormat, bindDisplayName, editModeColor);
        }

        public static void Set(this KryptonTextBox kryptonTextBox, string bindDataName)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, bindDataName, EnumBindDirection.Both);
        }

        public static void Set(this KryptonTextBox kryptonTextBox,
            string bindDataName, EnumBindDirection bindDirection)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, bindDataName, bindDirection);
        }


        public static void Set(this KryptonTextBox kryptonTextBox,
            string bindDataName,
            EnumBindDirection bindDirection,
            EnumEditModeColor editModeColor)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, bindDataName, bindDirection, editModeColor);
        }

        public static void Set(this KryptonTextBox kryptonTextBox, string bindDataName, string bindFormat, string bindDisplayName, EnumBindDirection bindDirection)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, bindDataName, bindFormat, bindDisplayName, bindDirection);
        }

        public static void Set(this KryptonTextBox kryptonTextBox,
           string permissionName,
           Operate permissionMode)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, permissionName, permissionMode);
        }

        public static void Set(this KryptonTextBox kryptonTextBox, string permissionName, string dependentDetailName, Operate permissionMode)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, permissionName, dependentDetailName, permissionMode);
        }

        public static void Set(this KryptonTextBox kryptonTextBox, string permissionName, string dependentDetailName)
        {
            GetProvider().SetCustomAttribute(kryptonTextBox, permissionName, dependentDetailName);
        }

        public static string GetValue(this KryptonTextBox kryptonTextBox)
        {
            if (string.IsNullOrEmpty(kryptonTextBox.Text)) return string.Empty;
            return kryptonTextBox.Text.Trim();
        }

        public static void SetValue(this KryptonTextBox kryptonTextBox, string value)
        {
            kryptonTextBox.Text = value;
        }

        public static void SetValue(this KryptonTextBox kryptonTextBox, object value, string bindFormat)
        {
            if (value == null)
            {
                kryptonTextBox.Text = null;
            }
            else if (value is string)
            {
                kryptonTextBox.Text = value.ToString();
            }
            else if (value is DateTime)
            {
                if (value == null)
                    kryptonTextBox.Text = string.Empty;
                else if (!string.IsNullOrEmpty(bindFormat))
                    kryptonTextBox.Text = ((DateTime)value).ToString(bindFormat);
                else
                    kryptonTextBox.Text = ((DateTime)value).ToString(DATA_FROMAT);
            }
            else if (value is SqlDateTime)
            {
                if (((SqlDateTime)value).IsNull)
                    kryptonTextBox.Text = string.Empty;
                else if (!string.IsNullOrEmpty(bindFormat))
                    kryptonTextBox.Text = ((DateTime)value).ToString(bindFormat);
                else
                    kryptonTextBox.Text = ((DateTime)value).ToString(DATA_FROMAT);
            }
            else if (value is decimal || value is int || value is Int32 || value is Int64 || value is Int16 || value is double || value is float)
            {
                if (!string.IsNullOrEmpty(bindFormat))
                    kryptonTextBox.Text = (Convert.ToDecimal(value)).ToString(bindFormat);
                else
                    kryptonTextBox.Text = (Convert.ToDecimal(value)).ToString();
            }
        }

        public static BindingContextCustomAttribute GetCustomAttribute(this KryptonTextBox kryptonTextBox)
        {
            return GetProvider().GetCustomAttribute(kryptonTextBox);
        }

        private static BindingContextAttributeProvider GetProvider()
        {
            return ServiceHelper.GetService<BindingContextAttributeProvider>() ??
        throw new InvalidOperationException("BindingContextAttributeProvider is not registered in the service GetProvider().");
        }
    }
}

