using Shared;
using HDL_ON.UI.CSS;
using System;
using System.Text;

namespace HDL_ON.Stan
{
    /// <summary>
    /// 文本输入的弹窗界面(不用加入父控件)
    /// </summary>
    public class TextInputDialog
    {
        #region ■ 变量声明___________________________

        /// <summary>
        /// 标题文本
        /// </summary>
        private string titleText = string.Empty;
        /// <summary>
        /// 初始值
        /// </summary>
        private string textValue = string.Empty;
        /// <summary>
        /// 当输入框为空白时的提示文本
        /// </summary>
        private string emptyMsg = string.Empty;
        /// <summary>
        /// 确认按钮的文本
        /// </summary>
        private string buttonOkText = string.Empty;
        /// <summary>
        /// 取消按钮的文本
        /// </summary>
        private string buttonCancelText = string.Empty;
        /// <summary>
        /// 是否是密码输入
        /// </summary>
        private bool isPassword = false;

        #endregion

        #region ■ 初始化_____________________________

        /// <summary>
        /// 文本输入的弹窗界面(不用加入父控件)
        /// </summary>
        /// <param name="i_title">标题文本</param>
        /// <param name="i_text">初始值</param>
        /// <param name="i_emptyMsg">当输入框为空白时的提示文本</param>
        /// <param name="i_buttonOkText">确认按钮的文本</param>
        /// <param name="i_buttonCancelText">取消按钮的文本</param>
        /// <param name="i_isPassword">是否是密码输入</param>
        public TextInputDialog(string i_title, string i_text, string i_emptyMsg, string i_buttonOkText = null, string i_buttonCancelText = null, bool i_isPassword = false)
        {
            this.titleText = i_title;
            this.textValue = i_text;
            this.emptyMsg = i_emptyMsg;

            //确认按钮文本
            this.buttonOkText = i_buttonOkText == null ? Language.StringByID(StringId.Confirm) : i_buttonOkText;
            this.buttonCancelText = i_buttonCancelText == null ? Language.StringByID(StringId.Cancel) : i_buttonCancelText;

            this.isPassword = i_isPassword;
        }

        #endregion

        #region ■ 弹窗显示___________________________

