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

public class blockBehavier : MonoBehaviour
{

    public GameManagement gameManage;
    public int health = 1;
    public Vector3 position;
    public int wallType;

    public void SetGameManagement(GameManagement gameManage)
    {
        this.gameManage = gameManage;
    }

    public void GetHit()
    {
        health = 0;

    }

    //Usethisforinitialization
    void Start()
    {

    }

    //Updateiscalledonceperframe
    void Update()
    {

        if (health == 0)
        {

            health--;
            Debug.Log("healk" + health);
            switch (wallType)
            {
                case 2:
                    if (gameManage.m_mainmap[(int)position.x, (int)position.y + 1] == 9)
                    {
                        Instantiate(gameManage.floor, new Vector3(position.x, position.y, 0f), Quaternion.identity);
                    }
                    else
                    {
                        Instantiate(gameManage.broke_wall[0], position, Quaternion.identity);
                    } break;
                case 3:
                    if (gameManage.m_mainmap[(int)position.x, (int)position.y + 1] == 9)
                    {
                        Instantiate(gameManage.floor, new Vector3(position.x, position.y, 0f), Quaternion.identity);
                    }
                    else
                    {
                        Instantiate(gameManage.broke_wall[1], position, Quaternion.identity);
                    }
                    break;
                case 4:
                    if (gameManage.m_mainmap[(int)position.x, (int)position.y + 1] == 9)
                    {
                        Instantiate(gameManage.floor, new Vector3(position.x, position.y, 0f), Quaternion.identity);
                    }
                    else
                    {
                        Instantiate(gameManage.broke_wall[2], position, Quaternion.identity);
                    }
                    break;
                case 5:
                    if (gameManage.m_mainmap[(int)position.x, (int)position.y + 1] == 9)
                    {
                        Instantiate(gameManage.floor, new Vector3(position.x, position.y, 0f), Quaternion.identity);
                    }
                    else
                    {
                        Instantiate(gameManage.broke_wall[3], position, Quaternion.identity);
                    }
                    break;

            }
            switch (wallType)
            {
                case 2:
                case 3:
                case 4:
                case 5:
                    if (gameManage.m_mainmap[(int)position.x, (int)position.y - 1] == 9)
                    {
                        Instantiate(gameManage.floor, new Vector3(position.x, position.y - 1, 0f), Quaternion.identity);
                    }

                    gameManage.m_mainmap[(int)position.x, (int)position.y] = 9;
                    break;

                case 0:
                    
                    Instantiate(gameManage.broke_wall[4], new Vector3(position.x, position.y, 0f), Quaternion.identity);
                    Instantiate(gameManage.broke_wall[5], new Vector3(position.x, position.y - 1, 0f), Quaternion.identity);
                    gameManage.m_mainmap[(int)position.x, (int)position.y] = 8;

                    
                    break;






                    //------
                    /*
                    case 1:
                        Debug.Log("XPos" + position.x + "YPos" + position.y);

                        if (gameManage.m_mainmap[(int)position.x - 1, (int)position.y] == 9 || gameManage.m_mainmap[(int)position.x + 1, (int)position.y] == 9)
                        {
                            if (gameManage.m_mainmap[(int)position.x - 1, (int)position.y] == 9)
                            {
                                Instantiate(gameManage.broke_wall[9], new Vector3(position.x, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[8], new Vector3(position.x, position.y + 1, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[7], new Vector3(position.x - 1, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[6], new Vector3(position.x - 1, position.y + 1, 0f), Quaternion.identity);
                            }
                            else
                            {
                                Instantiate(gameManage.broke_wall[7], new Vector3(position.x, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[6], new Vector3(position.x, position.y + 1, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[9], new Vector3(position.x + 1, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[8], new Vector3(position.x + 1, position.y + 1, 0f), Quaternion.identity);
                            }
                            gameManage.m_mainmap[(int)position.x, (int)position.y] = 8;
                            if (gameManage.m_mainmap[(int)position.x, (int)position.y + 1] == 2)
                            {
                                gameManage.m_mainmap[(int)position.x, (int)position.y + 1] = 8;
                            }
                        }
                        else if (gameManage.m_mainmap[(int)position.x - 1, (int)position.y] == 8 || gameManage.m_mainmap[(int)position.x + 1, (int)position.y] == 8)
                        {
                            if (gameManage.m_mainmap[(int)position.x - 1, (int)position.y] == 8)
                            {
                                Instantiate(gameManage.broke_wall[9], new Vector3(position.x, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[8], new Vector3(position.x, position.y + 1, 0f), Quaternion.identity);
                                Instantiate(gameManage.floor, new Vector3(position.x - 1, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.floor, new Vector3(position.x - 1, position.y + 1, 0f), Quaternion.identity);
                            }
                            else
                            {
                                Instantiate(gameManage.broke_wall[7], new Vector3(position.x, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[6], new Vector3(position.x, position.y + 1, 0f), Quaternion.identity);
                                Instantiate(gameManage.floor, new Vector3(position.x + 1, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.floor, new Vector3(position.x + 1, position.y + 1, 0f), Quaternion.identity);
                            }
                            gameManage.m_mainmap[(int)position.x, (int)position.y] = 8;

                            if (gameManage.m_mainmap[(int)position.x, (int)position.y + 1] == 2)
                            {
                                gameManage.m_mainmap[(int)position.x, (int)position.y + 1] = 8;
                            }
                        }
                        else
                        {
                            Instantiate(gameManage.broke_wall[5], position, Quaternion.identity);
                            Instantiate(gameManage.broke_wall[4], new Vector3(position.x, position.y + 1, 0f), Quaternion.identity);

                            gameManage.m_mainmap[(int)position.x, (int)position.y] = 9;
                            if (gameManage.m_mainmap[(int)position.x, (int)position.y + 1] == 2)
                            {
                                gameManage.m_mainmap[(int)position.x, (int)position.y + 1] = 9;
                            }
                        }
                        break;

                    case 0:

                        if (gameManage.m_mainmap[(int)position.x - 1, (int)position.y] == 9 || gameManage.m_mainmap[(int)position.x + 1, (int)position.y] == 9)
                        {
                            if (gameManage.m_mainmap[(int)position.x - 1, (int)position.y] == 9)
                            {
                                Instantiate(gameManage.broke_wall[9], new Vector3(position.x, position.y - 1, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[8], new Vector3(position.x, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[6], new Vector3(position.x - 1, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[7], new Vector3(position.x - 1, position.y - 1, 0f), Quaternion.identity);
                            }
                            else
                            {
                                Instantiate(gameManage.broke_wall[7], new Vector3(position.x, position.y - 1, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[6], new Vector3(position.x, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[8], new Vector3(position.x + 1, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[9], new Vector3(position.x + 1, position.y - 1, 0f), Quaternion.identity);

                            }
                            gameManage.m_mainmap[(int)position.x, (int)position.y] = 8;
                            if (gameManage.m_mainmap[(int)position.x, (int)position.y - 1] == 2)
                            {
                                gameManage.m_mainmap[(int)position.x, (int)position.y - 1] = 8;
                            }
                        }
                        else if (gameManage.m_mainmap[(int)position.x - 1, (int)position.y] == 8 || gameManage.m_mainmap[(int)position.x + 1, (int)position.y] == 8)
                        {
                            if (gameManage.m_mainmap[(int)position.x - 1, (int)position.y] == 8)
                            {
                                Instantiate(gameManage.broke_wall[9], new Vector3(position.x, position.y - 1, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[8], new Vector3(position.x, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.floor, new Vector3(position.x - 1, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.floor, new Vector3(position.x - 1, position.y - 1, 0f), Quaternion.identity);
                            }
                            else
                            {
                                Instantiate(gameManage.broke_wall[7], new Vector3(position.x, position.y - 1, 0f), Quaternion.identity);
                                Instantiate(gameManage.broke_wall[6], new Vector3(position.x, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.floor, new Vector3(position.x + 1, position.y, 0f), Quaternion.identity);
                                Instantiate(gameManage.floor, new Vector3(position.x + 1, position.y - 1, 0f), Quaternion.identity);

                            }
                            gameManage.m_mainmap[(int)position.x, (int)position.y] = 8;
                            if (gameManage.m_mainmap[(int)position.x, (int)position.y - 1] == 2)
                            {
                                gameManage.m_mainmap[(int)position.x, (int)position.y - 1] = 8;
                            }
                        }
                        else
                        {
                            Instantiate(gameManage.broke_wall[4], position, Quaternion.identity);
                            Instantiate(gameManage.broke_wall[5], new Vector3(position.x, position.y - 1, 0f), Quaternion.identity);

                            gameManage.m_mainmap[(int)position.x, (int)position.y] = 9;
                            if (gameManage.m_mainmap[(int)position.x, (int)position.y - 1] == 2) { 
                            gameManage.m_mainmap[(int)position.x, (int)position.y - 1] = 9;
                        }
                        }


                        break;
                        */
            }



            Debug.Log("testt" + gameManage.m_mainmap[(int)position.x, (int)position.y]);
            Destroy(this.gameObject);
        }

    }
}
