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

public class Fight : MonoBehaviour
{
    GameObject tile;
    GameObject enemy;
    GameObject player;
    System.Random rand = new System.Random();
    UIHandler uihandler;

    public void startFight(GameObject tile, GameObject enemy, GameObject player)
    {
        this.tile = tile;
        this.enemy = enemy;
        this.player = player;
        enemy.GetComponent<Enemy>().scaleEnemy(player.GetComponent<Player>());
        enemy.transform.rotation = player.transform.rotation;

        uihandler = GameObject.Find("UIHandler").GetComponent<UIHandler>();
        uihandler.openFight();
        uihandler.updateFightInterface(enemy, player);
    }

    private void endFight()
    {
        uihandler.closeFight();
    }

    public void playerAction(int index)
    {
        int playerDamage = 0;
        int skillnumber = -1;
        switch (index)
        {
            case 1:
                endFight();
                uihandler.showMessage("INFORMATION;You escaped");
                break;
            case 2:
                //User waits
                break;
            case 3:
                playerDamage = player.GetComponent<Player>().calculateDamage();
                break;
            case 4:
                skillnumber = 0;
                break;
            case 5:
                skillnumber = 1;
                break;
            case 6:
                skillnumber = 2;
                break;
        }
        if (skillnumber != -1)
        {
            playerDamage = player.GetComponent<Player>().castSkill(skillnumber);
        }
        player.GetComponent<Player>().reduceCooldown(skillnumber);
        player.GetComponent<Player>().regainSecondary(2);
        bool isDead = enemy.GetComponent<Enemy>().takeDamage(playerDamage);
        if (isDead)
        {
            endFight();
            tile.GetComponent<Tile>().enemyKilled(enemy);
            player.GetComponent<Player>().enemyKilled();
            player.GetComponent<Player>().gainExperience(enemy.GetComponent<Enemy>().getExperience());
        }
        else
        {
            if (index != 1)
            {
                enemyAction();
                uihandler.updateFightInterface(enemy, player);
            }
        }
    }

    public void enemyAction()
    {
        int enemyDamage = -1;
        // { health, maxHealth, secondary, maxSecondary, strength, dexterity, intelligence };
        int[] enemyStats = enemy.GetComponent<Enemy>().getStats();
        int index = rand.Next(0,3);
        if (enemyStats[0] <= enemyStats[1] / 4 && index != 0)
        {
            enemyDamage = secondChance();
            if (enemyDamage != 0)
            {
                enemyDamage = secondChance();
            }
        }
        else if (enemyStats[0] <= enemyStats[1] / 2 && index != 0)
        {
            enemyDamage = secondChance();
        }

        if (index == 2 && enemyStats[2] <= 0)
        {
            index = 1;
        }

        if (enemyDamage == 0 || index == 0)
        {
            endFight();
            uihandler.showMessage("INFORMATION;The enemy escaped!");
        }
        else
        {
            switch (index)
            {
                case 1:
                    enemyDamage = enemy.GetComponent<Enemy>().calculateDamage();
                    break;
                case 2:
                    enemyDamage = enemy.GetComponent<Enemy>().calculateHeavy();
                    break;
            }
        }

        if (player.GetComponent<Player>().takeDamage(enemyDamage))
        {
            uihandler.showDeathScreen();
        }
    }

    private int secondChance()
    {
        int result = rand.Next(0, 3);
        return result;
    }
}
