using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// Defines the logic behind a single achievement on the UI
/// </summary>
public class UIAchievement : MonoBehaviour
{
    public TMP_Text Title, Description, Percent;
    public Image Icon, OverlayIcon, ProgressBar;
    public GameObject SpoilerOverlay;
    public TMP_Text SpoilerText;
    public AchievenmentStack AS;
    public GameObject Tick;
    public GameObject CompleteToClaimButton;
    public Button ClaimButton;
    /// <summary>
    /// Destroy object after a certain amount of time
    /// </summary>
    public void StartDeathTimer ()
    {
        StartCoroutine(Wait());
    }


    void RewardSetter(AchievementInfromation Information)
    {
        Debug.Log("Reward Setter");
        int index=transform.GetSiblingIndex();
        int CurrentTotalReward = Information.TotalCoinReward;
        int subAchievementIndex = Information.SubAchievementIndex;
        int RewardtoAdd = Information.CoinRewards[subAchievementIndex];
        int TotalReward = RewardtoAdd + CurrentTotalReward;
        Information.TotalCoinReward = TotalReward;
        PlayerPrefs.SetInt("TotalRewards"+index, TotalReward);
    }

    /// <summary>
    /// Add information  about an Achievement to the UI elements
    /// </summary>

    public void Set (AchievementInfromation Information, AchievementState State)
    {
        if(Information.Spoiler && !State.Achieved)
        {
            if(SpoilerOverlay)
                SpoilerOverlay.SetActive(true);
            if(SpoilerText)
                SpoilerText.text = AchievementManager.instance.SpoilerAchievementMessage;
        }
        else
        {
            // Title.text = Information.DisplayName;
            Description.text = Information.Description;

            if (Information.LockOverlay && !State.Achieved)
            {
                if (OverlayIcon)
                {
                    OverlayIcon.gameObject.SetActive(true);
                    OverlayIcon.sprite = Information.LockedIcon;
                }
                if(Icon)
                    Icon.sprite = Information.AchievedIcon;
            }
            else
            {
                if(Icon)
                    Icon.sprite = State.Achieved ? Information.AchievedIcon : Information.LockedIcon;
            }

            if (Information.Progression)
            {
                float CurrentProgress = AchievementManager.instance.ShowExactProgress ? State.Progress : (State.LastProgressUpdate * Information.NotificationFrequency);
                float DisplayProgress = State.Achieved ? Information.ProgressGoal : CurrentProgress;

                if (State.Achieved)
                {
                    RewardSetter(Information);
                    if(Percent)
                    {
                        Percent.text = Information.ProgressGoal + Information.ProgressSuffix + " / " + Information.ProgressGoal + Information.ProgressSuffix + " (Achieved)";
                    }
                }
                else
                {
                    if(Percent)
                        Percent.text = DisplayProgress + Information.ProgressSuffix +  " / " + Information.ProgressGoal + Information.ProgressSuffix;
                }

                ProgressBar.fillAmount = DisplayProgress / Information.ProgressGoal;
            }
            else //Single Time
            {
                ProgressBar.fillAmount = State.Achieved ? 1 : 0;
                Percent.text = State.Achieved ? "(Achieved)" : "(Locked)";
            }
        }
    }

    private IEnumerator Wait ()
    {
        yield return new WaitForSeconds(AchievementManager.instance.DisplayTime);
        GetComponent<Animator>().SetTrigger("ScaleDown");
        yield return new WaitForSeconds(0.1f);
        AS.CheckBackLog();
        Destroy(gameObject);
    }
}