using System;
using HDL_ON.Entity;
using HDL_ON.UI.CSS;
using HDL_ON.UI.Music;
using Shared;

namespace HDL_ON.UI
{
    public partial class FunctionControlZone : FrameLayout
    {
        #region 区域控件
        static FrameLayout bodyDiv;
        /// <summary>
        /// 功能/场景icon
        /// </summary>
        Button btnIcon;
        /// <summary>
        /// 功能名称/场景名称
        /// </summary>
        Button btnName;
        /// <summary>
        /// 楼层信息显示按钮
        /// </summary>
        Button btnFromFloor;
        /// <summary>
        /// 收藏按钮
        /// </summary>
        Button btnCollectionIcon;
        #endregion

        #region 区域变量
        Function function;
        /// <summary>
        /// 是否在调光
        /// </summary>
        bool onDimmerBar;
        #endregion
        public FunctionControlZone(Function func)
        {
            bodyDiv = this;
            bodyDiv.Tag = func.sid;
            function = func;
        }

        public override void RemoveFromParent()
        {
            base.RemoveFromParent();
            bodyDiv = null;
        }

        /// <summary>
        /// 加载功能类型控制卡片
        /// </summary>
        public void LoadFunctionDiv()
        {
            //todo 增加设备,增加功能列表卡片
            if (function == null)
            {
                this.RemoveFromParent();
                return;
            }
            LoadDiv();

            btnIcon.UnSelectedImagePath = $"FunctionIcon/Icon/{function.IconName}.png";

            if (function.Spk_Prefix == FunctionCategory.Curtain//窗帘没有开关按钮
                || function.Spk_Prefix == FunctionCategory.Music//音乐没有开关按钮
                || function.Spk_Prefix == FunctionCategory.Sensor//传感器没有开关按钮
                )
            {
                switch (function.Spk_Prefix)
                {
                    case FunctionCategory.Curtain:
                        CurtainFragment();
                        break;
                    case FunctionCategory.Sensor:
                        var powerLowTipAttr = function.attributes.Find((obj) => obj.key == FunctionAttributeKey.BatteryState);
                        if (powerLowTipAttr != null)
                        {
                            if (powerLowTipAttr.state.ToLower() == "low")
                            {
                                Button btnTipPowerLow = new Button()
                                {
                                    X = Application.GetRealWidth(307),
                                    Y = Application.GetRealHeight(25),
                                    Width = Application.GetRealWidth(24),
                                    Height = Application.GetRealWidth(24),
                                    UnSelectedImagePath = "FunctionIcon/ArmSensor/PowerLowIcon.png",
                                    SelectedImagePath = "FunctionIcon/ArmSensor/PowerLowIcon.png",
                                };
                                this.AddChidren(btnTipPowerLow);
                            }
                        }

                        break;
                    case FunctionCategory.Music:
                        MusicFragment();
                        break;

                }
            }
            else
            {
                if (function.spk == SPK.ClothesHanger)//晾衣架
                {
                    ClothesHangerFragment();
                }
                else
                {
                    #region 加载开关按钮

                    /// <summary>
                    /// 开关按钮
                    /// </summary>
                    Button btnSwitch;
                    if (function.spk == SPK.LightRGB || function.spk == SPK.LightDimming)
                    {
                        btnSwitch = new Button()
                        {
                            X = Application.GetRealWidth(303),
                            Y = Application.GetRealHeight(78),
                            Width = Application.GetMinRealAverage(32),
                            Height = Application.GetMinRealAverage(32),
                            UnSelectedImagePath = "Public/PowerClose.png",
                            SelectedImagePath = "Public/PowerOpen.png",
                            IsSelected = function.trait_on_off.curValue.ToString() == "on",
                            Tag = function.sid + "_Switch",
                        };
                        bodyDiv.AddChidren(btnSwitch);
                        //加载开关按钮事件
                        LoadEvent_SwitchFunction(btnSwitch, function.attributes.Find((obj) => obj.key == FunctionAttributeKey.FadeTime));
                    }
                    else
                    {
                        btnSwitch = new Button()
                        {
                            X = Application.GetRealWidth(303),
                            Y = Application.GetRealHeight(58),
                            Width = Application.GetMinRealAverage(32),
                            Height = Application.GetMinRealAverage(32),
                            UnSelectedImagePath = "Public/PowerClose.png",
                            SelectedImagePath = "Public/PowerOpen.png",
                            IsSelected = function.trait_on_off.curValue.ToString() == "on",
                            Tag = function.sid + "_Switch",
                        };
                        bodyDiv.AddChidren(btnSwitch);
                        //加载开关按钮事件
                        LoadEvent_SwitchFunction(btnSwitch);
                    }

                    switch (function.Spk_Prefix)
                    {
                        case FunctionCategory.AC:
                            LoadLastStatesButton();
                            break;
                        case FunctionCategory.IR:
                            LoadLastStatesButton();
                            break;
                        case FunctionCategory.FloorHeat:
                            LoadLastStatesButton();
                            break;
                        case FunctionCategory.Light:
                            btnSwitch.IsSelected = function.trait_on_off.curValue.ToString() == "on";
                            LoadLightControl();
                            break;
                        case FunctionCategory.Electric:
                            btnSwitch.IsSelected = function.trait_on_off.curValue.ToString() == "on";
                            break;
                    }
                    #endregion
                }
            }

            LoadEvent_DivSkipEvent();
            LoadEvent_FunctionCollection();
        }

