using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Newtonsoft.Json;

public class LevelSelect : MonoBehaviour
{
    public GameObject levelPrefab;

    public int LevelCount = 25;
    public static Dictionary<int,int> levelProgress;

    // public static Dictionary<int,int> levelProgress  {
    //     get{
    //         if(m_levelProgress == null){
    //             if(!PlayerPrefs.HasKey("progress") ){        
    //                 Debug.Log("No key for progress found, generating new");
    //                 m_levelProgress=new Dictionary<int, int>();
    //             }else{
    //                 try{
    //                     m_levelProgress= JsonConvert.DeserializeObject<Dictionary<int,int>>(PlayerPrefs.GetString("progress"));
    //                 }catch{
    //                     Debug.LogError("Failed parsing progress from save, Creating new save");
    //                     m_levelProgress = new Dictionary<int, int>();
    //                 }
    //             }
    //         }
            

    //         return m_levelProgress;
    //     }set{
    //         m_levelProgress=value;
    //         // Debug.Log()
    //         PlayerPrefs.SetString("progress", JsonConvert.SerializeObject(value));
    //         PlayerPrefs.Save();
    //         Debug.Log("saving: " +JsonConvert.SerializeObject(value));


    //     }
    // }
 
    public static void SetLevel(int level, int stars){
        if(levelProgress == null){
            levelProgress = new Dictionary<int, int>();
        }

        if(levelProgress.ContainsKey(level)){
            levelProgress[level] = stars;
        }else{
            levelProgress.Add(level,stars);
        }

        Debug.Log(JsonConvert.SerializeObject(levelProgress));
        PlayerPrefs.SetString("progress", JsonConvert.SerializeObject(levelProgress));
        PlayerPrefs.Save();
    }

    public Sprite locked,unlocked;
    void Start()
    {
        // levelProgress.Add(0,2);
        if(PlayerPrefs.HasKey("progress")){
            string savedata = PlayerPrefs.GetString("progress");
            levelProgress = JsonConvert.DeserializeObject<Dictionary<int,int>>(savedata);

            Debug.Log(savedata);
        }else{
            levelProgress = new Dictionary<int, int>();
        }
       
        //Populate level selection
        for(int i=0; i < LevelCount;i++){
             int curIndex = i;
            GameObject newLevel = Instantiate(levelPrefab, levelPrefab.transform.parent);
            newLevel.transform.GetChild(0).GetComponent<Text>().text = $"Level {i+1}";

            if(i==0){
                newLevel.transform.GetChild(1).GetComponent<Image>().sprite = unlocked;
                if(levelProgress.ContainsKey(0)){
                    newLevel.transform.GetChild(2).gameObject.SetActive(true);
                    for(int x =0; x < 3; x++){
                        newLevel.transform.GetChild(2).GetChild(x).GetComponent<Image>().color = (levelProgress[i] > x) ? Color.yellow : Color.gray;
                    }
                }else{
                    
                }
            }else{
                if(levelProgress.ContainsKey(i-1)){
                    newLevel.transform.GetChild(2).gameObject.SetActive(true);
                    newLevel.transform.GetChild(1).GetComponent<Image>().sprite = unlocked;
                    if(levelProgress.ContainsKey(i)){
                        for(int x =0; x < 3; x++){
                        newLevel.transform.GetChild(2).GetChild(x).GetComponent<Image>().color = (levelProgress[i] > x) ? Color.yellow : Color.gray;
                        }
                    }
                }else{
                    newLevel.transform.GetChild(1).GetComponent<Image>().sprite = locked;
                    newLevel.GetComponent<Button>().interactable=false;
                    newLevel.transform.GetChild(2).gameObject.SetActive(false);
                }
            }

            newLevel.GetComponent<Button>().onClick.AddListener(()=>{SceneManager.LoadScene("Level"+curIndex);});


        }

        Destroy(levelPrefab);
    }

    // Update is called once per frame
    void Update()
    {
        
    }


    public static void SaveProgress(Dictionary<int,int> progress){
        string val = JsonConvert.SerializeObject(progress);
        PlayerPrefs.SetString("progress", val);
        PlayerPrefs.Save();

        Debug.Log("saving: " +val);
    }
}
