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


public class PDAMapManager : MonoBehaviour
{
    public static PDAMapManager Instance;

    [SerializeField]
    private int width = 3;
    [SerializeField]
    private int height = 3;

    [SerializeField]
    private int fieldSize = 32;

    [SerializeField, Range(1, 3)]
    private float scale = 1;

    public Sprite flagSprite;
    public Sprite mineSprite;
    public Sprite treasureSprite;


    private int slotCount;
    private List<GameObject> slots = new List<GameObject>();
    private GridLayoutGroup layout;

    public GameObject grid;
    public GameObject gridSlot;

    private GameObject selectedSlot;

    private void Awake()
    {
        if (Instance != null)
        {
            Destroy(gameObject);
            return;
        }
        else
        {
            Instance = this;
        }

        layout = grid.GetComponent<GridLayoutGroup>();
    }

    private void OnEnable()
    {
        MapGenerator.OnMapGenerated += InitializeMap;
        BeaconManager.OnBeaconPlaced += PlaceBeacon;
    }

    private void OnDisable()
    {
        MapGenerator.OnMapGenerated -= InitializeMap;
        BeaconManager.OnBeaconPlaced -= PlaceBeacon;
    }

    public void InitializeMap(MapGenerator.MapData _data)
    {
        SetGrid(_data.Width, _data.Height);
        fieldSize = (int)(((280 / (_data.Width + 1)) - 3) * scale);
        SetFieldSize(fieldSize);
    }

    public void RegenerateSlots()
    {
        foreach (GameObject slot in slots)
        {
            Destroy(slot);
        }
        slots.Clear();

        for (int i = 0; i < slotCount; i++)
        {
            slots.Add(Instantiate(gridSlot));
            slots[i].transform.SetParent(grid.transform, false);
            slots[i].GetComponent<PDAMapSlot>().Initialize(i);
        }
    }

    public void SetFieldSize( int s )
    {
        layout.cellSize = new Vector2(s,s);
    }

    public void SetGrid(int w, int h)
    {
        if (w <= 0) { w = 1; }
        if (h <= 0) { h = 1; }
        slotCount = w * h;
        layout.constraintCount = w;
        RegenerateSlots();
    }

    public void SelectSlot(int slot_id)
    {
        GameObject newslot = slots[slot_id];
        if (newslot == selectedSlot)
        {
            Debug.Log("Slot deselected");
            selectedSlot.GetComponentsInChildren<Image>()[1].color = new Color32(255, 235, 170, 255);
            selectedSlot = null;
        }
        else
        {
            Debug.Log("Selected slot " + slot_id.ToString());
            if (selectedSlot != null) selectedSlot.GetComponentsInChildren<Image>()[1].color = new Color32(255, 235, 170, 255);
            selectedSlot = newslot;
            selectedSlot.GetComponentsInChildren<Image>()[1].color = new Color32(170, 235, 170, 255);
        }
    }

    public void DeselectSlot()
    {
        Debug.Log("Slot deselected");
        if (selectedSlot != null) selectedSlot.GetComponentsInChildren<Image>()[1].color = new Color32(255, 235, 170, 255);
        selectedSlot = null;
    }

    public void FlagField(int field_id)
    {

    }

    public void FlagField()
    {
        if (selectedSlot == null)
        {
            Debug.Log("No slot selected.");
            return;
        }
        else
        {
            bool flag = selectedSlot.GetComponent<PDAMapSlot>().isFlagged;

            if (flag)
            {
                Debug.Log("Unflag field");
                //selectedSlot.GetComponentInChildren<Text>().text = string.Empty;
                selectedSlot.GetComponentInChildren<Image>().color = Color.grey;
                selectedSlot.GetComponentsInChildren<Image>()[2].sprite = null;
                selectedSlot.GetComponentsInChildren<Image>()[2].color = new Color32(255, 255, 255, 0);
            }
            else
            {
                Debug.Log("Flag field");
                //selectedSlot.GetComponentInChildren<Text>().text = "F";
                selectedSlot.GetComponent<Image>().color = Color.red;
                selectedSlot.GetComponentsInChildren<Image>()[2].sprite = flagSprite;
                selectedSlot.GetComponentsInChildren<Image>()[2].color = new Color32(255, 255, 255, 255);

            }

            selectedSlot.GetComponent<PDAMapSlot>().Flag();
            DeselectSlot();

        }
    }

    public void PlaceBeacon(int[] _coords)
    {

    }

}
