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
    }
}