using System;
using UnityEngine;
using UnityEngine.UI;

namespace WS
{
    [ObjectSystem]
    public class ChoosePopControllerUpdateSystem : UpdateSystem<ChoosePopController>
    {
        public override void Update(ChoosePopController self)
        {
            self.Update();
        }
    }

    public class ChoosePopController : Controller<ChoosePopView, ChoosePopModel>
    {
        /// <summary> 完成引导回调 </summary>
        private Action OnGuideComplete;
        private ProcessManager processManager;

        protected override void BindingViewModel()
        {
            _Model.Bind(_View.LearnBtn, OnLearnBtnClick);
            _Model.Bind(_View.PracticeBtn, OnPracticeBtnClick);
            _Model.Bind(_View.ExamineBtn, () => { });
            _Model.Bind(
                _View.SafetyBtn,
                () =>
                {
                    SceneComponent.Instance.LoadScene(
                        SceneName.安全教育.ToString(),
                        () => {
                            // GameObject.Find("Interaction").GetComponent<SafeStudyMono>().Launch();
                        }
                    );
                    CloseView();
                }
            );
            _Model.Bind(_View.WanderBtn, OnWanderBtnClick);
            _Model.Bind(_View.ExamineBtn, OnExaminBtnClick);
            _Model.Bind(
                _View.ReturnBtn,
                () =>
                {
                    Application.Quit();
                }
            );
            _Model.Bind(_View.HelpBtn, OnHelpBtnClick);
        }

        public override void OpenView()
        {
            base.OpenView();
            processManager = GameManager.Instance.ProcessManager;
            Vector3 pos = new Vector3(-304, 1500, -105658);
            Quaternion rot = Quaternion.Euler(0, 180, 0);
            _View.SetPosition(pos, rot);
        }

        public void Update()
        {
            _View.Frame.gameObject.SetActive(GameManager.Instance.Data.IsGuide);
        }

        /// <summary> 进入教学 </summary>
        public void OnLearnBtnClick()
        {
            if (!GameManager.Instance.Data.IsGuide)
            {
                GameManager.Instance.Data.Pattern = PatternType.教学;
                SceneComponent.Instance.LoadScene(
                    SceneName.水轮机层.ToString(),
                    () =>
                    {
                        processManager.StartProcess("AllProcess");
                        ProgressPopController progressPop =
                            FacadeComponent.Instance.CreateController<ProgressPopController>();
                        progressPop.OpenView();
                        GameObject startPoint = GameObject.Find("Process1_Task1");
                        Player.Inst.transform.position = startPoint.transform.position;
                        Player.Inst.transform.rotation = startPoint.transform.rotation;
                        CloseView();
                    }
                );
                //processManager.StartProcess("Process4");
                //ProgressPopController progressPop = FacadeComponent.Instance.CreateController<ProgressPopController>();
                //progressPop.OpenView();
                //CloseView();
                //SceneComponent.Instance.LoadScene(SceneName.地下副厂房10KV配电室400v照明配电室.ToString(), () =>
                //{
                //    processManager.StartProcess("Process4");
                //    ProgressPopController progressPop = FacadeComponent.Instance.CreateController<ProgressPopController>();
                //    progressPop.OpenView();
                //    GameObject startPoint = GameObject.Find("StartPoint");
                //    Player.instance.transform.position = startPoint.transform.position;
                //    Player.instance.transform.rotation = startPoint.transform.rotation;
                //    CloseView();
                //});
            }
            else
            {
                GameManager.Instance.Data.Pattern = PatternType.引导;
                SceneComponent.Instance.LoadScene(
                    SceneName.安装间下400V配电室.ToString(),
                    () =>
                    {
                        Player.Inst.transform.position = new Vector3(1.9f, 0, -9);
                        Player.Inst.transform.rotation = Quaternion.Euler(new Vector3(0, -90, 0));
                        OnGuideComplete?.Invoke();
                        //CloseView();
                    }
                );
            }
            CloseView();
        }

