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

public class Ship : MonoBehaviour {

    public float maxSailHealth;
    public int baseAccuracy; // xxx/100    
    public float cannonDamage;
    public float maxHullHealth;
    public float baseSpeed;
    public float cannonCD;

    [SerializeField] protected float hullHealth;
    [SerializeField] protected float sailHealth;
    [SerializeField] protected float curSpeed;

    public float GetHullHealth()
    { return hullHealth; }
    public float GetCurSpeed()
    { return curSpeed; }

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    public virtual void DealDamage()
    {
        int hitRoll = UnityEngine.Random.Range(1, 100);
        // if hitRoll <= accuracy, target is hit.
        if (hitRoll <= baseAccuracy)
        {
            if (hitRoll % 2 == 0)
            {
                // even roll - hit sail

            }
            else
            {
                // Odd roll - Hit hull

            }
        }
    }

	public virtual void TakeDamage(CombatSceneManager.damageType dmgType, float damage)
    {
        if (dmgType == CombatSceneManager.damageType.Hull)
        {
            hullHealth -= damage;
        }
        else if (dmgType == CombatSceneManager.damageType.Sail)
        {
            // sails take half damage - cause reasons
            sailHealth -= damage/2;
            UpdateSpeed();
        }
        else
            Debug.Log("Unrecognised damage type: " + dmgType.ToString());
    }

    protected virtual void UpdateSpeed()
    {
        //Debug.Log("Should be ovverridden");

        float percentSailEfficiency = (sailHealth / maxSailHealth) * 100;
        Debug.Log("Sail health %: " + percentSailEfficiency);
        curSpeed = (baseSpeed / 100) * percentSailEfficiency;
        Debug.Log("New ships speed: " + curSpeed);
    }
}