        /// <summary>
        /// 加载控制卡片区域
        /// </summary>
        void LoadDiv()
        {
            btnIcon = new Button()
            {
                X = Application.GetRealWidth(10),
                Y = Application.GetRealHeight(15),
                Width = Application.GetRealWidth(32),
                Height = Application.GetRealWidth(32),
            };
            bodyDiv.AddChidren(btnIcon);

            btnName = new Button()
            {
                X = Application.GetRealWidth(8 +10 + 32),
                Y = Application.GetRealHeight(10),
                Width = Application.GetRealWidth(200),
                Height = Application.GetRealHeight(24),
                Text = function.name,
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.TextFontSize,
            };
            bodyDiv.AddChidren(btnName);

            btnFromFloor = new Button()
            {
                X = Application.GetRealWidth(8 + 10 + 32),
                Y = Application.GetRealHeight(10 + 24),
                Width = Application.GetRealWidth(200),
                Height = Application.GetRealHeight(18),
                Text = function.GetRoomListName(),
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = CSS_Color.PromptingColor1,
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
            };
            bodyDiv.AddChidren(btnFromFloor);

            btnCollectionIcon = new Button()
            {
                X = Application.GetRealWidth(299),
                Y = Application.GetRealHeight(10),
                Width = Application.GetMinRealAverage(40),
                Height = Application.GetMinRealAverage(40),
                SelectedImagePath = "Collection/CollectionIcon.png",
                UnSelectedImagePath = "Collection/CollectionGrayIcon.png",
                IsSelected = function.collect
            };
            //bodyDiv.AddChidren(btnCollectionIcon);
            //2020-12-16 如果是成员隐藏收藏功能
            if ( function.Spk_Prefix != FunctionCategory.Sensor)//!DB_ResidenceData.Instance.CurrentRegion.isOtherShare &&
            {
                bodyDiv.AddChidren(btnCollectionIcon);
            }
        }