        /// <summary> 练习 </summary>
        private void OnPracticeBtnClick()
        {
            if (!GameManager.Instance.Data.IsGuide)
            {
                GameManager.Instance.Data.Pattern = PatternType.练习;

                SceneComponent.Instance.LoadScene(
                    SceneName.水轮机层.ToString(),
                    () =>
                    {
                        processManager.StartProcess("AllProcess");
                        ProgressPopController progressPop =
                            FacadeComponent.Instance.CreateController<ProgressPopController>();
                        progressPop.OpenView();
                        GameObject startPoint = GameObject.Find("Process1_Task1");
                        Player.Inst.transform.position = startPoint.transform.position;
                        Player.Inst.transform.rotation = startPoint.transform.rotation;
                        //CloseView();1
                    }
                );
            }
            CloseView();
        }

        private void OnExaminBtnClick()
        {
            GameManager.Instance.Data.Pattern = PatternType.考核;
            SceneComponent.Instance.LoadScene(
                SceneName.水轮机层.ToString(),
                () =>
                {
                    processManager.StartProcess("AllProcess");
                    ProgressPopController progressPop =
                        FacadeComponent.Instance.CreateController<ProgressPopController>();
                    progressPop.OpenView();
                    GameObject startPoint = GameObject.Find("Process1_Task1");
                    Player.Inst.transform.position = startPoint.transform.position;
                    Player.Inst.transform.rotation = startPoint.transform.rotation;
                    //CloseView();
                }
            );
            CloseView();
        }

        private void OnHelpBtnClick()
        {
            //HelpViewController help =
            //    FacadeComponent.Instance.CreateController<HelpViewController>();
            //help.OpenView();

            //help.SetView(new Vector3(0, 1500, -105658), Quaternion.Euler(new Vector3(0, 180, 0)));
        }

        private void OnWanderBtnClick()
        {
            GameManager.Instance.Data.Pattern = PatternType.漫游;
            MapController map = FacadeComponent.Instance.CreateController<MapController>();
            map.OpenView();
            CloseView();
        }

        /// <summary>
        /// 设置引导模式下按钮的交互状态
        /// </summary>
        /// <param name="action">完成引导操作的回调</param>
        public void SetGuide(Action action)
        {
            OnGuideComplete = action;
            SetBtnStatus(false);
        }

        /// <summary>重置页面上按钮的交互状态 </summary>
        public void SetBtnStatus(bool isInteract)
        {
            _View.PracticeBtn.interactable = isInteract;
            _View.DisabledPractice.enabled = !isInteract;

            _View.ExamineBtn.interactable = isInteract;
            _View.DisabledExamine.enabled = !isInteract;

            _View.SafetyBtn.interactable = isInteract;
            _View.DisabledSafety.enabled = !isInteract;

            _View.WanderBtn.interactable = isInteract;
            _View.DisabledWander.enabled = !isInteract;
        }
    }

    public class ChoosePopView : View
    {
        public Button LearnBtn;
        public Button PracticeBtn;
        public Button ExamineBtn;
        public Button ReturnBtn;
        public Button WanderBtn;
        public Button SafetyBtn;
        public Button HelpBtn;
        public Image DisabledWander;
        public Image DisabledSafety;
        public Image DisabledExamine;
        public Image DisabledPractice;
        public Image Frame;

        public override void LoadInit()
        {
            ShowModeType = ViewShowModeType.World;
            ReferenceCollector rc = UIGameObject.GetComponent<ReferenceCollector>();
            LearnBtn = rc.GetComponent<Button>("LearnBtn");
            PracticeBtn = rc.GetComponent<Button>("PracticeBtn");
            ExamineBtn = rc.GetComponent<Button>("ExamineBtn");
            ReturnBtn = rc.GetComponent<Button>("ReturnBtn");
            WanderBtn = rc.GetComponent<Button>("WanderBtn");
            SafetyBtn = rc.GetComponent<Button>("SafetyBtn");
            HelpBtn = rc.GetComponent<Button>("HelpBtn");
            DisabledWander = rc.GetComponent<Image>("DisabledWander");
            DisabledSafety = rc.GetComponent<Image>("DisabledSafety");
            DisabledExamine = rc.GetComponent<Image>("DisabledExamine");
            DisabledPractice = rc.GetComponent<Image>("DisabledPractice");
            Frame = rc.GetComponent<Image>("Frame");
        }

        public override void Dispose()
        {
            base.Dispose();
        }
    }

    public class ChoosePopModel : Model
    {
        /// <summary> 完成引导回调 </summary>
        public Action OnGuideComplete;
        public ProcessManager processManager;

        public override void InitProperty() { }
    }
}
