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

public class PanelLevel : BasePanel
{
    [SerializeField] Button mBtnBack;
    [SerializeField] Button mBtnBackToMain;
    [SerializeField] LevelPage mLvPage;
    [SerializeField] RectTransform mRctChicken;
    [SerializeField] RectTransform mRctScream;
    [SerializeField] GameObject mGobOpMask;

    private bool mOpenInLevel = false;

    private void Awake()
    {
        UIUtils.BindBtn(mBtnBack, Close);
        UIUtils.BindBtn(mBtnBackToMain, OnClickBackToMain, AudioClipType.Click_Normal);

        mLvPage.DelClickLevel = OnClickLevel;
    }

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

        mLvPage.LoadData();
        mLvPage.PageTo((PlayerData.Instance.CurrentLevel - 1) / mLvPage.CountPerPage, false);

        LevelCell tCell = mLvPage.GetCell(PlayerData.Instance.CurrentLevel - 1);
        mRctChicken.position = tCell.CurrentSignTrans.position;
        mRctChicken.transform.SetParent(tCell.CurrentSignTrans);

        mRctScream.gameObject.SetActive(false);
        mGobOpMask.SetActive(false);

        ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.PanelLevelShowed);
    }

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

        AudioManager.Instance.PlaySound(AudioClipType.UIClose);
        ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.PanelLevelBack);
    }

    public void Init(bool pOpenInLevel)
    {
        mOpenInLevel = pOpenInLevel;
        mBtnBackToMain.gameObject.SetActive(pOpenInLevel);
    }

    private void OnClickLevel(int pLevelIndex)
    {
        if (mOpenInLevel && PlayerData.Instance.CurrentLevel == (pLevelIndex + 1))
        {
            Close();
        }
        else
        {
            mGobOpMask.SetActive(true);

            LevelCell tCell = mLvPage.GetCell(pLevelIndex);
            mRctChicken.SetParent(transform);
            mRctChicken.DOMove(tCell.CurrentSignTrans.position, 0.5f).SetEase(Ease.Linear);

            mRctScream.gameObject.SetActive(true);
            mRctScream.position = tCell.transform.position;
            AudioManager.Instance.PlaySound(AudioClipType.StartGame);

            PlayerData.Instance.CurrentLevel = pLevelIndex + 1;

            TimerManager.Instance.Schedule(GameManager.Instance.LoadGame, 1f);
        }
    }

    private void OnClickBackToMain()
    {
        ToukaSDKManager.Instance.LogEventByUmeng(Const.AdsEvent.PanelLevelHome);

        GameManager.Instance.LoadMain();
    }
}