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

public class HelicopterProcessor : MonoBehaviour
{
    public Helicopter heli = null;

    public readonly float SplooshRadius = 1.5f;
    public GameObject WaterBurstEffect = null;

    // Use this for initialization
    void Start()
    {

    }

    public bool HelicopterActive
    {
        get
        {
            return (Time.time + 3.0f) < heli.FinishTime;
        }
    }

    public void ReleaseHelicopter(Vector3 destination)
    {
        heli.DropPoint = destination;
        heli.StartTime = Time.time;
        heli.FinishTime = Time.time + 8.0f;
        heli.PreviousPosition = Vector3.zero;
        heli.PayloadDropped = false;

        Vector3 centerToDropPoint = destination.normalized;
        float outwarddistance = GetComponent<TreeFieldPopulator>().XVariance + GetComponent<TreeFieldPopulator>().ZVariance;
        float AttackAngle = Random.Range(20.0f, 45.0f);
        if (Random.value < 0.5f)
            AttackAngle = -AttackAngle;

        Vector3 vec0 = Quaternion.Euler(0.0f, AttackAngle, 0.0f) * centerToDropPoint;
        Vector3 vec3 = Quaternion.Euler(0.0f, -AttackAngle, 0.0f) * centerToDropPoint;

        Vector3 p0 = vec0 * outwarddistance;
        Vector3 p5 = vec3 * outwarddistance;

        Vector3 AttackLine = Vector3.Cross(centerToDropPoint, Vector3.up);

        float AttackDistance = Random.Range(4.0f, 6.0f);
        Vector3 p2 = destination + AttackLine * AttackDistance;
        Vector3 p3 = destination - AttackLine * AttackDistance;

        if (AttackAngle > 0.0f)
        {
            Vector3 tmp = p2;
            p2 = p3;
            p3 = tmp;
        }

        Vector3 p1 = p2 * 1.5f;
        Vector3 p4 = p5 * 1.5f;

        heli.Path1 = new CubicBezierCurve(p0, p1, p2, heli.DropPoint);
        heli.Path2 = new CubicBezierCurve(heli.DropPoint, p3, p4, p5);
    }

    void ReleasePayload()
    {
        Collider[] colliders = Tree.GetGameLogicObjectsInRange(heli.DropPoint, SplooshRadius);
        GameObject.Instantiate(WaterBurstEffect, heli.DropPoint, Quaternion.identity);

        foreach (Collider c in colliders)
        {
            Tree tree = c.GetComponent<Tree>();
            Combustible combust = c.GetComponent<Combustible>();


            if (combust != null)
            {
                combust.CurrentTemperature = 0.0f;
            }

        }
    }

    // Update is called once per frame
    void Update()
    {
        // Just process heli! It's the only helicopter in the game
        float t = (Time.time - heli.StartTime) / (heli.FinishTime - heli.StartTime);

        if (t < 1.0f)
        {
            if (t < 0.5f)
            {
                heli.transform.position = heli.Path1.CalculateCubicBezierPoint(t * 2.0f);
            }
            else
            {
                if (!heli.PayloadDropped)
                {
                    ReleasePayload();

                    // Register that we dropped the payload
                    heli.PayloadDropped = true;
                }
                heli.transform.position = heli.Path2.CalculateCubicBezierPoint((t - 0.5f) * 2.0f);
            }

            // Fix up our rotation
            Vector3 dir = (heli.transform.position - heli.PreviousPosition).normalized;
            heli.PreviousPosition = heli.transform.position;

            // Face along our movement
            heli.transform.rotation = Quaternion.LookRotation(dir, Vector3.up);
        }
    }
}