using CloudBuilder;
using CloudBuilder.Gui;
using CloudBuilder.Gui.DependencyInjection;
using Krypton.Toolkit;
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 KryptonMaskedTextBoxExtensions
    {
        public const string DATA_FROMAT = "yyyy-MM-dd";

        public static ExFormSnapshotEntry GetExFormSnapshotEntry(this KryptonMaskedTextBox kryptonMaskedTextBox)
        {
            string bindDataName = null;

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

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

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

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

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

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

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

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


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

        public static void Set(this KryptonMaskedTextBox kryptonMaskedTextBox,
            string bindDataName,
            string bindFormat,
            EnumEditModeColor editModeColor)
        {
            GetProvider().SetCustomAttribute(kryptonMaskedTextBox, bindDataName, bindFormat, editModeColor);
        }

        public static void Set(this KryptonMaskedTextBox kryptonMaskedTextBox,
            string bindDataName,
            string bindFormat,
            EnumBindDirection bindDirection)
        {
            GetProvider().SetCustomAttribute(kryptonMaskedTextBox, bindDataName, bindFormat, bindDirection);
        }

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

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

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

        public static string GetValue(this KryptonMaskedTextBox kryptonMaskedTextBox)
        {
            if (IsEmpty(kryptonMaskedTextBox)) return string.Empty;
            return kryptonMaskedTextBox.Text.Trim();
        }

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

        public static void SetValue(this KryptonMaskedTextBox kryptonMaskedTextBox, object value, string bindFormat)
        {
            if (value == null)
            {
                kryptonMaskedTextBox.Text = null;
            }
            else if (value is string)
            {
                kryptonMaskedTextBox.Text = value.ToString();
            }
            else if (value is DateTime)
            {
                if (value == null)
                    kryptonMaskedTextBox.Text = string.Empty;
                else if (!string.IsNullOrEmpty(bindFormat))
                    kryptonMaskedTextBox.Text = ((DateTime)value).ToString(bindFormat);
                else
                    kryptonMaskedTextBox.Text = ((DateTime)value).ToString(DATA_FROMAT);
            }
            else if (value is SqlDateTime)
            {
                if (((SqlDateTime)value).IsNull)
                    kryptonMaskedTextBox.Text = string.Empty;
                else if (!string.IsNullOrEmpty(bindFormat))
                    kryptonMaskedTextBox.Text = ((DateTime)value).ToString(bindFormat);
                else
                    kryptonMaskedTextBox.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))
                    kryptonMaskedTextBox.Text = (Convert.ToDecimal(value)).ToString(bindFormat);
                else
                    kryptonMaskedTextBox.Text = (Convert.ToDecimal(value)).ToString();
            }
        }

        public static bool IsEmpty(this KryptonMaskedTextBox kryptonMaskedTextBox)
        {
            string mark = System.Text.RegularExpressions.Regex.Replace(kryptonMaskedTextBox.Mask, @"\d", string.Empty);
            if (mark.Length > 0)
                mark = mark.Substring(0, 1);
            if (string.IsNullOrEmpty(kryptonMaskedTextBox.Text.Replace(kryptonMaskedTextBox.PromptChar.ToString(), string.Empty).Replace(mark, string.Empty))) return true;
            return false;
        }

        public static BindingContextCustomAttribute GetCustomAttribute(this KryptonMaskedTextBox kryptonMaskedTextBox)
        {
            return GetProvider().GetCustomAttribute(kryptonMaskedTextBox);
        }

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