using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XWFramework.Tools;
using System;
namespace Level1
{
    [DefaultExecutionOrder(-10)]
    public class GameManager : MonoSingleton<GameManager>
    {
        public GameObject btn;
        public delegate void BaseDelegate();

        [NonSerialized]
        public UI ui;
        [NonSerialized]
        public CameraManager cameraManager;
        [NonSerialized]
        public DataManager dataManager;
        [NonSerialized]
        public AudioManager audioManager;

        
        /// <summary>
        /// 玩家移动,NPC移动,车子移动
        /// </summary>
        public static BaseDelegate StartGame;
        public Dictionary<int, BaseDelegate> questionCallBack = new Dictionary<int, BaseDelegate>();

        public PlayerController player;
        [NonSerialized]
        public int questionIndex = 0;
        public Transform bjz;
        public GameObject sjz;
        public Animator animator;
        public GameObject[] needTransferObj;

        protected override void Awake()
        {
            base.Awake();
            ui = GetComponentInChildren<UI>();
            cameraManager = GetComponentInChildren<CameraManager>();
            dataManager = GetComponentInChildren<DataManager>();
            audioManager = GetComponentInChildren<AudioManager>();
            InitQuestionCallBack();
        }

        void InitQuestionCallBack() {
            questionCallBack.Add(0, Choose);
        }

        #region 进程
        private void Start()
        {
            btn.SetActive(true);
        }

        public void ClickStart() {
            StartGame();
        }

        public void PlayerArrive() {
            ui.Show(UIType.Toggle);
        }
        #endregion

        #region QuestionPanel   
        public Question GetQuestion() {
            return dataManager.questionData[questionIndex];
        }

        public void QuestionCallBack() {
            questionCallBack[questionIndex]();
        }
        #endregion

        #region 答题回调

        void Choose() {
            player.gameObject.SetActive(false);
            sjz.SetActive(true);
            animator.SetTrigger("Yaotou");
            ui.Show(UIType.Info);
            StartCoroutine(Wait(2f,()=> {
                ui.Show(UIType.Safe);
            }));
        }
        #endregion
        /// <summary>
        /// 调整maskPanel
        /// 转移物体
        /// 调整摄像机视角
        /// </summary>
        public void Transfer() {
            ui.Show(UIType.Mask);
            ui.uiDicts[UIType.Mask].GetComponent<MaskPanel>().OpenMask(0.5f);
            StartCoroutine(Wait(1f, () => {
                cameraManager.SetLookAt(bjz).ChangePosition(0.75f, 56.27f, 10);
                foreach (var obj in needTransferObj)
                {
                    obj.transform.position += new Vector3(-12, 0.185f, 0);
                }
                sjz.SetActive(true);
                ui.ShowErrorTip("恭喜通关",60);
                audioManager.PlayEnd();
                ui.ShowTip(dataManager.dialogues[2]);
            }));

            StartCoroutine(Wait(2.5f, () =>
            {
                EduCoderTool.WebConnecter.Singleton.SendResultToWeb(true);
            }));
        }
        
        IEnumerator Wait(float t, Action action)
        {
            yield return new WaitForSeconds(t);
            action();
        }
    }
}