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

namespace HDL_ON.Stan
{
    /// <summary>
    /// 设备功能卡片的底层界面
    /// </summary>
    public class DeviceFunctionCardCommonForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________

        /// <summary>
        /// 当前回路对象
        /// </summary>
        public Function device = null;
        /// <summary>
        /// 右上角配置结束的事件
        /// </summary>
        public Action SettionFinishEvent = null;
        /// <summary>
        /// 左右翻页的控件的事件
        /// </summary>
        public Action<int> PageChangeEvent = null;
        /// <summary>
        /// 左右整个翻页的控件
        /// </summary>
        private PageLayout pageLayout = null;
        /// <summary>
        /// 设备名字控件(刷新用到)
        /// </summary>
        public NormalViewControl btnDeviceName = null;
        /// <summary>
        /// 房间名字控件(刷新用到)
        /// </summary>
        public NormalViewControl btnRoomName = null;
        /// <summary>
        /// 底部快捷按钮1
        /// </summary>
        private NormalViewControl btnBottomOne = null;
        /// <summary>
        /// 底部快捷按钮2
        /// </summary>
        private NormalViewControl btnBottomTwo = null;
        /// <summary>
        /// 第一索引的白色区域的桌布控件
        /// </summary>
        public NormalFrameLayout FrameWhiteCentet1 = null;
        /// <summary>
        /// 第二索引的白色区域的桌布控件(请先调用AddSecondPage函数完成添加)
        /// </summary>
        public NormalFrameLayout FrameWhiteCentet2 = null;

        /// <summary>
        /// 是否显示收藏按钮
        /// </summary>
        public bool ShowColltionButton = true;
        #endregion

        #region ■ 初始化_____________________________

        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_device">设备对象</param>
        /// <param name="btnHigherCollection">上一级界面的收藏控件(原来on的框架太坑)</param>
        /// <param name="btnHigherDeviceName">上一级界面的设备名字控件(原来on的框架太坑)</param>
        /// <param name="btnHigherRoom">上一级界面的房间名字控件(原来on的框架太坑)</param>
        public void ShowForm(Function i_device, Button btnHigherCollection, Button btnHigherDeviceName, Button btnHigherRoom)
        {
            this.device = i_device;

            //添加头部【设置】图标
            this.AddTopSettingIcon(btnHigherDeviceName, btnHigherRoom);

            //初始化中部信息
            this.InitMiddleFrame(btnHigherCollection);
        }

        /// <summary>
        /// 初始化中部信息
        /// </summary>
        /// <param name="btnHigherCollection">上一级界面的收藏控件</param>
        private void InitMiddleFrame(Button btnHigherCollection)
        {
            //清空bodyFrame
            this.ClearBodyFrame();

            //左右整个翻页的控件
            this.pageLayout = new PageLayout();
            pageLayout.IsShowPoint = false;
            pageLayout.ScrollEnabled = false;
            bodyFrameLayout.AddChidren(pageLayout);
            pageLayout.PageChange += (sender, index) =>
            {
                //设置底部两个按钮的状态
                this.SetBottomTwoButtonStatu();
                this.PageChangeEvent?.Invoke(index);
            };

            //初始化第一个索引的桌布
            this.InitFrameWhiteContent1(btnHigherCollection);
            //初始化白色内容(需要自主实现)
            this.InitFrameWhiteContent();
        }

