using UnityEngine;
using UnityEngine.UI;
using static WS.ProcessManager;

namespace WS
{
    public class EndTeachController : Controller<EndTeachView, EndTeachModel>
    {
        bool _returnMain;

        protected override void BindingViewModel()
        {
            _Model.Bind(_View.ExitBtn, OnExitBtnClick);
            _Model.Bind(_View.ReturnBtn, OnReturnBtnClick);
            _Model.Bind(_View.Message, _Model.Message);
        }

        public override void OpenView()
        {
            base.OpenView();

            var dir = Vector3.ProjectOnPlane(Player.Inst.hmdTransform.forward, Vector3.up);
            var pos = Player.Inst.hmdTransform.position + dir.normalized * 1f;
            var rot = Quaternion.LookRotation(dir.normalized);
            _View.SetPosition(pos / 0.001f, rot);
        }

        private void OnExitBtnClick()
        {
            ProcessManager processMgr = GameManager.Instance.ProcessManager;
            if (processMgr.GetProcessStatus() != StateType.NotStarted)
            {
                WSTaskRecord wSTaskRecord = processMgr.GetVTaskRecord(
                    processMgr.GetCurrentModule().VTaskId
                );
                wSTaskRecord.IsEnd = true;
                FacadeComponent.Instance.GetController<ProgressPopController>().CloseView();
                processMgr.EndProcess(true);
                SceneDataSaveComponent.Instance.RestAllScene();
            }
            if (_returnMain)
            {
                if (SceneComponent.Instance.ThisScene != SceneName.发电机层.ToString())
                {
                    SceneComponent.Instance.LoadScene(
                        SceneName.发电机层.ToString(),
                        () =>
                        {
                            GameObject startPoint = GameObject.Find("StartPoint");
                            Player.Inst.transform.position = startPoint.transform.position;
                            Player.Inst.transform.rotation = startPoint.transform.rotation;
                            ChoosePopController choosePop =
                                FacadeComponent.Instance.CreateController<ChoosePopController>();
                            choosePop.OpenView();
                        }
                    );
                }
                FacadeComponent.Instance.GetController<ChoosePopController>()?.SetBtnStatus(true);
            }
            else
            {
                //ScorePopController scorePopController =
                //    FacadeComponent.Instance.CreateController<ScorePopController>();
                //scorePopController.OpenView();
            }
            CloseView();
        }

        private void OnReturnBtnClick()
        {
            ProgressPopController progressPop =
                FacadeComponent.Instance.GetController<ProgressPopController>();
            if (progressPop != null)
            {
                progressPop.isForbidden = false;
                progressPop.ViewStatus(false);
            }
            CloseView();
        }

        /// <summary>
        /// 设置页面信息、按钮状态
        /// </summary>
        /// <param name="mesg">信息</param>
        /// <param name="haveCancel">取消按钮是否显示</param>
        /// <param name="returnMain">退出按钮是否直接返回主场景</param>
        public void SetViewMesg(string mesg, bool haveCancel = false, bool returnMain = true)
        {
            _returnMain = returnMain;
            _Model.Message.Value = mesg;
            _View.ReturnBtn.gameObject.SetActive(haveCancel);
        }

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

    public class EndTeachView : View
    {
        public Button ExitBtn;
        public Button CloseBtn;
        public Button ReturnBtn;
        public Text Message;

        public override void LoadInit()
        {
            ShowModeType = ViewShowModeType.World;
            ReferenceCollector rc = UIGameObject.GetComponent<ReferenceCollector>();
            ExitBtn = rc.GetComponent<Button>("ExitBtn");
            CloseBtn = rc.GetComponent<Button>("CloseBtn");
            ReturnBtn = rc.GetComponent<Button>("ReturnBtn");
            Message = rc.GetComponent<Text>("Message");
        }
    }

    public class EndTeachModel : Model
    {
        public BindableProperty<string> Message;

        public override void InitProperty()
        {
            Message = CreateProperty<string>();
        }
    }
}
