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

public class Levels : MonoBehaviour {

    public static Levels instance;
    public Object[] blocsPrefabs;
    public Object herosPrefab;
    public static int lastLevel = 1;
    public static int currentLevel;
    private string lvlTxt;
    static public bool checkpoint;

    Dictionary<Bloc.BLOC_TYPE, Object> blocsHashtable = null;
    Dictionary<Color32, Bloc.BLOC_TYPE> blocsTypeHashtable = null;

    [HideInInspector]
    public bool loadingInProgress = false;
    private Vector3 startPos;

    private string baseUrl = "http://www.zerokcm.fr/blocs/levels/";

	
    void Start()
    {
        lastLevel = PlayerPrefs.GetInt("lastLevel", 1);
    }
    void Awake () 
    {
        instance = this;
        InitHashtable();
	}

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.D)) PlayerPrefs.DeleteAll();
        
    }
    void InitHashtable()
    {
        blocsHashtable = new Dictionary<Bloc.BLOC_TYPE, Object>();
        blocsTypeHashtable = new Dictionary<Color32, Bloc.BLOC_TYPE>();

        foreach (Object blocPrefab in blocsPrefabs)
        {
            if (blocPrefab is GameObject)
            {
                Bloc blocScript = ((GameObject)blocPrefab).GetComponent<Bloc>();
                if (blocScript != null)
                {
                    blocsHashtable.Add(blocScript.type, blocPrefab);
                    blocsTypeHashtable.Add(blocScript.col, blocScript.type);
                }
            }
        }
    }

    public void LoadLevel2(int numLvl)
    {
        Debug.Log(numLvl);
        //Debug.Log(PlayerPrefs.GetInt("lastLevel"));

        if (numLvl <= PlayerPrefs.GetInt("lastLevel", 1))
        {
            currentLevel = numLvl;
            Texture2D tempTex = Resources.Load("level" + numLvl.ToString("00")) as Texture2D;
            tempTex.Apply(true, false);

            Color32[] pixel = tempTex.GetPixels32();

            for (int j = 0; j < tempTex.width; j++)
            {
                for (int i = 0; i < tempTex.height; i++)
                {
                    Color32 color32 = pixel[i + j * tempTex.width];
                    //color32.a = 255;
                    PW_Color color = new PW_Color(color32);

                    float h = color.r;
                    //Debug.Log(color32);
                    CreateBloc(color32, i, j);

                }
            }
            loadingInProgress = false;
            GameObject hero = (GameObject)Instantiate(herosPrefab, startPos, Quaternion.identity);
            hero.transform.parent = transform;
            hero.transform.localRotation = new Quaternion(0f, 90f, 90f, 0f);
            Game.instance.herosScript = hero.GetComponent<Heros>();
            Game.instance.state = Game.STATES.PLAY;
        }
    }

    void CreateBloc(Color32 c, int pos_x, int pos_y)
    {
        
        if (blocsTypeHashtable.ContainsKey(c))
        {
            Bloc.BLOC_TYPE blocType = blocsTypeHashtable[c];
            Object blocPrefab = blocsHashtable[blocType];
            GameObject temp = (GameObject)Instantiate(blocPrefab, new Vector3(pos_x, pos_y, 0f), Quaternion.identity);
            temp.transform.rotation = new Quaternion(0, 180, 0, 0);
            temp.transform.parent = transform;
            temp.name = blocPrefab.name;
            if (blocType == Bloc.BLOC_TYPE.START || (blocType == Bloc.BLOC_TYPE.CHECKPOINT && checkpoint)) startPos = new Vector3(pos_x, pos_y, 0f);
        }
    }
    
}
