using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class PanelGame : BasePanel
{
    public Action<int[]> DelPlay;

    [SerializeField] LevelOpening mLvOpening;

    [SerializeField] Text mTxtTitle;
    [SerializeField] Button mBtnMoreGame;
    [SerializeField] Button mBtnLevel;
    [SerializeField] Button mBtnMenu;
    [SerializeField] ButtonEndings mBtnEndings;

    [SerializeField] ButtonSlot[] mBtnSlots;
    [SerializeField] GameObject[] mGobArrows;
    [SerializeField] ButtonSound[] mBtnSounds;

    [SerializeField] GameObject mGobChooseTip;
    [SerializeField] GameObject mGobTapTip;
    [SerializeField] GameObject mGobPlay;
    [SerializeField] Button mBtnPlay;

    [SerializeField] GameObject mGobPlayProgress;
    [SerializeField] Image mImgPlayProgress;

    [SerializeField] Animator mAtrAction;
    [SerializeField] GameObject mGobOpMask;

    private int mTotalSlot;
    private int mCurSlotIndex;
    private int[] mSoundOrder;

    private PanelGameAni mAni;

    private void Awake()
    {
        UIUtils.BindBtn(mBtnMoreGame, OnClickMoreGame);
        UIUtils.BindBtn(mBtnLevel, OnClickLevel, AudioClipType.Click_Normal);
        UIUtils.BindBtn(mBtnMenu, OnClickMenu, AudioClipType.Click_Tip);
        UIUtils.BindBtn(mBtnPlay, OnClickPlay, AudioClipType.Click_Play);

        mBtnEndings.DelClick = OnClickEndings;

        mAni = GetComponent<PanelGameAni>();
    }

    public void Init(bool pWithOpening = false)
    {
        if (pWithOpening)
        {
            mLvOpening.Enter(PlayerData.Instance.CurrentLevel, ShowStage);
        }
        else
        {
            mLvOpening.gameObject.SetActive(false);
            ShowStage();
        }
    }

    public void RiseSlot(int pIndex)
    {
        mBtnSlots[pIndex].RiseUp();
    }

    private void ShowStage()
    {
        LevelData tLvData = GameConfig.Instance.GetLevelData(PlayerData.Instance.CurrentLevelID);

        mTxtTitle.text = tLvData.LevelName;
        mBtnEndings.Init(PlayerData.Instance.CurrentLevelID, PlayerData.Instance.EndingCountOfLevel(PlayerData.Instance.CurrentLevelID), tLvData.Endings.Count);

        mTotalSlot = tLvData.SlotCount;
        mCurSlotIndex = 0;
        mSoundOrder = new int[mTotalSlot];
        for (int i = 0; i < mSoundOrder.Length; i++)
        {
            mSoundOrder[i] = -1;
        }

        for (int i = 0; i < mBtnSlots.Length; i++)
        {
            mBtnSlots[i].gameObject.SetActive(i < mTotalSlot);
            mBtnSlots[i].Init(i);
            mBtnSlots[i].ShowCurrent(i == mCurSlotIndex);
            mBtnSlots[i].DelClick = OnClickSlot;
        }

        for (int i = 0; i < mGobArrows.Length; i++)
        {
            mGobArrows[i].SetActive(i < mTotalSlot - 1);
        }

        for (int i = 0; i < mBtnSounds.Length; i++)
        {
            mBtnSounds[i].Init(i);
            mBtnSounds[i].SetSoundPic(ResourceManager.Instance.LoadRes<Sprite>(Const.Path.GetSoundPic(PlayerData.Instance.CurrentLevelID, i)));
            mBtnSounds[i].DelClick = OnClickSound;
        }

        mGobChooseTip.SetActive(false);

        mGobTapTip.SetActive(false);
        mGobPlay.SetActive(false);
        mGobPlayProgress.SetActive(false);
        mAtrAction.gameObject.SetActive(false);
        mGobOpMask.SetActive(false);

        mAni.Init();
        TimerManager.Instance.Schedule(() => mGobChooseTip.SetActive(true), 0.5f);
    }

    private void OnClickMoreGame()
    {
        ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.ClickMoreGame, Const.AdsEvtID.Level, PlayerData.Instance.CurrentLevel.ToString());

        ToukaUtils.OpenMoreGames();
    }

    private void OnClickLevel()
    {
        ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.ClickLevel, Const.AdsEvtID.Level, PlayerData.Instance.CurrentLevel.ToString());

        PanelLevel tUILevel = UIManager.Instance.OpenUI<PanelLevel>();
        tUILevel.Init(true);

        ToukaSDKManager.Instance.PlayInterstitialAd(Const.AdsEvtValue.GamingSelect);
    }

    private void OnClickMenu()
    {
        UIManager.Instance.OpenUI<PanelSettings>();
    }

    private void OnClickEndings()
    {
        PanelEndings tUIEndings = UIManager.Instance.OpenUI<PanelEndings>();
        tUIEndings.Init(PlayerData.Instance.CurrentLevelID);
    }

    private void OnClickPlay()
    {
        mGobTapTip.SetActive(false);
        mGobPlay.SetActive(false);
        mGobOpMask.SetActive(true);
        for (int i = 0; i < mBtnSlots.Length; i++)
        {
            mBtnSlots[i].ShowCurrent(false);
        }

        mAni.ReadyPlay(0.5f);
        TimerManager.Instance.Schedule(Action, 0.5f);
    }

    private void Action()
    {
        mAtrAction.gameObject.SetActive(true);
        mAtrAction.Play("Action");

        AudioManager.Instance.PlaySound(AudioClipType.Click_Play);
        TimerManager.Instance.Schedule(PlayActionSound, 1f);
    }

    private void PlayActionSound()
    {
        AudioManager.Instance.PlaySound(AudioClipType.Action);
        TimerManager.Instance.Schedule(StartPlay, 1f);
    }

    private void StartPlay()
    {
        DelPlay?.Invoke(mSoundOrder);

        mGobPlayProgress.SetActive(true);
        mImgPlayProgress.fillAmount = 0;
        mImgPlayProgress.DOFillAmount(1, mSoundOrder.Length * 1.5f).SetEase(Ease.Linear);
    }

    private void OnClickSlot(int pIndex)
    {
        mCurSlotIndex = pIndex;
        for (int i = 0; i < mBtnSlots.Length; i++)
        {
            mBtnSlots[i].ShowCurrent(i == mCurSlotIndex);
        }
    }

    private void OnClickSound(int pIndex)
    {
        mSoundOrder[mCurSlotIndex] = pIndex;
        mBtnSlots[mCurSlotIndex].SetSoundPic(ResourceManager.Instance.LoadRes<Sprite>(Const.Path.GetSoundPic(PlayerData.Instance.CurrentLevelID, pIndex)));
        if (mCurSlotIndex < mTotalSlot - 1)
        {
            OnClickSlot(mCurSlotIndex + 1);
        }

        AudioManager.Instance.PlaySound(ResourceManager.Instance.LoadRes<AudioClip>(Const.Path.GetSound(PlayerData.Instance.CurrentLevelID, pIndex)));

        CheckOrder();
    }

    private void CheckOrder()
    {
        bool tAllSet = true;
        for (int i = 0; i < mSoundOrder.Length; i++)
        {
            if (mSoundOrder[i] < 0)
            {
                tAllSet = false;
                break;
            }
        }

        mGobChooseTip.SetActive(!tAllSet);
        mGobTapTip.SetActive(tAllSet);
        mGobPlay.SetActive(tAllSet);
    }
}