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

public class Creature : MonoBehaviour {

    [SerializeField] Sprite creatureImage;
    [SerializeField] bool humanControlled;

    private int init;
    private int dex = 0;
    private int creatureHealth;
    private List<IAction> actions = new List<IAction>();

    internal int CurrentHealth { get; set; }
    internal int MaxHealth { get; set; }


    Weapon weapon;
    int health;
    private CanvasGroup canvasGroup;
    private RectTransform rectTransform;
    private Vector3Int currentPos;

    internal IAction GetFirstAction() {
        return firstAction;
    }

    private IAction firstAction;
    private IAction secondAction;
    public GameObject InitPanelGameObject { set; get; }

    internal int movementRate;

    public int Dex { get => dex; set => dex = value; }

    public int Init { get => init; set => init = value; }

    public int CreatureHealth { get => creatureHealth; set => creatureHealth = value; }
    public bool IsCreatureAlive { get; set; }
    public Vector3Int CurrentPos { get => currentPos; set => currentPos = value; }

    private void Start() {
        canvasGroup = GetComponent<CanvasGroup>();
        rectTransform = GetComponent<RectTransform>();
    }

    public int GetInit() {
        return Init;
    }

    internal bool HasSecondAction() {
        return secondAction != null;
    }

    public void RollInit() {
        Init = Random.Range(0, 100 - dex);
    }

    public Sprite GetSprite() {
        return creatureImage;
    }

    public bool IsHumanControlled() {
        return humanControlled;
    }

    public void SetFirstAction(IAction action) {
        firstAction = action;
    }

    public void SetSecondAction(IAction action) {
        secondAction = action;
    }

    internal List<Creature> EnemiesInSquareNextToCreature(Creature creature, List<Creature> combatants) {
        List<Creature> enemiesClose = new List<Creature>();
        foreach (Creature c in combatants) {
            Vector3 distance = c.CurrentPos - creature.CurrentPos;
            if (c.name != creature.name && distance.x <= 1 && distance.x >= -1 && distance.y <= 1 && distance.y >= -1) {
                enemiesClose.Add(c);
            }
        }
        return enemiesClose;
    }

    internal void SetPositionInGrid(Vector3Int vector3Int) {
        CurrentPos = vector3Int;
    }

    public bool HasFirstAction() {
        return firstAction != null;
    }

    internal Creature FindClosestEnemy(Creature creature, List<Creature> combatants) {
        Creature closestEnemy = null;
        float minDist = Mathf.Infinity;
        List<Creature> enemies = combatants.FindAll(p => p.IsHumanControlled() != creature.IsHumanControlled());

        foreach (Creature e in enemies) {
            float dist = Vector3.Distance(e.CurrentPos, creature.CurrentPos);
            if (dist < minDist) {
                closestEnemy = e;
                minDist = dist;
            }
        }
        Debug.Log("Closest enemy is " + closestEnemy.name);
        return closestEnemy;
    }

    public void Death() {
        IsCreatureAlive = false;
    }


    public void TakeDamage(int damage) {
        CurrentHealth -= damage;
        if (CurrentHealth <= 0) {
            Death();
        }

        InitPanelGameObject.GetComponent<InitCreaturePanelScript>().UpdateHealthBar(MaxHealth / CurrentHealth);
    }

}
