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

namespace HDL_ON.Stan
{
    /// <summary>
    /// 控制上下左右停止的图像控件
    /// </summary>
    public class DirectionImageControl : NormalFrameLayout
    {
        #region ■ 变量声明___________________________

        /// <summary>
        /// 点击后自动还原状态的控制事件(只有Mid,left,right,up,down)
        /// </summary>
        public Action<DirectionEnum> AutoRecoverControlEvent = null;
        /// <summary>
        /// 点击后不会还原状态的控制事件(只有enable,Mid,left,right,up,down。 enable:手指松开方向键后触发)
        /// </summary>
        public Action<DirectionEnum> NotRecoverControlEvent = null;
        /// <summary>
        /// 能否控制方向
        /// </summary>
        private bool CanDirection = false;
        /// <summary>
        /// 各图片路径 0:可以控制 1:不可以控制 2:上 3:下 4:左 5:右
        /// </summary>
        private List<string> listIconPath = new List<string>();

        #endregion

        #region ■ 初始化_____________________________

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="i_enablePath">可以控制的背景图</param>
        /// <param name="i_disablePath">不可以控制的背景图</param>
        /// <param name="i_upPath">点击向上的背景图</param>
        /// <param name="i_downPath">点击向下的背景图</param>
        /// <param name="i_leftPath">点击向左的背景图</param>
        /// <param name="i_rightPath">点击向右的背景图</param>
        public void InitControl(string i_enablePath, string i_disablePath, string i_upPath, string i_downPath,
            string i_leftPath, string i_rightPath)
        {
            this.listIconPath.Add(i_enablePath);
            this.listIconPath.Add(i_disablePath);
            this.listIconPath.Add(i_upPath);
            this.listIconPath.Add(i_downPath);
            this.listIconPath.Add(i_leftPath);
            this.listIconPath.Add(i_rightPath);

            this.BackgroundImagePath = i_disablePath;
            this.CanDirection = false;
            //初始化按钮
            this.IntiButtion();
        }

        /// <summary>
        /// 初始化按钮
        /// </summary>
        private void IntiButtion()
        {
            //正中间的按钮
            var btnMid = new PicViewControl(70, 70);
            btnMid.Gravity = Gravity.Center;
            this.AddChidren(btnMid);
            btnMid.ButtonClickEvent += (sender, e) =>
            {
                //判断能否点击
                if (this.CanClick == false) { return; }
                //开启等待线程(不允许狂点)
                this.StartWaitThread();

                this.AutoRecoverControlEvent?.Invoke(DirectionEnum.Mid);
                this.NotRecoverControlEvent?.Invoke(DirectionEnum.Mid);
            };

            //上按钮
            var btnUp = new PicViewControl(52, 52);
            this.AddChidren(btnUp);
            btnUp.X = (this.Width - btnUp.Width) / 2;
            btnUp.ButtonClickEvent += (sender, e) =>
            {
                //方向点击
                this.DirectionClickEvent(DirectionEnum.Up);
            };
            btnUp.ButtonDownClickEvent += (sender, e) =>
            {
                //按键按下
                this.DirectionDownClickEvent(DirectionEnum.Up);
            };

            //下按钮
            var btnDown = new PicViewControl(btnUp.Width, btnUp.Height, false);
            btnDown.X = btnUp.X;
            this.AddChidren(btnDown);
            btnDown.Y = this.Height - btnDown.Height;
            btnDown.ButtonClickEvent += (sender, e) =>
            {
                //方向点击
                this.DirectionClickEvent(DirectionEnum.Down);
            };
            btnDown.ButtonDownClickEvent += (sender, e) =>
            {
                //按键按下
                this.DirectionDownClickEvent(DirectionEnum.Down);
            };

            //左按钮
            var btnLeft = new PicViewControl(btnUp.Width, btnUp.Height, false);
            this.AddChidren(btnLeft);
            btnLeft.Y = (this.Height - btnLeft.Height) / 2;
            btnLeft.ButtonClickEvent += (sender, e) =>
            {
                //方向点击
                this.DirectionClickEvent(DirectionEnum.Left);
            };
            btnLeft.ButtonDownClickEvent += (sender, e) =>
            {
                //按键按下
                this.DirectionDownClickEvent(DirectionEnum.Left);
            };

            //右按钮
            var btnRight = new PicViewControl(btnUp.Width, btnUp.Height, false);
            btnRight.Y = btnLeft.Y;
            this.AddChidren(btnRight);
            btnRight.X = this.Width - btnRight.Width;
            btnRight.ButtonClickEvent += (sender, e) =>
            {
                //方向点击
                this.DirectionClickEvent(DirectionEnum.Right);
            };
            btnRight.ButtonDownClickEvent += (sender, e) =>
            {
                //按键按下
                this.DirectionDownClickEvent(DirectionEnum.Right);
            };
        }

        #endregion

        #region ■ 外部设置图片_______________________

        /// <summary>
        /// 设置图片显示
        /// </summary>
        /// <param name="direction">方向枚举(不支持Mid)</param>
        public void SetDirectionImage(DirectionEnum direction)
        {
            if (direction == DirectionEnum.Mid)
            {
                //不支持Mid
                return;
            }
            if (direction == DirectionEnum.Disable)
            {
                //不能控制方向
                this.CanDirection = false;
            }
            else
            {
                //能够控制方向
                this.CanDirection = true;
            }

            string iconPath = this.listIconPath[((int)direction) - 1];
            if (this.BackgroundImagePath != iconPath)
            {
                this.BackgroundImagePath = iconPath;
            }
        }

        #endregion

        #region ■ 方向点击___________________________

        /// <summary>
        /// 方向点击
        /// </summary>
        /// <param name="direction"></param>
        private void DirectionClickEvent(DirectionEnum direction)
        {
            //判断能否点击
            if (this.CanClick == false || this.CanDirection == false)
            {
                return;
            }

            //如果使用特效的话
            if (this.AutoRecoverControlEvent != null)
            {
                //切换图片
                this.SetDirectionImage(direction);
                //开启等待线程(不允许狂点)
                this.StartWaitThread();

                this.AutoRecoverControlEvent?.Invoke(direction);
            }
            else
            {
                //切换图片
                this.SetDirectionImage(DirectionEnum.Enable);
                this.NotRecoverControlEvent?.Invoke(DirectionEnum.Enable);
            }
        }

        /// <summary>
        /// 方向按键按下事件
        /// </summary>
        /// <param name="direction"></param>
        private void DirectionDownClickEvent(DirectionEnum direction)
        {
            //如果不使用特效的话
            if (this.CanClick == false || this.CanDirection == false || this.NotRecoverControlEvent == null)
            {
                return;
            }
            //切换图片
            this.SetDirectionImage(direction);
            this.NotRecoverControlEvent?.Invoke(direction);
        }

        #endregion

        #region ■ 一般方法___________________________

        /// <summary>
        /// 开启等待线程(不允许狂点)
        /// </summary>
        private void StartWaitThread()
        {
            this.CanClick = false;
            HdlThreadLogic.Current.RunThread(() =>
            {
                System.Threading.Thread.Sleep(300);
                this.CanClick = true;

                //如果不使用特效的话
                if (this.NotRecoverControlEvent != null)
                {
                    return;
                }
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //图片还原
                    if (this.BackgroundImagePath != this.listIconPath[0])
                    {
                        this.BackgroundImagePath = this.listIconPath[0];
                    }

                }, ShowErrorMode.NO);

            }, ShowErrorMode.NO);
        }

        #endregion
    }
}