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 PanelGame mUIGame;

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

        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;

        mUIGame.DelPlay = PlayFilm;

        ToukaStageCommonEvent.Instance.StageStartEvent(PlayerData.Instance.CurrentLevel);
        ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.StartGame, Const.AdsEvtID.Level, PlayerData.Instance.CurrentLevel.ToString());
    }

    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 = -1;
        PlaySound();
    }

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

            ToukaUtils.CheckPassLvAndAdEvent(PlayerData.Instance.CurrentLevel);
            ToukaStageCommonEvent.Instance.StageTimeEvent(PlayerData.Instance.CurrentLevel);
            ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.FinishGame, Const.AdsEvtID.Level, PlayerData.Instance.CurrentLevel.ToString());

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

                EndingData tEdData = tLvData.Endings[tEndingID];
                if (tEdData.IsGoodEnding)
                {
                    PlayerData.Instance.UnlockNewLevel();
                    AudioManager.Instance.PlaySound(AudioClipType.Win);

                    ToukaStageCommonEvent.Instance.StageSuccessEvent(PlayerData.Instance.CurrentLevel);
                    ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.GameWin, Const.AdsEvtID.Level, PlayerData.Instance.CurrentLevel.ToString());
                }
                else
                {
                    AudioManager.Instance.PlaySound(AudioClipType.Lose);

                    ToukaStageCommonEvent.Instance.StageFailureEvent(PlayerData.Instance.CurrentLevel);
                    ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.GameLose, Const.AdsEvtID.Level, PlayerData.Instance.CurrentLevel.ToString());
                }
                tUIResult.Init(PlayerData.Instance.CurrentLevelID, tEdData.EndingName, tEdData.IsGoodEnding, PlayerData.Instance.IsNextLevelPassed(), tHasNew, tHasComplete);
            }
            else
            {
                AudioManager.Instance.PlaySound(AudioClipType.Lose);
                tUIResult.Init(PlayerData.Instance.CurrentLevelID, "", false, PlayerData.Instance.IsNextLevelPassed(), false, true);

                ToukaStageCommonEvent.Instance.StageFailureEvent(PlayerData.Instance.CurrentLevel);
                ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.GameLose, Const.AdsEvtID.Level, PlayerData.Instance.CurrentLevel.ToString());
            }

            tUIResult.DelAgain = PlayAgain;
        }
    }

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

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

        mFilm.PlayScene("0");
    }
}