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

public class ButtonEndings : MonoBehaviour
{
    public Action DelClick;

    [SerializeField] GameObject mGobIncomplete;
    [SerializeField] GameObject mGobComplete;

    [SerializeField] Text mTxtUnlocked;
    [SerializeField] Text mTxtTotal;

    [SerializeField] Image mImgIcon;

    [SerializeField] GameObject mGobTips;
    [SerializeField] GameObject mGobNewTag;
    [SerializeField] GameObject mGobCompleteTag;

    private bool mIsCompleted;

    private void Awake()
    {
        Button tBtn = GetComponent<Button>();
        UIUtils.BindBtn(tBtn, OnClick, AudioClipType.Click_Tip);
    }

    public void Init(int pLevelID, int pUnlockCount, int pTotalCount)
    {
        mIsCompleted = pUnlockCount == pTotalCount;

        mGobIncomplete.SetActive(!mIsCompleted);
        mGobComplete.SetActive(mIsCompleted);

        if (!mIsCompleted)
        {
            mTxtUnlocked.text = pUnlockCount.ToString();
            mTxtTotal.text = pTotalCount.ToString();
        }
        else
        {
            mImgIcon.sprite = ResourceManager.Instance.LoadRes<Sprite>(Const.Path.GetLevelIconSmall(pLevelID));
        }

        ShowTip(false);
        ShowNew(false);
        ShowComplete(false);
    }

    public void ShowTip(bool pShow)
    {
        if (mGobTips != null)
        {
            mGobTips.SetActive(pShow);
        }
    }

    public void ShowNew(bool pShow)
    {
        if (mGobNewTag != null)
        {
            mGobNewTag.SetActive(pShow);
            if (pShow)
            {
                mGobNewTag.transform.localScale = Vector3.zero;
                mGobNewTag.transform.DOScale(Vector3.one, 0.5f).SetEase(Ease.OutBounce);
                AudioManager.Instance.PlaySound(AudioClipType.New);
            }
        }
    }

    public void ShowComplete(bool pShow)
    {
        if (mGobCompleteTag != null)
        {
            mGobCompleteTag.SetActive(pShow);
            if (pShow)
            {
                mGobCompleteTag.transform.localScale = Vector3.zero;
                mGobCompleteTag.transform.DOScale(Vector3.one, 0.5f).SetEase(Ease.OutBounce);
                AudioManager.Instance.PlaySound(AudioClipType.Complete);
            }
        }
    }

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

        ShowTip(false);
        DelClick?.Invoke();
    }
}