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

public class CombatSceneManager : BaseSceneManager {

    public enum damageType
    {
        Sail,
        Hull,
    }

    public GameObject hostileShip;
    public GameObject playerShip;
    public float shipSpeed;
    public Canvas victory;
    public EncounterMessage messageEncounter;

    private float hostileXStart = 30f;
    //private float hostileYStart = 4f;
    private float hostileXEnd = -35f;
    private bool playerDead = false;
    private bool hostileGone = false;

    private bool finished = false;

    float lastPlayerShot = 0f;
    float lastHostileShot = 0f;

    float curTime;

	// Use this for initialization
	void Start () {        
        StartCoroutine("HostileSailOn");
	}
	
	// Update is called once per frame
	void Update () 
    {
        // Combat logic does not begin until encounter has started
        if (encounterBegun)
        {
            // Check for Win conditions
            // check outrun
            if (hostileShip.transform.position.x <= hostileXEnd)
            {
                
                messageEncounter.ShowSceneWithText("You have succesfully evaded capture, and live to fight another day!");
                

                //Instantiate(victory, )
                if (Time.time - curTime > 20)
                {
                    hostileGone = true;
                    SceneManager.LoadScene("BookScene");
                }
                
                
                // send 'you outran the enemy' message
                // got to map screen
            }
            // check if sunk enemy ship 
            else if (hostileShip.GetComponent<HostileShip>().GetHullHealth() <= 0)
            {
                
                messageEncounter.ShowSceneWithText("You have succesfully your foes, and live to fight another day!");
                
                //Instantiate(victory, )
                if (Time.time - curTime > 20)
                {
                    hostileGone = true;
                    SceneManager.LoadScene("BookScene");
                }
                // send 'you defeated the enemy' Message
                // Go to Map screen
            }
            // check if the player is defeated
            else if (playerShip.GetComponent<PlayerShip>().GetHullHealth() <= 0)
            {
                Debug.Log("You died, you suck");
                playerDead = true;
                // send 'you sank, game over' message
                // Return to main menu
            }

            if (!playerDead && !hostileGone)
            {
                CombatnShit();
                MovementCalc();
            }
        }
	}

    private void CombatnShit()
    {
        float playerCannonCD = playerShip.GetComponent<PlayerShip>().curCannonCD;
        if (Time.time - lastPlayerShot >= playerCannonCD)
        {
            Debug.Log("Player Fires!");
            playerShip.GetComponent<PlayerShip>().DealDamage();
            lastPlayerShot = Time.time;
            
        }

        float hostileCannonCD = hostileShip.GetComponent<HostileShip>().cannonCD;
        if (Time.time - lastHostileShot >= hostileCannonCD)
        {
            Debug.Log("Enemy Fires!");
            hostileShip.GetComponent<HostileShip>().DealDamage();
            lastHostileShot = Time.time;
        }
    }

    private void MovementCalc()
    {
        float deltaSpeed = hostileShip.GetComponent<HostileShip>().GetCurSpeed() -
            playerShip.GetComponent<PlayerShip>().GetCurSpeed();

        if (hostileShip.transform.position.x <= hostileXStart)
            hostileShip.transform.Translate(new Vector3(deltaSpeed, 0.0f, 0.0f) * Time.deltaTime, Space.Self);
    }

    IEnumerator SinkHostile()
    {
        while (hostileShip.transform.position.y >= -20f)
        {
            // Move Ship towards start point
            hostileShip.transform.Translate(new Vector3(0.0f, -0.5f, 0.0f) * Time.deltaTime, Space.Self);
            curTime = Time.time;
            yield return null;
        }
    }

    IEnumerator HostileSailOn()
    {
        while(hostileShip.transform.position.x >= hostileXStart)
        {
            // Move Ship towards start point
            hostileShip.transform.Translate(new Vector3(shipSpeed, 0.0f, 0.0f) * Time.deltaTime, Space.Self);
            curTime = Time.time;
            yield return null;
        }
        // encounterBegun = true;
    }
}
