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

public class GameManagement : MonoBehaviour {
    public Map mapdata;
	public static GameManagement instance=null;
    public int[,] m_mainmap;
	public bool gameOver;
    public int column = 6;
    public int row = 5;
    private int wallType=0;

    public GameObject floor;
    public GameObject[] joint;
    public GameObject[] hor_wall;
    public GameObject[] ver_wall;
    public GameObject[] broke_wall;

    private Transform holder,holder_bg;
    private List<Vector3> gridPosition = new List<Vector3>();


    GameObject toInstantiate;

    void Awake(){
		if (instance == null)
			instance = this;
		else if (instance != this)
			Destroy (gameObject);
		gameOver = false;

        m_mainmap = mapdata.Map02;

        InitGame();

	}

    void intitBoard()
    {/*
        holder_bg = new GameObject("Board_bg").transform;
        gridPosition.Clear();
        for (int j = 0; j < m_mainmap.GetLength(0); j++)
        {

            for (int i = 0; i < m_mainmap.GetLength(1); i++)
            {
                toInstantiate = floor;
                GameObject instance = Instantiate(toInstantiate, new Vector3(i, j, 0f), Quaternion.identity) as GameObject;
                instance.transform.SetParent(holder_bg);
            }
        }
        //holder_bg.transform.localScale = new Vector3(3, 3, 1);*/
    }

    void setup()
    {
        holder = new GameObject("Board").transform;

        Debug.Log("sad " +  m_mainmap.GetLength(1));

        for (int j = 0; j < m_mainmap.GetLength(0); j++)
          {

              for (int i = 0; i < m_mainmap.GetLength(1); i++)
              {
                Debug.Log("i " + i + " j " + j);
                // GameObject toInstantiate = floor;
                wallType = 9;
                toInstantiate = floor;
                switch (m_mainmap[j, i])
                  {
                      case 0:
                        if (j < m_mainmap.GetLength(0)-1)
                        {
                            switch (m_mainmap[j+1,i])
                            {
                                case 0: toInstantiate = floor; break;
                                case 1: if (m_mainmap[j + 1, i + 1] !=0 && m_mainmap[j+ 1, i- 1] !=0) {
                                        //
                                        toInstantiate = joint[10];
                                    } else {
                                        if (m_mainmap[j+1, i-1] == 0)
                                        {
                                            toInstantiate = joint[9];
                                        }else
                                        {
                                            toInstantiate = joint[11];
                                        }
                                    } break;
                                case 2: toInstantiate = ver_wall[1]; wallType = 1; break;
                            }
                        }
                          break;
                      case 1:
                            if (m_mainmap[ j, i + 1] != 0 && m_mainmap[ j - 1,i] != 0) { toInstantiate = joint[0]; }
                            if (m_mainmap[ j, i - 1] != 0 && m_mainmap[ j - 1,i] != 0) { toInstantiate = joint[2]; }
                            if (m_mainmap[ j, i + 1] != 0 && m_mainmap[ j + 1,i] != 0) { toInstantiate = joint[6]; }
                            if (m_mainmap[ j, i - 1] != 0 && m_mainmap[ j + 1,i] != 0) { toInstantiate = joint[8]; }

                        if (m_mainmap[j,i - 1] != 0 && m_mainmap[j,i+1 ] != 0 && m_mainmap[ j -1,i]!=0) { toInstantiate = joint[1]; }
                        if (m_mainmap[j,i - 1] != 0 && m_mainmap[j,i + 1] != 0 && m_mainmap[ j + 1,i] != 0) { toInstantiate = joint[7]; }

                        if (m_mainmap[ j+1,i] != 0 && m_mainmap[ j-1,i] != 0 && m_mainmap[j,i+1] != 0) { toInstantiate = joint[3]; }
                        if (m_mainmap[ j + 1,i] != 0 && m_mainmap[ j - 1,i] != 0 && m_mainmap[j,i - 1] != 0) { toInstantiate = joint[5]; }
                        if (m_mainmap[ j + 1,i] != 0 && m_mainmap[ j - 1,i] != 0 && m_mainmap[j,i - 1] != 0 && m_mainmap[j,i + 1] != 0) { toInstantiate = joint[4]; }
                        
                        break;
                    case 2:
                        //ver_wall or not?
                        if (m_mainmap[j,i + 1] != 2 && m_mainmap[j,i - 1] != 2)
                        {
                            //bolow joint or not?
                            if (m_mainmap[ j + 1,i] == 1)
                            {
                                if(m_mainmap[j + 1,i+1] != 0 && m_mainmap[j+1,i - 1] != 0)
                                {
                                    wallType = 4;
                                    toInstantiate = hor_wall[2];
                                }else
                                {
                                    if (m_mainmap[j + 1, i + 1] !=0)
                                    {
                                        wallType = 3;
                                        toInstantiate = hor_wall[1];
                                    }else
                                    {
                                        wallType = 5;
                                        toInstantiate = hor_wall[3];
                                    }
                                }
                            }else
                            {
                                wallType = 2;
                                toInstantiate = hor_wall[0];
                            }
                        }else
                        {

                            wallType = 0;
                            toInstantiate = ver_wall[0];
                        }

                        break;

                  }


                  GameObject instance = Instantiate(toInstantiate, new Vector3(i, j, 0f), Quaternion.identity) as GameObject;
                           instance.transform.SetParent(holder);
                if(m_mainmap[j, i]==2 || wallType==1)
                {
                    instance.GetComponent<blockBehavier>().SetGameManagement(this);
                    instance.GetComponent<blockBehavier>().position=new Vector3(i, j, 0f);
                    instance.GetComponent<blockBehavier>().wallType = wallType;
                }
              }
          }
        //holder.transform.localScale = new Vector3(3, 3,1);

    }
    void InitGame()
    {
        // 0:floor
        // 1:joint
        // 2:wall


        intitBoard();
        setup();


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

	public bool _GameOver{
		get{return this.gameOver;}
		set{ this.gameOver = value;}
	}

    private void Start()
    {
                mapdata = GetComponent<Map>();
    }
}
