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

public enum EntityType
{
    None,
    Ghost,
    Item
}

public enum CellState
{
    Hidden,
    Revealed,
    Marked,
    Graved
}

public class Cell
{
    public CellState State {
        get { return m_State; }
    }
    public bool Hidden {
        get { return m_State == CellState.Hidden || m_State == CellState.Marked; }
    }
    public int m_EntityID = -1;
    public EntityType m_EntityType = EntityType.None;
    public int Seed {
        get { return seed; }
    }
    int seed;
    private CellState m_State = CellState.Hidden;

    public Cell()
    {
        seed = Random.Range(0, 20);
    }

    public void Reveal()
    {
        m_State = CellState.Revealed;
    }

    public void Mark()
    {
        m_State = CellState.Marked;
    }

    public void Grave()
    {
        m_State = CellState.Graved;
    }
}

public class GameEntity
{
    public GameEntity(string _name)
    {
        Name = _name;
    }
    string Name;
}

public class Board
{
    public System.Action<Vector2Int, Cell> CellChanged;

    public Cell[,] GetBoard {
        get {
            return m_Board;
        }
    }


    private Cell[,] m_Board;
    private int m_BoardSize = 0;

    public Cell GetCell(int x, int y)
    {
        return m_Board[x, y];
    }

    public void BuildBoard(int _size)
    {
        m_BoardSize = _size;
        m_Board = new Cell[m_BoardSize, m_BoardSize];

        for (int y = 0; y < m_BoardSize; y++)
        {
            for (int x = 0; x < m_BoardSize; x++)
            {
                m_Board[x, y] = new Cell();
            }
        }

        SetUpBoardEntities(ref m_Board);
    }

    void SetUpBoardEntities(ref Cell[,] Board)
    {
        float[,] perlinMap;
        BuildPerlinMap(out perlinMap, m_BoardSize);

        float baseRate = 0.05f;
        float distance = 0f;
        float difficulty = 0f;
        Vector2Int mid = new Vector2Int(512, 512);
        for (int y = 0; y < m_BoardSize; y++)
        {
            for (int x = 0; x < m_BoardSize; x++)
            {
                difficulty = perlinMap[x, y];
                distance = Vector2Int.Distance(new Vector2Int(x, y), mid);
                distance /= 512.0f;
                float mineChance = baseRate + (0.1f * distance) + (0.05f * difficulty);

                if (Random.value < mineChance)
                {
                    Board[x, y].m_EntityID = 1;
                    Board[x, y].m_EntityType = EntityType.Ghost;
                }
            }
        }
    }

    void BuildPerlinMap(out float[,] perlinMap, int size)
    {
        float max = 0, min = 1;
        perlinMap = new float[size, size];
        float a = 1.0f, f = 1.0f, maxA = 0f;
        int octaves = 1;

        for (int y = 0; y < size; y++)
        {
            for (int x = 0; x < size; x++)
            {
                maxA = 0f;
                a = 1.0f; f = 1.0f;
                perlinMap[x, y] = 0;
                for (int o = 0; o < octaves; o++)
                {
                    perlinMap[x, y] += Mathf.PerlinNoise((float)x * 0.01234f * f, (float)y * 0.01234f * f) * a;
                    maxA += a;
                    a *= 0.5f;
                    f *= 2.0f;
                }
                perlinMap[x, y] /= maxA;

                if (perlinMap[x, y] > max)
                    max = perlinMap[x, y];
                if (perlinMap[x, y] < min)
                    min = perlinMap[x, y];
            }
        }

        for (int y = 0; y < size; y++)
        {
            for (int x = 0; x < size; x++)
            {
                perlinMap[x, y] = (perlinMap[x, y] - min) / (max - min);
            }
        }
    }

    public bool Reveal(Vector2Int cellPos)
    {
        if (!OnBoard(cellPos))
            return false;
        if (m_Board[cellPos.x, cellPos.y].Hidden)
        {
            m_Board[cellPos.x, cellPos.y].Reveal();
            CellChanged(cellPos, m_Board[cellPos.x, cellPos.y]);
            return true;
        }
        return false;
    }

    public bool Mark(Vector2Int cellPos)
    {
        if (!OnBoard(cellPos))
            return false;
        if (m_Board[cellPos.x, cellPos.y].Hidden)
        {
            Debug.Log("Mark");
            m_Board[cellPos.x, cellPos.y].Mark();
            CellChanged(cellPos, m_Board[cellPos.x, cellPos.y]);
            return true;
        }
        return false;
    }

    public bool Grave(Vector2Int cellPos)
    {
        if (!OnBoard(cellPos))
            return false;
        if (m_Board[cellPos.x, cellPos.y].Hidden)
        {
            m_Board[cellPos.x, cellPos.y].Grave();
            CellChanged(cellPos, m_Board[cellPos.x, cellPos.y]);
            return true;
        }
        return false;
    }

    public void ClearCell(Vector2Int cellPos)
    {
        m_Board[cellPos.x, cellPos.y].m_EntityID = -1;
        m_Board[cellPos.x, cellPos.y].Reveal();
    }

    private bool OnBoard(Vector2Int cellPos)
    {
        return !(cellPos.x < 0 && cellPos.x > m_BoardSize-1 && cellPos.y < 0 && cellPos.y > m_BoardSize-1);
    }

    public int CheckSurroundingEntities(Vector2Int cellPos)
    {
        int count = 0;
        for (int y = cellPos.y - 1; y < cellPos.y + 2; y++)
            for (int x = cellPos.x -1; x < cellPos.x + 2; x++)
            {
                if (x == cellPos.x && y == cellPos.y)
                    continue;
                if(m_Board[x,y].m_EntityID > 0 && m_Board[x,y].Hidden)
                {
                    count++;
                }
            }

        return count;
    }

}
