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

public class EntityStats {
    
    public EntityStatsData data;

    //Stats
    public bool isDead = false;
    public bool useShield = false;

    //Base
    public float maxHealth = 1;
    public float health = 1;
    public float healthRegen = 1;
    public float maxEnergy = 1;
    public float energy = 1;
    public float energyRegen = 1;
    public float armor = 1;

    public int agility = 1;
    public int strength = 1;
    public int intelligence = 1;
    public int endurance = 1;

    public int meeleArts = 1;
    public int throwArts = 1;
    public int shotArts = 1;
    public int blockArts = 1;

    public float weight = 0;

    //Bonuses
    public int healthBonus = 0;
    public int healthRegenBonus = 0;
    public int energyBonus = 0;
    public int armorBonus = 0;
    public int energyRegenBonus = 0;

    public int agilityBonus = 0;
    public int strengthBonus = 0;
    public int intelligenceBonus = 0;
    public int enduranceBonus = 0;

    public int meeleArtsBonus = 0;
    public int throwArtsBonus = 0;
    public int shotArtsBonus = 0;
    public int blockArtsBonus = 0;

    public float speedBonus = 0;
    public float jumpPowerBonus = 0;

    //Modifiers
    public float shieldArmor = 0;
    public float shieldEnergy = 0;

    //Static
    readonly float weightPerStrength = 30.0f;
    readonly float speedPerAgility = 0.2f;
    readonly float baseSpeed = 2.0f;
    readonly float baseWeight = 100.0f;
    readonly float damagePerStrength = 0.2f;

    public float BlockDamage(float amount) {
        if(energy >= (shieldEnergy * amount)) {
            energy = energy - shieldEnergy* amount;
            return amount - shieldArmor;
        }
        else {
            energy = 0.0f;
            return amount - shieldArmor * 0.3f;
        }
    }

    public void Hurt(float amount) {
        if(isDead) return;

        if(useShield) amount = BlockDamage(amount);
        amount = amount - (armor + armorBonus);

        if(amount < 0.0f) return;

        health -= amount ;
        if(health <= 0.0f) {
            health = 0.0f;
            isDead = true;
        }
    }

    public void Update(float time) {
        if(isDead) return;

        health += (healthRegen + healthRegenBonus) * time;
        energy += (energyRegen + energyRegenBonus) * time;

        if(health >= (maxHealth + healthBonus)) {
            health = maxHealth + healthBonus;
        }

        if(energy >= (maxEnergy + energyBonus)) {
            energy = maxEnergy + energyBonus;
        }
    }

    static float speedBuf;
    static float weightBuf;

    public float GetSpeed() {
        speedBuf = (baseSpeed + speedPerAgility * agility);
        weightBuf = baseWeight + weightPerStrength * strength;

        if(weight > weightBuf) {
            speedBuf = speedBuf / (weight/weightBuf);
        }

        return speedBuf;
    }

    public float GetMeeleDamage(float baseDmg) {
        return baseDmg + baseDmg * (strength * damagePerStrength);
    }

    public EntityStats() {

    }

    public EntityStats(EntityStatsData data) {
        this.data = data;

        maxHealth   = data.maxHealth   ;
        healthRegen = data.healthRegen ;
                     
        maxEnergy   = data.maxEnergy   ;
        energyRegen = data.energyRegen ;
                      
        armor       = data.armor       ;
                     
        agility     = data.agility     ;
        strength    = data.strength    ;
        intelligence= data.intelligence;
        endurance   = data.endurance   ;
                      
        meeleArts   = data.meeleArts   ;
        throwArts   = data.throwArts   ;
        shotArts    = data.shotArts    ;
        blockArts   = data.blockArts   ;
    }
}
