using System;
using UnityEngine;
using UnityEngine.UI;

namespace WS
{
    public class GuideControllerUpdataeSystem : UpdateSystem<GuideController>
    {
        public override void Update(GuideController self)
        {
            if (Input.GetKeyDown(KeyCode.B))
            {
                self.OnSkipBtnClick();
            }
        }
    }

    public class GuideController : Controller<GuideView, GuideModel>
    {
        /// <summary>音频实体</summary>
        public VoiceEntity voice;

        /// <summary>流程管理器</summary>
        public ProcessManager processManager;

        /// <summary>完成回调</summary>
        public Action OnTaskComplete;

        protected override void BindingViewModel()
        {
            _Model.Bind(_View.SkipBtn, OnSkipBtnClick);
            _Model.Bind(_View.CloseGuideToggle, _Model.CloseGuide, OnCloseGuideBtnClick);
            _Model.Bind(_View.ExitBtn, OnExitBtnClick);
        }

        public override void OpenView()
        {
            base.OpenView();
            processManager = GameManager.Instance.ProcessManager;
            processManager.OnEndProcessCallback += OnProcessComplete;
            SetEndBtnStatus(false);
        }

        /// <summary> 跳过引导 </summary>
        public void OnSkipBtnClick()
        {
            _Model.CurrentTaskId = processManager.GetCurrentModule().VTaskId;

            processManager.EndProcess(true);
            OnProcessComplete();
            CloseView();
        }

        /// <summary>不再弹出引导 </summary>
        public void OnCloseGuideBtnClick(bool isOn)
        {
            GameManager.Instance.Data.IsGuide = isOn;
        }

        /// <summary>确认并关闭</summary>
        public void OnExitBtnClick()
        {
            OnTaskComplete?.Invoke();
            _View.UIGameObject.SetActive(false);
            //CloseView();
        }

        public void OnProcessComplete()
        {
            GameManager.Instance.Data.IsGuide = false;
            EndTeachController endTeachController =
                FacadeComponent.Instance.CreateController<EndTeachController>();
            endTeachController.OpenView();
            if (_Model.CurrentTaskId == 1)
            {
                endTeachController.SetViewMesg("已跳过新手引导");
            }
            else if (_Model.CurrentTaskId == processManager.GetVTaskList().Count)
            {
                endTeachController.SetViewMesg("已完成新手引导，现在返回大厅");
            }
            else
            {
                endTeachController.SetViewMesg("已跳过新手引导，现在返回大厅");
            }
            CloseView();
        }

        /// <summary>
        /// 设置引导页面
        /// </summary>
        /// <param name="textureName">提示图片</param>
        /// <param name="audioName">语音名称</param>
        /// <param name="pos">位置</param>
        /// <param name="quaternion">旋转</param>
        public void SetView(
            string textureName,
            string audioName,
            Vector3 pos,
            Quaternion quaternion
        )
        {
            Texture2D texture = Resources.Load<Texture2D>("Texture/" + textureName);
            Sprite sprite = Sprite.Create(
                texture,
                new Rect(0, 0, texture.width, texture.height),
                Vector2.zero
            );
            _View.HintImage.sprite = sprite;

            if (voice != null)
            {
                if (!voice.IsDisposed)
                    VoiceManager.Instance.Stop(voice);
            }
            voice = VoiceManager.Instance.Play(VoiceType.Voice, audioName);
            _View.SetPosition(pos, quaternion);
        }

        /// <summary> 注册关闭回调 </summary>
        public void AddExitBtnAddListener(Action action)
        {
            OnTaskComplete = action;
        }

        /// <summary>按钮状态 </summary>
        public void SetEndBtnStatus(bool isActive)
        {
            _View.SkipBtn.gameObject.SetActive(!isActive);
            _View.CloseGuideToggle.gameObject.SetActive(isActive);
            _View.ExitBtn.gameObject.SetActive(isActive);
        }

        public override void Dispose()
        {
            if (voice != null)
            {
                if (!voice.IsDisposed)
                    VoiceManager.Instance.Stop(voice);
            }

            voice = null;
            processManager.OnEndProcessCallback -= OnProcessComplete;
            base.Dispose();
        }
    }

    public class GuideView : View
    {
        public Image HintImage;
        public Button SkipBtn;
        public Toggle CloseGuideToggle;
        public Button ExitBtn;

        public override void LoadInit()
        {
            ShowModeType = ViewShowModeType.World;
            ReferenceCollector rc = UIGameObject.GetComponent<ReferenceCollector>();
            HintImage = rc.GetComponent<Image>("HintImage");
            SkipBtn = rc.GetComponent<Button>("SkipBtn");
            CloseGuideToggle = rc.GetComponent<Toggle>("CloseGuideToggle");
            ExitBtn = rc.GetComponent<Button>("ExitBtn");
        }
    }

    public class GuideModel : Model
    {
        /// <summary>关闭引导</summary>
        public BindableProperty<bool> CloseGuide;

        /// <summary> 当前任务ID </summary>
        public int CurrentTaskId;

        public override void InitProperty()
        {
            CloseGuide = CreateProperty<bool>();
        }
    }
}