        /// <summary>
        /// 弹窗显示
        /// </summary>
        /// <param name="finishEvent">回调函数,参数为输入框的值</param>
        public void Show(Action<string> finishEvent)
        {
            var dialogForm = new Dialog();
            dialogForm.BackgroundColor = CSS_Color.DialogTransparentColor1;
            //主控件
            var frameMain = new NormalFrameLayout();
            dialogForm.AddChidren(frameMain);
            dialogForm.Show();

            //中间区域
            var frameCenter = new NormalFrameLayout();
            frameCenter.Gravity = Gravity.Center;
            frameCenter.Width = Application.GetRealWidth(270);
            frameCenter.Height = Application.GetRealHeight(50);
            frameCenter.BackgroundColor = CSS_Color.MainBackgroundColor;
            frameCenter.Radius = (uint)Application.GetMinRealAverage(10);
            frameMain.AddChidren(frameCenter);

            //标题
            var btnTitle = new NormalViewControl(frameCenter.Width - HdlControlResourse.XXLeft * 2, Application.GetRealHeight(24), false);
            btnTitle.Y = Application.GetRealHeight(19);
            btnTitle.Gravity = Gravity.CenterHorizontal;
            btnTitle.TextColor = CSS_Color.MainColor;
            btnTitle.TextSize = CSS_FontSize.SubheadingFontSize;
            btnTitle.TextAlignment = TextAlignment.Center;
            btnTitle.IsBold = true;
            btnTitle.Text = this.titleText.Replace("{0}", "\r\n");
            btnTitle.Height = Application.GetRealHeight(24) * btnTitle.GetRealRowCountByText();
            btnTitle.IsMoreLines = true;
            frameCenter.AddChidren(btnTitle);

            //输入框的背景
            var frameInput = new FrameLayout();
            frameInput.Y = btnTitle.Bottom + Application.GetRealHeight(16);
            frameInput.Width = Application.GetRealWidth(222);
            frameInput.Height = Application.GetRealHeight(40);
            frameInput.Gravity = Gravity.CenterHorizontal;
            frameInput.Radius = (uint)Application.GetRealWidth(4);
            frameInput.BackgroundColor = CSS_Color.BackgroundColor;
            frameCenter.AddChidren(frameInput);
            //输入框
            var txtInput = new TextInputControl(174, 24, true);
            txtInput.X = Application.GetRealWidth(12);
            txtInput.Gravity = Gravity.CenterVertical;
            txtInput.TextColor = CSS_Color.FirstLevelTitleColor;
            txtInput.Text = this.textValue;
            frameInput.AddChidren(txtInput);

            //密码型
            if (this.isPassword == true)
            {
                txtInput.IsNumberKeyboardType = true;
                txtInput.SecureTextEntry = true;
                //密码可视图标
                var btnIcon = new IconViewControl(24);
                btnIcon.X = txtInput.Right + Application.GetRealWidth(8);
                btnIcon.Gravity = Gravity.CenterVertical;
                btnIcon.UnSelectedImagePath = "LoginIcon/HidePasswordIcon.png";
                btnIcon.SelectedImagePath = "LoginIcon/ShowPasswordIcon.png";
                frameInput.AddChidren(btnIcon);
                btnIcon.ButtonClickEvent += (sender, e) =>
                {
                    btnIcon.IsSelected = !btnIcon.IsSelected;
                    txtInput.SecureTextEntry = !txtInput.SecureTextEntry;
                };
            }
            else
            {
                //取消图标
                var btnIcon = new IconViewControl(24);
                btnIcon.X = txtInput.Right + Application.GetRealWidth(8);
                btnIcon.Gravity = Gravity.CenterVertical;
                btnIcon.UnSelectedImagePath = "LoginIcon/1.png";
                frameInput.AddChidren(btnIcon);
                btnIcon.ButtonClickEvent += (sender, e) =>
                {
                    txtInput.Text = string.Empty;
                };
            }

            //错误显示消息
            var btnErrorMsg = new NormalViewControl(frameInput.Width, Application.GetRealHeight(21), false);
            btnErrorMsg.Y = frameInput.Bottom;
            btnErrorMsg.Gravity = Gravity.CenterHorizontal;
            btnErrorMsg.TextColor = CSS_Color.WarningColor;
            btnErrorMsg.Text = this.emptyMsg.Replace("{0}", "\r\n");
            btnErrorMsg.IsMoreLines = true;
            btnErrorMsg.TextAlignment = TextAlignment.TopLeft;
            btnErrorMsg.Height = Application.GetRealHeight(21) * btnErrorMsg.GetRealRowCountByText();
            btnErrorMsg.Visible = false;
            frameCenter.AddChidren(btnErrorMsg);

            //底部按钮的高度
            int buttomButtonHeigth = Application.GetRealHeight(43);
            //调整白色桌布的高度和坐标
            frameCenter.Height = frameInput.Bottom + Application.GetRealHeight(31) + buttomButtonHeigth;
            //白色背景在蓝湖上的坐标为264,高度为172  然后让它按这个比例置于桌布
            frameCenter.Y = Application.GetRealHeight(264) - (frameCenter.Height - Application.GetRealHeight(172)) / 2;

            //取消
            var btnCancel = new NormalViewControl(frameCenter.Width / 2, buttomButtonHeigth, false);
            btnCancel.Gravity = Gravity.BottomLeft;
            btnCancel.TextAlignment = TextAlignment.Center;
            btnCancel.TextSize = CSS_FontSize.SubheadingFontSize;
            btnCancel.Text = this.buttonCancelText;
            frameCenter.AddChidren(btnCancel);
            btnCancel.ButtonClickEvent += (sender, e) =>
            {
                //关闭界面
                dialogForm.Close();
                finishEvent = null;
            };
            //线
            var btnLine = new NormalViewControl(frameCenter.Width / 2, HdlControlResourse.BottomLineHeight, false);
            btnLine.Y = btnCancel.Y - HdlControlResourse.BottomLineHeight;
            btnLine.BackgroundColor = CSS_Color.DividingLineColor;
            frameCenter.AddChidren(btnLine);

            //确认
            var btnConfirm = new NormalViewControl(frameCenter.Width - btnCancel.Width, buttomButtonHeigth + HdlControlResourse.BottomLineHeight, false);
            btnConfirm.X = btnCancel.Right;
            btnConfirm.Y = btnLine.Y;
            btnConfirm.TextAlignment = TextAlignment.Center;
            btnConfirm.TextSize = CSS_FontSize.SubheadingFontSize;
            btnConfirm.TextColor = CSS_Color.MainBackgroundColor;
            btnConfirm.BackgroundColor = CSS_Color.MainColor;
            btnConfirm.Text = this.buttonOkText;
            frameCenter.AddChidren(btnConfirm);
            btnConfirm.SetCornerWithSameRadius((uint)Application.GetMinRealAverage(10), HDLUtils.RectCornerBottomRight);
            btnConfirm.ButtonClickEvent += (sender, e) =>
            {
                string inputValue = txtInput.Text.Trim();
                if (inputValue == string.Empty && string.IsNullOrEmpty(this.emptyMsg) != true)
                {
                    //空白的时候,提示消息
                    btnErrorMsg.Visible = true;

                    //看看消息显示的控件有没有大于31,大于的话,则算出它增加的宽度(31是输入框到底部按钮Y轴的空白区域)
                    int value = 0;
                    if (btnErrorMsg.Height > Application.GetRealHeight(31))
                    {
                        //5是与底部按钮Y轴的间距
                        value = btnErrorMsg.Height - Application.GetRealHeight(31) + Application.GetRealHeight(5);
                    }
                    //调整白色桌布的高度和坐标
                    frameCenter.Height = frameInput.Bottom + Application.GetRealHeight(31) + buttomButtonHeigth + value;
                    //白色背景在蓝湖上的坐标为264,高度为172  然后让它按这个比例置于桌布
                    frameCenter.Y = Application.GetRealHeight(264) - (frameCenter.Height - Application.GetRealHeight(172)) / 2;
                    //两个按钮置底
                    btnCancel.Gravity = Gravity.BottomLeft;
                    btnLine.Y = btnCancel.Y - HdlControlResourse.BottomLineHeight;
                    btnConfirm.Y = btnLine.Y;

                    return;
                }
                //关闭界面
                dialogForm.Close();
                //回调函数
                finishEvent?.Invoke(txtInput.Text.Trim());
            };
        }

        #endregion
    }
}