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

public class ShipPrototype : MonoBehaviour {
    [SerializeField] public Text textParams = null; // Set from editor
    [SerializeField] public ShipPrototype enemy = null; // Set from editor
    [SerializeField] public string shipName = "Player";
    [SerializeField] public Color shipColor = Color.blue;

    [SerializeField] public int armor = 100;
    [SerializeField] public int maxArmor = 100;

    [SerializeField] public int shield = 50;
    [SerializeField] public int maxShield = 50;

    [SerializeField] public int restoreShielPerTurn = 5;

    [SerializeField] public int heat = 0;
    [SerializeField] public int maxHeat = 10;
    [SerializeField] public int coolPerTurn = 2;

    [SerializeField] public int energy = 0;
    [SerializeField] public int energyPerTurn = 3;

    // protection
    [SerializeField] public int kineticProtection = 0;
    [SerializeField] public int laserProtection = 0;
    [SerializeField] public int missileProtection = 0;

    [SerializeField] WeaponPrototype[] weapons = new WeaponPrototype[] {
        new WeaponPrototype(WeaponType.Missile, "Rocket", 10),
        new WeaponPrototype(WeaponType.Kinetic, "Plasma", 10),
        new WeaponPrototype(WeaponType.Laser, "Laser", 10)
    };
    [SerializeField] public bool isAi = false;

    private BattleControllerPrototype battleController = null;

    internal void Init(BattleControllerPrototype battleControllerPrototype) {
        battleController = battleControllerPrototype;
    }

    public void StartTurn() {
        TextEffects.Show(Vector3.zero, shipName + " turn", shipColor);
        if (shield + restoreShielPerTurn > maxShield) {
            shield = maxShield;
        } else {
            shield += restoreShielPerTurn;
        }
        heat -= coolPerTurn;
        if (heat < 0) {
            heat = 0;
        }
        energy = energyPerTurn;
        UpdateParams();
        textParams.color = Color.white;
        if (isAi) {
            MakeAutoTurn();
        }
        hitCount = 0; // for effects
    }

    private void MakeAutoTurn() {
        for (int i = 0; i < 10; i++) {
            Timer.Add(i * 0.2f, UseRandomWeapon);
        }
        Timer.Add(2.2f, battleController.MakeTurn);
    }

    private void UseRandomWeapon() {
        int randomWeapon = UnityEngine.Random.Range(0, weapons.Length - 1);
        if (weapons[randomWeapon].IsCanFire(this)) {
            Fire(randomWeapon);
        }
    }

    public void Fire(int weaponId) {
        if (weapons != null && weaponId >= 0 && weaponId < weapons.Length && weapons[weaponId] != null) {
            weapons[weaponId].Fire(this, enemy);
        }
    }


    public void EndTurn() {
        energy = 0;
        textParams.color = Color.gray;
    }

    public void UpdateParams() {
        if (textParams != null) {
            textParams.text = GetParams();
        }
    }

    string GetParams() {
        return name + "\n" +
            "(HP) " + (armor + shield) + " " + "\n" + "(A) " + armor + " (S)" + shield + "\n" +
            "{E} " + energy + " (+" + energyPerTurn + ")" + "\n" +
            "{Heat} " + heat + " (-" + coolPerTurn + ")";
    }

    float hitCount = 0;
    internal void Hit(WeaponType type, int inputDamage) {

        hitCount += 1;
        var damage = inputDamage;
        switch (type) {
            case WeaponType.Kinetic:
                damage -= kineticProtection;
                break;
            case WeaponType.Laser:
                damage -= laserProtection;
                break;
            case WeaponType.Missile:
                damage -= missileProtection;
                break;
        }
        TextEffects.Show(transform.position + new Vector3(0, -hitCount * 0.2f, 0), damage.ToString(), Color.red);
        Debug.Log(name + " hit! " + " Damage " + damage + " of type " + type.ToString());
        int damageCount = damage;
        if (shield > damageCount) {
            shield -= damageCount;
            damageCount = 0;
        } else {
            damageCount = damageCount - shield;
            shield = 0;
        }
        armor -= damageCount;
        if (armor < 0) {
            Die();
        }
    }

    private void Die() {
        Debug.LogError(name + " ship is dead");
    }
}
