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

public class PlayerShip : Ship {

    public GameObject hostileShip;
    public float curCannonCD;
	public BaseRoom[] rooms;
    //public float criticalDamageThreshold;

	// Use this for initialization
	void Start () {
        hullHealth = maxHullHealth;
        sailHealth = maxSailHealth;
        curSpeed = baseSpeed;
        curCannonCD = cannonCD;
	}
	
	// Update is called once per frame
	void Update () 
    {  
        // TO DO...
        // current cannon cd is base cooldown,
        // must be dependent on how many crew working the cannons
	
	}

    public override 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
                hostileShip.GetComponent<HostileShip>().TakeDamage(CombatSceneManager.damageType.Sail, cannonDamage);
                Debug.Log("Player Hits Sail!");
            }
            else
            {
                // Odd roll - Hit hull
                hostileShip.GetComponent<HostileShip>().TakeDamage(CombatSceneManager.damageType.Hull, cannonDamage);
                Debug.Log("Player Hits Hull!");
            }
        }
    }

    protected override void UpdateSpeed()
    {
        // TO DO...
        // Speed determined by crew in the rigging
        // capped at zero if we have nobody at the helm
        //
        base.UpdateSpeed();

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

    public override void TakeDamage(CombatSceneManager.damageType dmgType, float damage)
    {
        if (dmgType == CombatSceneManager.damageType.Hull)
        {
            hullHealth -= damage;
			var room = rooms [Random.Range (0, rooms.Length - 1)];
			Debug.Log ("The " + room.name + " room took " + damage + " damage!");
			room.TakeDamage (Mathf.RoundToInt(damage));
        }
        else if (dmgType == CombatSceneManager.damageType.Sail)
        {
            sailHealth -= damage;
            UpdateSpeed();
        }
        else
            Debug.Log("Unrecognised damage type: " + dmgType.ToString());
    }

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