using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cinemachine;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UI;


public class GameManagerScript : MonoBehaviour {
    CinemachineBrain camBrain;

    RoundManager roundManager;

    [SerializeField] List<GameObject> enemies;
    [SerializeField] List<GameObject> humans;
    [SerializeField] GameObject initiativePanel;
    [SerializeField] GameObject initiativeCreaturePanel;
    [SerializeField] GameObject roundActionsPanel;

    [SerializeField] Grid grid;

    [SerializeField] Tilemap tileMap;
    public Tilemap TileMap { get => tileMap; set => tileMap = value; }
    public Grid Grid { get => grid; set => grid = value; }

    List<Creature> combatants = new List<Creature>();

    static GameManagerScript instance;

    public static GameManagerScript GetInstance() {
        return instance;
    }

    private void Start() {
        instance = this;
        combatants.AddRange(enemies.Select(e => e.GetComponent<Creature>()));
        combatants.AddRange(humans.Select(h => h.GetComponent<Creature>()));
    }

    private void Awake() {
        if (camBrain == null) {
            camBrain = Camera.main.GetComponent<CinemachineBrain>();
        }

        if (roundManager == null) {
            roundManager = GameObject.Find("RoundManager").GetComponent<RoundManager>();
        }
    }

    public CinemachineVirtualCamera GetActiveCamera() {
        CinemachineVirtualCamera cam = camBrain.ActiveVirtualCamera as CinemachineVirtualCamera;

        return cam;
    }

    internal void RollInitiative() {
        combatants.ForEach(creature => {
            creature.RollInit();
        });

        combatants.Sort((x, y) => x.GetInit().CompareTo(y.GetInit()));
        ClearInitPanel();
        BuildInitPanel();
        ShowRoundActionsPanel(true);
        roundManager.StartFirstRound(combatants);
    }

    private void ShowRoundActionsPanel(bool show) {
        roundActionsPanel.SetActive(show);
    }

    private void ClearInitPanel() {
        int children = initiativePanel.transform.childCount;
        for (int i = children - 1; i >= 0; i--) {
            GameObject.Destroy(initiativePanel.transform.GetChild(i).gameObject);
        }
    }

    public void ShowInitPanel(Boolean show) {
        initiativePanel.SetActive(show);
    }

    internal void BuildInitPanel() {
        initiativePanel.SetActive(true);
        combatants.ForEach(c => {
            GameObject creaturePanel = GameObject.Instantiate(initiativeCreaturePanel, Vector3.zero, Quaternion.identity);
            creaturePanel.transform.localScale = new Vector3(1, 1, 1);
            creaturePanel.transform.SetParent(initiativePanel.transform);
            creaturePanel.GetComponent<InitCreaturePanelScript>().SetImage(c.GetComponent<Creature>().GetSprite());

            Debug.Log(c.name + " init: " + c.GetComponent<Creature>().GetInit() + " Dex " + c.GetComponent<Creature>().Dex +
                " pos: " + Grid.WorldToCell(c.transform.localPosition));

            c.InitPanelGameObject = creaturePanel;
            c.SetPositionInGrid(Grid.WorldToCell(c.transform.localPosition));
        });

    }

    public List<Creature> GetCombatants() {
        return combatants;
    }
}
