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

public class CellDisplay
{
    public CellDisplay(SpriteRenderer _renderer)
    {
        Renderer = _renderer;
    }
    public SpriteRenderer Renderer;
    public SpriteRenderer Attachment = null;
}

public class BoardChunk : MonoBehaviour
{
    public GameObject spritePrefab;
    public GameObject candlePrefab;
    public GameObject gravePrefab;
    public int ChunkSize = 64;
    private CellDisplay[,] m_Renderers;

    public Sprite[] revealed;
    public Sprite[] hidden;
    public Sprite mine;
    public Sprite boneFloor;
    public Sprite[] m_Numbers;

    private Vector2Int m_Anchor;

    private void Awake()
    {
        m_Renderers = new CellDisplay[ChunkSize, ChunkSize];
        for (int y = 0; y <ChunkSize; y++)
        {
            for (int x = 0; x < ChunkSize; x++)
            {
                m_Renderers[x, y] = new CellDisplay(Instantiate<GameObject>(spritePrefab).GetComponent<SpriteRenderer>());
                m_Renderers[x, y].Renderer.transform.SetParent(transform);
            }
        }
    }


    // Update is called once per frame
    public void RebuildChunk(Cell[,] chunkData, Vector2Int bottomLeft)
    {
        m_Anchor = bottomLeft;
        m_Anchor -= new Vector2Int(ChunkSize / 2, ChunkSize / 2);
        m_Anchor = Vector2Int.Max(m_Anchor, Vector2Int.zero);
        for (int y = 0; y <  ChunkSize; y++)
        {
            for (int x = 0; x <  ChunkSize; x++)
            {
                m_Renderers[x, y].Renderer.transform.position = new Vector3(m_Anchor.x + x, m_Anchor.y + y, 0);
                m_Renderers[x, y].Renderer.sprite = chunkData[m_Anchor.x + x, m_Anchor.y + y].Hidden ? HiddenSprite(chunkData[m_Anchor.x + x, m_Anchor.y + y].Seed) : RevealedSprite(chunkData[m_Anchor.x + x, m_Anchor.y + y].Seed);
            }
        }
    }

    public void UpdateCell(Cell c, Vector2Int pos)
    {
        switch (c.State)
        {
            case CellState.Hidden:
                m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Renderer.sprite = HiddenSprite(c.Seed);
                if (m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment != null)
                {
                    Destroy(m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment.gameObject);
                    m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment = null;
                }
                    break;
            case CellState.Marked:
                print("Do mark");
                m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Renderer.sprite = HiddenSprite(c.Seed);
                if (m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment == null)
                {
                    print("Do mark 2");
                    m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment = (Instantiate<GameObject>(candlePrefab).GetComponent<SpriteRenderer>());
                    m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment.transform.SetParent(m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Renderer.transform);
                    m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment.transform.localPosition = Vector3.zero;
                    m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment.sortingOrder = 100;
                }
                break;
            case CellState.Revealed:
                if (c.m_EntityType == EntityType.Ghost)
                {
                    m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Renderer.sprite = boneFloor;
                }
                else
                {
                    m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Renderer.sprite = RevealedSprite(c.Seed);
                }
                if (m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment != null)
                {
                    Destroy(m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment.gameObject);
                    m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment = null;
                }
                break;
            case CellState.Graved:
                print("Do Grave");
                m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Renderer.sprite = RevealedSprite(c.Seed);
               /* if (m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment == null)
                {
                    m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment = (Instantiate<GameObject>(gravePrefab).GetComponent<SpriteRenderer>());
                    m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment.transform.SetParent(m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Renderer.transform);
                    m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment.transform.localPosition = Vector3.zero;
                    m_Renderers[pos.x - m_Anchor.x, pos.y - m_Anchor.y].Attachment.sortingOrder = 100;
                }*/
                break;
        }
    }

    private Sprite RevealedSprite(int i)
    {
        if(i < revealed.Length-1) { 
        return revealed[i+1];
        } else{
            return revealed[0];
        }
    }
    private Sprite HiddenSprite(int i)
    {
        if (i < hidden.Length - 1)
        {
            return hidden[i + 1];
        }
        else
        {
            return hidden[0];
        }
    }
}
