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

public class RoundManager : MonoBehaviour {

    [SerializeField] Button executeRoundButton;
    private List<Creature> combatants;
    int round;

    private bool executing = false;
    private Creature activeCreature;
    [SerializeField] GameObject decitionButtonsPanel;
    [SerializeField] Button moveActionButton;
    [SerializeField] Button attackActionButton;
    private bool movingAction;
    private List<Vector3> highlightedTiles;

    private void Start() {
        executeRoundButton.onClick.AddListener(ExecuteRound);
        moveActionButton.onClick.AddListener(CreateMoveAction);
        attackActionButton.onClick.AddListener(CreateAttackAction);
    }

    private void CreateAttackAction() {
    }

    private void CreateMoveAction() {
        movingAction = true;
        MoveAction ma = new MoveAction();
        ma.SetStartPosition(activeCreature.transform.position);
        if (!activeCreature.HasFirstAction()) {
            activeCreature.SetFirstAction(ma);
        } else {
            activeCreature.SetSecondAction(ma);
        }
        GameManagerScript.GetInstance().Grid.GetComponent<GridController>().HilightAction = ma;
    }

    private void ExecuteRound() {
        executing = true;
        executeRoundButton.interactable = false;
        foreach (Creature c in combatants) {
            StartCoroutine(c.GetFirstAction().PerformAction());
        }
        round++;
    }

    private void FixedUpdate() {
        if (executing) {
            foreach (Creature c in combatants) {
                c.CurrentPos = Vector3Int.RoundToInt(c.gameObject.transform.position);
            }
        }

        if (movingAction) {
            LineRenderer lineRenderer = GameManagerScript.GetInstance().GetLineRenderer();

            lineRenderer.startWidth = 0.01f;
            lineRenderer.endWidth = 0.01f;

            Vector3 mousePos = Input.mousePosition;
            mousePos.z = Camera.main.nearClipPlane;
            Vector3 realMousePos = Camera.main.ScreenToWorldPoint(mousePos);

            lineRenderer.SetPosition(0, activeCreature.transform.position);
            lineRenderer.SetPosition(1, realMousePos);
        }
    }

    internal void StartFirstRound(List<Creature> combatants) {
        this.combatants = combatants;
        round = 1;
        MakeDecisions();
    }

    internal void SetupNextRound() {
        this.combatants = combatants.FindAll(c => c.IsCreatureAlive);
        MakeDecisions();
    }

    private void MakeDecisions() {
        foreach (Creature creature in combatants) {
            if (creature.IsHumanControlled()) {
                CameraManager.GetInstance().FocusOnGameObject(creature.gameObject);

                activeCreature = creature;
                StartCoroutine(creature.WaitForPlayerAction());
                // Wait for human decision
            } else {
                creature.DecideActions(combatants);
                // AI make decision
            }
        }


    }

    private void Update() {
        if (CheckIfAllCombatantsHaveTheirActions()) {
            executeRoundButton.interactable = true;
        }

        if (activeCreature != null && activeCreature.IsHumanControlled()) {
            decitionButtonsPanel.SetActive(true);
        } else {
            decitionButtonsPanel.SetActive(false);
        }
    }

    private bool CheckIfAllCombatantsHaveTheirActions() {
        return combatants != null && combatants.All(c => c.HasFirstAction() && c.HasSecondAction());
    }
}
