﻿using System;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
///     Create Custom achievements (Achievements with your own UI interface).
///     This Script will provide you the list of locked and unlocked achievement "names" and "keys",so you can create
///     achievements Interface with your UI.
///     For Unlocking Achievements , follow the same procedure as in IdnetSample.cs
///     Usage: Attach the script on any gameobject and pass your {app-id} in Start or call
///     CustomAchievementExample.CustomAchievements() from other script.
///     PrintLockedAchievements() and PrintUnLockedAchievements() are the example methods,that explains how to get the
///     UnLockedAchievements name and description.
///     For showing achievement images , you can add images locally in your project and load them in scene.
/// </summary>
public class CustomAchievementExample : MonoBehaviour
{
    //unlocked achievement list.
    public static Dictionary<string, string> UnLockedAchievements = new Dictionary<string, string>();
    //locked achievement list.
    public static Dictionary<string, string> LockedAchievements = new Dictionary<string, string>();

    private void Start()
    {
        //initialize idnet with your app-id.
        Idnet.I.Initialize("55961a8be694aa9957000288");
        CustomAchievements();
    }


    public static void CustomAchievements()
    {
        try
        {
            Idnet.I.RequireAppId();

            UnLockedAchievements.Clear();
            LockedAchievements.Clear();

            Idnet.User.Current.Achievements.Clear();
            var pAchievementOptions = new PAchievementOptions
            {
                playerid = Idnet.User.Current.Pid
            };

            Playtomic.Achievements.List(pAchievementOptions, (achievements, response) =>
            {
                Idnet.User.Current.Achievements = achievements;

                if (achievements.Count > 0)
                {
                    foreach (var achievement in achievements)
                    {
                        if (achievement.HasUserUnlocked)
                        {
                            UnLockedAchievements.Add(achievement.achievement, achievement.description);
                        }
                        else
                        {
                            LockedAchievements.Add(achievement.achievement, achievement.description);
                        }
                    }
                }
                else
                {
                    Debug.Log("Developer has not setup any achievements.");
                }

                //debug locked and unlocked achievements for test.
                PrintLockedAchievements();
                PrintUnLockedAchievements();
            });
        }
        catch (Exception exception)
        {
            Debug.Log(exception);
        }
    }


    /// <summary>
    ///     Prints the locked achievements.
    /// </summary>
    private static void PrintLockedAchievements()
    {
        foreach (var lockedachievement in LockedAchievements)
        {
            Debug.Log("<color=cyan>Locked Achievements...</color> ");
            Debug.Log("Locked: Achievement Name <color=red> " + lockedachievement.Key +
                      "</color>, Achievement Desc. <color=green>" + lockedachievement.Value + "</color>");
        }
    }

    /// <summary>
    ///     Prints the un-locked achievements.
    /// </summary>
    private static void PrintUnLockedAchievements()
    {
        foreach (var unlockedachievement in UnLockedAchievements)
        {
            Debug.Log("<color=cyan>Un-Locked Achievements...</color>  ");
            Debug.Log("UnLocked: Achievement Name <color=red> " + unlockedachievement.Key +
                      "</color>, Achievement Desc. <color=green>" + unlockedachievement.Value + "</color>");
        }
    }
}