        /// <summary>
        /// 加载灯光功能控制卡片
        /// </summary>
        void LoadLightControl()
        {
            switch (function.spk)
            {
                case SPK.LightDimming:
                case SPK.LightRGB:
                    #region Dimmer
                    var btnDimmerMinValues = new Button()
                    {
                        X = Application.GetRealWidth(14),
                        Y = Application.GetRealHeight(85),
                        Width = Application.GetRealWidth(30),
                        Height = Application.GetRealHeight(21),
                        Text = "0%",
                        TextAlignment = TextAlignment.CenterLeft,
                        TextColor = CSS_Color.PromptingColor1,
                        TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                    };
                    bodyDiv.AddChidren(btnDimmerMinValues);

                    var dimmerControlBar = new DiyImageSeekBar()
                    {
                        X = Application.GetRealWidth(14 + 22),
                        Y = Application.GetRealHeight(68),//91,设计数据
                        Width = Application.GetRealWidth(180),
                        Height = Application.GetRealHeight(54),
                        SeekBarViewHeight = Application.GetRealHeight(8),
                        ThumbImagePath = "Public/ThumbImage.png",
                        ThumbImageHeight = Application.GetRealHeight(54),
                        ProgressBarColor = function.trait_on_off.curValue.ToString() == "on" ? CSS_Color.AuxiliaryColor1 : CSS_Color.PromptingColor2,
                        ProgressTextColor = CSS_Color.FirstLevelTitleColor,
                        ProgressTextSize = CSS_FontSize.PromptFontSize_SecondaryLevel,
                        MaxValue = 100,
                        Progress = Convert.ToInt32(function.GetAttrState(FunctionAttributeKey.Brightness)),
                        Tag = function.sid + "_DimmerBar",
                        SeekBarPadding = Application.GetRealWidth(20),
                    };
                    bodyDiv.AddChidren(dimmerControlBar);

                    var btnDimmerMaxValues = new Button()
                    {
                        X = Application.GetRealWidth(14 + 22+180),
                        Y = Application.GetRealHeight(85),
                        Width = Application.GetRealWidth(45),
                        Height = Application.GetRealHeight(21),
                        Text = "100%",
                        TextAlignment = TextAlignment.CenterLeft,
                        TextColor = CSS_Color.PromptingColor1,
                        TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                    };
                    bodyDiv.AddChidren(btnDimmerMaxValues);
                    //加载调光事件
                    LoadEvent_LightDimming(dimmerControlBar);
                    #endregion
                    break;
            }
        }

        /// <summary>
        /// 加载窗帘功能控制卡片
        /// </summary>
        void CurtainFragment()
        {
            #region Curtain
            var btnCurtainClose = new Button()
            {
                X = Application.GetRealWidth(163),
                Y = Application.GetRealHeight(58),
                Width = Application.GetMinRealAverage(32),
                Height = Application.GetMinRealAverage(32),
                UnSelectedImagePath = "FunctionIcon/Curtain/CurtainCloseIcon.png",
                SelectedImagePath = "FunctionIcon/Curtain/CurtainCloseOnIcon.png",
                IsSelected = function.GetAttrState(FunctionAttributeKey.OnOff) == "off",
                Tag = function.sid + "_off"
            };
            bodyDiv.AddChidren(btnCurtainClose);
            if(function.spk == SPK.CurtainRoller)
            {
                btnCurtainClose.UnSelectedImagePath = "FunctionIcon/Curtain/CurtainDownIcon.png";
                btnCurtainClose.SelectedImagePath = "FunctionIcon/Curtain/CurtainDownIconOn.png";
            }

            var btnCurtainStop = new Button()
            {
                X = Application.GetRealWidth(233),
                Y = Application.GetRealHeight(58),
                Width = Application.GetMinRealAverage(32),
                Height = Application.GetMinRealAverage(32),
                UnSelectedImagePath = "FunctionIcon/Curtain/CurtainStopIcon.png",
                SelectedImagePath = "FunctionIcon/Curtain/CurtainStopOnIcon.png",
                Tag = function.sid + "_stop",
            };
            bodyDiv.AddChidren(btnCurtainStop);

            var btnCurtainOpen = new Button()
            {
                X = Application.GetRealWidth(303),
                Y = Application.GetRealHeight(58),
                Width = Application.GetMinRealAverage(32),
                Height = Application.GetMinRealAverage(32),
                UnSelectedImagePath = "FunctionIcon/Curtain/CurtainOpenIcon.png",
                SelectedImagePath = "FunctionIcon/Curtain/CurtainOpenOnIcon.png",
                Tag = function.sid + "_on",
                IsSelected = function.GetAttrState(FunctionAttributeKey.OnOff) == "on",
            };
            bodyDiv.AddChidren(btnCurtainOpen);
            if (function.spk == SPK.CurtainRoller)
            {
                btnCurtainOpen.UnSelectedImagePath = "FunctionIcon/Curtain/CurtainUpIcon.png";
                btnCurtainOpen.SelectedImagePath = "FunctionIcon/Curtain/CurtainUpIconOn.png";
            }
            LoadEvent_ControlCurtain(btnCurtainStop, btnCurtainOpen, btnCurtainClose, function);
            #endregion
        }
        /// <summary>
        /// 加载晾衣架功能控制卡片
        /// </summary>
        void ClothesHangerFragment()
        {
            #region ClothesHanger
            var btnClothesHangerUp = new Button()
            {
                X = Application.GetRealWidth(233),
                Y = Application.GetRealHeight(58),
                Width = Application.GetMinRealAverage(32),
                Height = Application.GetMinRealAverage(32),
                UnSelectedImagePath = "FunctionIcon/Electrical/ClothesHanger/ClothesHangerUpIcon1.png",
                SelectedImagePath = "FunctionIcon/Electrical/ClothesHanger/ClothesHangerUpIcon1On.png",
            };
            bodyDiv.AddChidren(btnClothesHangerUp);

            var btnClothesHangerDown = new Button()
            {
                X = Application.GetRealWidth(303),
                Y = Application.GetRealHeight(58),
                Width = Application.GetMinRealAverage(32),
                Height = Application.GetMinRealAverage(32),
                UnSelectedImagePath = "FunctionIcon/Electrical/ClothesHanger/ClothesHangerDownIcon1.png",
                SelectedImagePath = "FunctionIcon/Electrical/ClothesHanger/ClothesHangerDownIcon1On.png",
            };
            bodyDiv.AddChidren(btnClothesHangerDown);
            LoadEvent_ControlClothesHanger(function,  btnClothesHangerUp, btnClothesHangerDown);
            #endregion


        }

