using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameLogic : MonoBehaviour
{
    [SerializeField] Transform mCtnStage;

    private Film mFilm;
    private int[] mSoundOrder;
    private string mEndingStr;
    private int mCurIndex;

    private void Start()
    {
        PanelGame tUIGame = UIManager.Instance.OpenUI<PanelGame>();
        tUIGame.Init();

        mFilm = Instantiate(ResourceManager.Instance.LoadRes<Film>(Const.Path.GetFilm(PlayerData.Instance.CurrentLevelID)));
        mFilm.transform.SetParent(mCtnStage);
        mFilm.transform.localPosition = Vector3.zero;
        mFilm.transform.localScale = Vector3.one;

        tUIGame.DelPlay = PlayFilm;
    }

    private void PlayFilm(int[] pSoundOrder)
    {
        mSoundOrder = pSoundOrder;

        mEndingStr = "";
        for (int i = 0; i < mSoundOrder.Length; i++)
        {
            mEndingStr += (mSoundOrder[i] + 1).ToString();
            if (i < mSoundOrder.Length - 1)
            {
                mEndingStr += "_";
            }
        }

        mFilm.PlayScene(mEndingStr);

        mCurIndex = 0;
        AudioManager.Instance.PlaySound(ResourceManager.Instance.LoadRes<AudioClip>(Const.Path.GetSound(PlayerData.Instance.CurrentLevelID, mSoundOrder[mCurIndex])));
        TimerManager.Instance.Schedule(PlaySound, 1.5f, mSoundOrder.Length);
    }

    private void PlaySound()
    {
        mCurIndex++;
        if (mCurIndex < mSoundOrder.Length)
        {
            AudioManager.Instance.PlaySound(ResourceManager.Instance.LoadRes<AudioClip>(Const.Path.GetSound(PlayerData.Instance.CurrentLevelID, mSoundOrder[mCurIndex])));
        }
        else
        {
            //end
            LevelData tLvData = GameConfig.Instance.GetLevelData(PlayerData.Instance.CurrentLevelID);
            int tEndingID = tLvData.GetMatchingEndingID(mEndingStr.Replace("_",""));
            
            UIManager.Instance.CloseUI<PanelGame>();

            PanelResult tUIResult = UIManager.Instance.OpenUI<PanelResult>();
            if (tEndingID >= 0)
            {
                bool tHasNew = PlayerData.Instance.AddEnding(PlayerData.Instance.CurrentLevelID, tEndingID);

                EndingData tEdData = tLvData.Endings[tEndingID];
                if (tEdData.IsGoodEnding)
                {
                    PlayerData.Instance.UnlockNewLevel();
                }
                tUIResult.Init(PlayerData.Instance.CurrentLevelID, tEdData.EndingName, tEdData.IsGoodEnding, tHasNew, PlayerData.Instance.HasUnlockedAllLevel());
            }
            else
            {
                tUIResult.Init(PlayerData.Instance.CurrentLevelID, "", false, false, PlayerData.Instance.HasUnlockedAllLevel());
            }

            tUIResult.DelAgain = PlayAgain;
        }
    }

    private void PlayAgain()
    {
        UIManager.Instance.CloseUI<PanelResult>();

        PanelGame tUIGame = UIManager.Instance.OpenUI<PanelGame>();
        tUIGame.Init();

        mFilm.PlayScene("0");
    }
}