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

namespace HDL_ON.Stan
{
    /// <summary>
    /// 有标题的弹窗型菜单选择控件(Y轴最好减掉12)
    /// </summary>
    public class DialogTitleMenuControl : NormalFrameLayout
    {
        #region ■ 变量声明___________________________

        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalListControl listView = null;
        /// <summary>
        /// 列表控件是否能滚动
        /// </summary>
        private bool listViewScroll = false;
        /// <summary>
        /// 标题(初始化之后会置空)
        /// </summary>
        private string StrTitle = null;
        /// <summary>
        /// 行高度
        /// </summary>
        private int RowHeight = HdlControlResourse.ListViewRowHeight;
        /// <summary>
        /// 行数
        /// </summary>
        private int RowCount = 0;

        #endregion

        #region ■ 初始化_____________________________

        /// <summary>
        /// 有标题的弹窗型菜单选择控件(Y轴最好减掉12)
        /// </summary>
        /// <param name="i_RowCount">菜单行数(不含标题)</param>
        /// <param name="i_title">标题</param>
        public DialogTitleMenuControl(int i_RowCount, string i_title)
        {
            //最大显示5个zzy //4个。。没有5个的背景图wxr
            this.RowCount = i_RowCount > 4 ? 4 : i_RowCount;
            this.listViewScroll = i_RowCount > 4;
            this.StrTitle = i_title;
            this.Height = Application.GetRealHeight(64 + 45 * this.RowCount);
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //已经初始化过,不需要再次初始化
            if (this.StrTitle == null) { return; }

            this.BackgroundImagePath = "FunctionIcon/Electrical/Fan/DialogTitleMenuGroud" + this.RowCount + ".png";

            //标题行
            var rowTitle = new FrameRowControl();
            rowTitle.LeftOffset = Application.GetRealWidth(24) - HdlControlResourse.XXLeft;
            rowTitle.Y = Application.GetRealHeight(8);
            rowTitle.Width = this.Width;
            rowTitle.Height = this.RowHeight;
            this.AddChidren(rowTitle);
            //标题
            var btnTitle = rowTitle.AddLeftCaption(this.StrTitle, 100);
            //从X轴开始,铺满整一行
            btnTitle.Width = rowTitle.Width - btnTitle.X;
            btnTitle.TextColor = CSS_Color.FirstLevelTitleColor;
            btnTitle.TextSize = CSS_FontSize.SubheadingFontSize;
            //线
            var btnLine = rowTitle.AddBottomLine();
            btnLine.Width = rowTitle.Width - Application.GetRealWidth(24) * 2;
            btnLine.Gravity = Gravity.CenterHorizontal;
            btnLine.BackgroundColor = CSS_Color.BackgroundColor;

            this.StrTitle = null;

            //列表控件
            this.listView = new VerticalListControl();
            listView.Y = rowTitle.Bottom;
            listView.Height = this.RowCount * this.RowHeight;
            listView.ScrollEnabled = this.listViewScroll;
            this.AddChidren(listView);

        }

        #endregion

        #region ■ 添加菜单___________________________

        /// <summary>
        /// 添加菜单行(它最终的父控件需要手动关闭)
        /// </summary>
        /// <param name="i_textValue">显示的文字</param>
        /// <param name="i_iconPath">图片(</param>
        /// <param name="i_select">是否是选择状态</param>
        /// <param name="clickEvent">单击菜单执行的事件</param>
        public void AddRowMenu(string i_textValue, string i_iconPath, bool i_select, Action clickEvent)
        {
            //先初始化控件
            this.InitControl();

            uint fontColor = i_select == true ? CSS_Color.MainColor : CSS_Color.FirstLevelTitleColor;
            //生成行控件
            var frameRow = this.CreatRowControl(i_iconPath, i_textValue, fontColor);
            frameRow.ButtonClickEvent += (sender, e) =>
            {
                //调用回调函数
                clickEvent?.Invoke();
                clickEvent = null;
            };
        }

        /// <summary>
        /// 生成行控件
        /// </summary>
        /// <param name="i_iconPath">图标</param>
        /// <param name="i_text">显示的文字</param>
        /// <param name="i_fontColor">字体颜色</param>
        /// <returns></returns>
        private FrameRowControl CreatRowControl(string i_iconPath, string i_text, uint i_fontColor)
        {
            //它的上一行
            var rowBefor = this.listView.GetChildren(this.listView.ChildrenCount - 1) as FrameRowControl;
            if (rowBefor != null)
            {
                //画底线
                var btnLine = rowBefor.AddBottomLine();
                btnLine.Width = rowBefor.Width - Application.GetRealWidth(24) * 2;
                btnLine.Gravity = Gravity.CenterHorizontal;
                btnLine.BackgroundColor = CSS_Color.BackgroundColor;
            }

            //行
            var rowContr = new FrameRowControl();
            rowContr.LeftOffset = Application.GetRealWidth(24) - HdlControlResourse.XXLeft;
            rowContr.Width = this.Width;
            rowContr.Height = this.RowHeight;
            this.listView.AddChidren(rowContr);
            //图标
            rowContr.AddLeftIcon(24, i_iconPath);
            //显示文本
            var btnView = rowContr.AddLeftCaption(i_text, 92);
            //从X轴开始,铺满整一行
            btnView.Width = rowContr.Width - btnView.X;
            btnView.TextColor = i_fontColor;

            return rowContr;
        }

        #endregion
    }
}