        /// <summary>
        /// 加载音乐功能控制卡片
        /// </summary>
        void MusicFragment()
        {
            #region Music
            var btnPrev = new Button()
            {
                X = Application.GetRealWidth(163),
                Y = Application.GetRealHeight(58),
                Width = Application.GetMinRealAverage(32),
                Height = Application.GetMinRealAverage(32),
                UnSelectedImagePath = "MusicIcon/prev.png",
                SelectedImagePath = "MusicIcon/prevSeletced.png",
            };
            bodyDiv.AddChidren(btnPrev);

            var btnPlay = new Button()
            {
                X = Application.GetRealWidth(233),
                Y = Application.GetRealHeight(58),
                Width = Application.GetMinRealAverage(32),
                Height = Application.GetMinRealAverage(32),
                UnSelectedImagePath = "MusicIcon/pause.png",
                SelectedImagePath = "MusicIcon/play.png",
                //IsSelected = (function as A31MusicModel).A31PlayStatus.status == "play" ,
                Tag = function.spk + "_State_" + function.sid
            };
            bodyDiv.AddChidren(btnPlay);

            var btnNext = new Button()
            {
                X = Application.GetRealWidth(303),
                Y = Application.GetRealHeight(58),
                Width = Application.GetMinRealAverage(32),
                Height = Application.GetMinRealAverage(32),
                UnSelectedImagePath = "MusicIcon/next.png",
                SelectedImagePath = "MusicIcon/nextSelected.png",
            };
            bodyDiv.AddChidren(btnNext);
            LoadEvent_ControlMusic(btnPlay, btnNext, btnPrev, function as A31MusicModel);
            #endregion
        }

        /// <summary>
        /// 加载功能状态信息按钮
        /// </summary>
        void LoadLastStatesButton()
        {
            if(function.spk == SPK.LightSwitch)
            {
                return;
            }
            var btnLastStates = new Button()
            {
                X = Application.GetRealWidth(10 + 32 + 8),
                Y = Application.GetRealHeight(10 + 24 + 18),
                Width = Application.GetRealWidth(200),
                Height = Application.GetRealHeight(18),
                Text = function.lastState,
                TextAlignment = TextAlignment.CenterLeft,
                SelectedTextColor = CSS_Color.PromptingColor1,
                TextColor = 0x00000000,
                TextSize = CSS_FontSize.PromptFontSize_SecondaryLevel,
                Tag = function.spk + "_laststates_" + function.sid,
                IsSelected = function.trait_on_off.curValue.ToString() == "on"
            };
            bodyDiv.AddChidren(btnLastStates);
        }
    }
}