        /// <summary>
        /// 初始化第一个索引的桌布
        /// </summary>
        /// <param name="btnHigherCollection">上一级界面的收藏控件</param>
        private void InitFrameWhiteContent1(Button btnHigherCollection)
        {
            //第一个索引桌布是必定存在的
            var frameTable1 = new FrameLayout();
            frameTable1.Width = pageLayout.Width;
            frameTable1.Height = pageLayout.Height;
            pageLayout.AddChidren(frameTable1);
            //第一个索引的白色桌布
            this.FrameWhiteCentet1 = new NormalFrameLayout();
            FrameWhiteCentet1.Gravity = Gravity.CenterHorizontal;
            FrameWhiteCentet1.Y = Application.GetRealHeight(22);
            FrameWhiteCentet1.Width = Application.GetRealWidth(327);
            FrameWhiteCentet1.Height = Application.GetRealHeight(526);
            FrameWhiteCentet1.BackgroundImagePath = "Public/Fragmentbg.png";
            frameTable1.AddChidren(FrameWhiteCentet1);
            //设备名字
            this.btnDeviceName = new NormalViewControl(270, 37, true);
            btnDeviceName.X = Application.GetRealWidth(16);
            btnDeviceName.Y = Application.GetRealHeight(18);
            btnDeviceName.TextColor = CSS_Color.FirstLevelTitleColor;
            btnDeviceName.TextSize = CSS_FontSize.EmphasisFontSize_FirstLevel;
            btnDeviceName.Text = device.name;
            btnDeviceName.Width = btnDeviceName.GetRealWidthByText();
            FrameWhiteCentet1.AddChidren(btnDeviceName);
            //房间名字
            this.btnRoomName = new NormalViewControl(270, 21, true);
            btnRoomName.X = Application.GetRealWidth(16);
            btnRoomName.Y = btnDeviceName.Bottom;
            btnRoomName.TextColor = CSS_Color.PromptingColor1;
            btnRoomName.TextSize = CSS_FontSize.PromptFontSize_FirstLevel;
            btnRoomName.Text = device.GetRoomListName();
            btnRoomName.Width = btnRoomName.GetRealWidthByText();
            FrameWhiteCentet1.AddChidren(btnRoomName);
            if (ShowColltionButton)
            {
                var btnCollection = new IconViewControl(40);
                btnCollection.X = Application.GetRealWidth(273);
                btnCollection.Y = Application.GetRealHeight(16);
                btnCollection.SelectedImagePath = "Collection/CollectionIcon.png";
                btnCollection.UnSelectedImagePath = "Collection/CollectionGrayIcon.png";
                btnCollection.IsSelected = device.collect;
                FrameWhiteCentet1.AddChidren(btnCollection);
                btnCollection.ButtonClickEvent += (sender, e) =>
                {
                    btnCollection.IsSelected = !btnCollection.IsSelected;
                    device.collect = btnCollection.IsSelected;
                    btnHigherCollection.IsSelected = device.collect;

                    device.CollectFunction();
                };
            }
        }

        /// <summary>
        /// 初始化第二个索引的桌布
        /// </summary>
        private void InitFrameWhiteContent2()
        {
            this.pageLayout.ScrollEnabled = true;
            //第一个索引桌布是必定存在的
            var frameTable2 = new FrameLayout();
            frameTable2.Width = pageLayout.Width;
            frameTable2.Height = pageLayout.Height;
            pageLayout.AddChidren(frameTable2);
            //第一个索引的白色桌布
            this.FrameWhiteCentet2 = new NormalFrameLayout();
            FrameWhiteCentet2.Gravity = Gravity.CenterHorizontal;
            FrameWhiteCentet2.Y = Application.GetRealHeight(22);
            FrameWhiteCentet2.Width = Application.GetRealWidth(327);
            FrameWhiteCentet2.Height = Application.GetRealHeight(526);
            FrameWhiteCentet2.BackgroundImagePath = "Public/Fragmentbg.png";
            frameTable2.AddChidren(FrameWhiteCentet2);
        }

        /// <summary>
        /// 初始化白色内容(需要自主实现)
        /// </summary>
        public virtual void InitFrameWhiteContent()
        {
        }

        #endregion

        #region ■ 添加第二索引页_____________________

