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

public class LevelOpening : MonoBehaviour
{
    [SerializeField] RectTransform mRctTopFrame;
    [SerializeField] RectTransform mRctBottomFrame;

    [SerializeField] RectTransform mRctTitleBg;

    [SerializeField] CanvasGroup mCvsBg;
    [SerializeField] CanvasGroup mCvsContent;

    [SerializeField] Image mImgBg;
    [SerializeField] Text mTxtTitle;
    [SerializeField] Image mImgLogo;
    [SerializeField] Image mImgIcon;

    private Action mDelExit;

    public void Enter(int pLevel, Action pDelExit)
    {
        gameObject.SetActive(true);
        mDelExit = pDelExit;
        int tLevelID = GameConfig.Instance.LevelSort[pLevel - 1];

        LevelData tLvData = GameConfig.Instance.GetLevelData(tLevelID);
        if (ColorUtility.TryParseHtmlString(tLvData.LevelColorCode, out Color tLevelColor))
        {
            mImgBg.color = tLevelColor;
        }

        mTxtTitle.text = "关卡 " + pLevel;
        mImgLogo.sprite = ResourceManager.Instance.LoadRes<Sprite>(Const.Path.GetLevelLogo(tLevelID));
        mImgIcon.sprite = ResourceManager.Instance.LoadRes<Sprite>(Const.Path.GetLevelIconSmall(tLevelID));

        //animation
        mRctTopFrame.anchoredPosition = new Vector2(0, 850);
        mRctBottomFrame.anchoredPosition = new Vector2(0, -760);

        mCvsBg.alpha = 1;
        mCvsContent.alpha = 1;
        mRctTitleBg.localScale = new Vector3(1, 0, 1);
        mImgLogo.rectTransform.anchoredPosition = new Vector2(600, 0);
        mImgIcon.rectTransform.localScale = Vector3.one * 1.2f;
        mImgIcon.color = new Color(1, 1, 1, 0);

        Sequence tAniSequence = DOTween.Sequence();
        tAniSequence.Append(mRctTopFrame.DOAnchorPosY(360, 0.5f));
        tAniSequence.Join(mRctBottomFrame.DOAnchorPosY(-360, 0.5f));
        tAniSequence.Join(mImgLogo.rectTransform.DOAnchorPosX(-320, 0.5f).SetEase(Ease.InSine));
        tAniSequence.Append(mImgLogo.rectTransform.DOSizeDelta(new Vector2(550, mImgLogo.rectTransform.sizeDelta.y), 0.15f).SetEase(Ease.OutSine));
        tAniSequence.Append(mImgLogo.rectTransform.DOSizeDelta(new Vector2(640, mImgLogo.rectTransform.sizeDelta.y), 0.15f).SetEase(Ease.InSine));
        tAniSequence.Append(mRctTitleBg.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutBack));
        tAniSequence.Append(mImgIcon.rectTransform.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutBounce));
        tAniSequence.Join(mImgIcon.DOFade(1, 0.1f));
        tAniSequence.AppendInterval(0.8f);
        tAniSequence.AppendCallback(Exit);
        tAniSequence.Append(mCvsBg.DOFade(0, 0.3f));
        tAniSequence.Join(mCvsContent.DOFade(0, 0.3f));
        tAniSequence.Join(mRctTopFrame.DOAnchorPosY(850, 0.5f));
        tAniSequence.Join(mRctBottomFrame.DOAnchorPosY(-760, 0.5f));
        tAniSequence.AppendCallback(Over);
    }

    private void Exit()
    {
        mDelExit?.Invoke();
    }

    private void Over()
    {
        gameObject.SetActive(false);
    }
}