        /// <summary>
        /// 添加第二索引页
        /// </summary>
        public void AddSecondPage()
        {
            //初始化第二个索引的桌布
            this.InitFrameWhiteContent2();

            //底部添加两个按钮
            this.btnBottomOne = new NormalViewControl(14, 6, true);
            btnBottomOne.X = Application.GetRealWidth(170);
            btnBottomOne.Y = FrameWhiteCentet1.Bottom + Application.GetRealHeight(30);
            btnBottomOne.BackgroundColor = CSS_Color.MainColor;
            btnBottomOne.Radius = (uint)Application.GetRealHeight(6) / 2;
            bodyFrameLayout.AddChidren(btnBottomOne);
            btnBottomOne.ButtonClickEvent += (sender, e) =>
            {
                this.pageLayout.PageIndex = 0;
                //设置底部两个按钮的状态
                this.SetBottomTwoButtonStatu();
                if (this.FrameWhiteCentet2 != null && this.pageLayout.ScrollEnabled == false)
                {
                    this.pageLayout.ScrollEnabled = true;
                }
            };
            this.btnBottomTwo = new NormalViewControl(14, 6, true);
            btnBottomTwo.X = btnBottomOne.Right + Application.GetRealWidth(8);
            btnBottomTwo.Y = btnBottomOne.Y;
            btnBottomTwo.BackgroundColor = 0xffadc7f7;
            btnBottomTwo.Radius = (uint)Application.GetRealHeight(6) / 2;
            bodyFrameLayout.AddChidren(btnBottomTwo);
            btnBottomTwo.ButtonClickEvent += (sender, e) =>
            {
                this.pageLayout.PageIndex = 1;
                //设置底部两个按钮的状态
                this.SetBottomTwoButtonStatu();
                if (this.FrameWhiteCentet2 != null && this.pageLayout.ScrollEnabled == false)
                {
                    this.pageLayout.ScrollEnabled = true;
                }
            };
        }

        /// <summary>
        /// 设置底部两个按钮的状态
        /// </summary>
        private void SetBottomTwoButtonStatu()
        {
            if (this.btnBottomTwo == null) { return; }

            if (this.pageLayout.PageIndex == 1)
            {
                this.btnBottomOne.BackgroundColor = 0xffadc7f7;
                this.btnBottomTwo.BackgroundColor = CSS_Color.MainColor;
            }
            else
            {
                this.btnBottomOne.BackgroundColor = CSS_Color.MainColor;
                this.btnBottomTwo.BackgroundColor = 0xffadc7f7;
            }
        }

        #endregion

        #region ■ 界面关闭___________________________

        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            base.CloseFormBefore();

            this.SettionFinishEvent = null;
            this.PageChangeEvent = null;
        }

        #endregion

        #region ■ 一般方法___________________________

        /// <summary>
        /// 获取头部右边的【设置】图标控件
        /// </summary>
        /// <returns></returns>
        public PicViewControl GetTopSettingIcon()
        {
            for (int i = 0; i < topFrameLayout.ChildrenCount; i++)
            {
                var myView = topFrameLayout.GetChildren(i);
                if (myView == null || myView.Name != "btnSetting")
                {
                    continue;
                }
                return myView as PicViewControl;
            }
            return null;
        }

        /// <summary>
        /// 添加头部【设置】的图标
        /// </summary>
        /// <param name="btnHigherCollection">上一级界面的收藏控件(原来on的框架太坑)</param>
        /// <param name="btnHigherDeviceName">上一级界面的设备名字控件(原来on的框架太坑)</param>
        private void AddTopSettingIcon(Button btnHigherDeviceName, Button btnHigherRoom)
        {
            //设置图标(门锁界面需要借用这个图标的X轴和Y轴,所以加进来)
            var btnSetting = new PicViewControl(28, 28);
            btnSetting.X = Application.GetRealWidth(337);
            btnSetting.Y = Application.GetRealHeight(9);
            btnSetting.UnSelectedImagePath = "Public/FuncInfoSetIcon.png";
            btnSetting.Name = "btnSetting";
            topFrameLayout.AddChidren(btnSetting);

            //如果是成员没有功能
            if (DB_ResidenceData.Instance.CurrentRegion.isOtherShare == true)
            {
                btnSetting.Visible = false;
            }

            btnSetting.ButtonClickEvent += (sender, e) =>
            {
                var infoView = new UI.FunctionBaseInfoSetPage(this.device, () =>
                {
                    //刷新显示
                    btnHigherDeviceName.Text = this.device.name;
                    btnHigherRoom.Text = this.device.GetRoomListName();

                    //从新计算宽度
                    this.btnDeviceName.Text = btnHigherDeviceName.Text;
                    this.btnDeviceName.Width = this.btnDeviceName.GetRealWidthByText();

                    this.btnRoomName.Text = btnHigherRoom.Text;
                    this.btnRoomName.Width = this.btnRoomName.GetRealWidthByText();

                    //回调事件
                    this.SettionFinishEvent?.Invoke();
                });
                MainPage.BasePageView.AddChidren(infoView);
                infoView.LoadPage();
                MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
            };
        }

        #endregion
    }
}