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

public class TractorProcessor : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}

    public float TimeToUprootATree = 3.0f;
	
	// Update is called once per frame
	void Update () {
        Tractor[] tractors = FindObjectsOfType<Tractor>();
        foreach (Tractor t in tractors)
        {
            // check to see if you still have a valid target
            if (t.CurrentTarget == null || (!t.CurrentTarget.SelectedAsTractorTarget && t.CurrentTarget.burningState != BurnState.GettingChoppedDown))
            {
                // Find a new target
                t.CurrentTarget = TreeProcessor.FindClosestSelectedTreeTo(t.transform.position);
                if (t.CurrentTarget != null)
                    t.GetComponent<NavMeshAgent>().SetDestination(t.CurrentTarget.transform.position);
            }

            if (t.CurrentTarget != null)
            {
                if (t.CurrentTarget.burningState == BurnState.GettingChoppedDown)
                {
                    // check for chopping done
                    if (Time.time > t.CurrentTarget.TimeChoppingEnds)
                    {
                        // We're done chopping the tree
                        // Update State
                        t.CurrentTarget.burningState = BurnState.Disabled;

                        // Update visuals
                        t.CurrentTarget.transform.rotation = Quaternion.identity;
                        t.CurrentTarget.transform.Find("Trunk").gameObject.SetActive(false);
                        t.CurrentTarget.transform.Find("Stump").gameObject.SetActive(true);
                        t.CurrentTarget.GetComponent<CapsuleCollider>().enabled = false;
                        GetComponent<Minimap>().DrawTree(t.CurrentTarget);

                        t.CurrentTarget = null;
                    }
                    else
                    {
                        // Jitter
                        float timescalar = t.CurrentTarget.TimeChoppingEnds - Time.time;
                        if (timescalar < 1.0f)
                        {
                            // fall down
                           t.CurrentTarget.transform.rotation = Quaternion.Euler(0.0f, 0.0f, Mathf.Lerp(t.CurrentTarget.FallAngle, 0.0f, timescalar));
                        }
                        else
                        {
                            // Wiggle
                           t.CurrentTarget.transform.rotation = Quaternion.Euler(0.0f, 0.0f, (Mathf.PingPong(timescalar, t.CurrentTarget.WiggleSpeed) - t.CurrentTarget.WiggleSpeed / 2.0f) * t.CurrentTarget.WiggleAngle);
                        }
                    }
                }
                else if ((t.CurrentTarget.transform.position - t.transform.position).sqrMagnitude < 0.5f)
                {
                    // We are in range of our target
                    t.CurrentTarget.burningState = BurnState.GettingChoppedDown;

                    // Remove all the fuel
                    t.CurrentTarget.GetComponent<Combustible>().Fuel = 0.0f;
                    t.CurrentTarget.TimeChoppingEnds = Time.time + TimeToUprootATree;
                    t.CurrentTarget.FallAngle = (Random.value < 0.5f) ? -90.0f : 90.0f;
                    t.CurrentTarget.WiggleSpeed = Random.Range(0.15f, 0.25f);
                    t.CurrentTarget.WiggleAngle = Random.Range(30.0f, 50.0f);
                }
            }

        }
	}
}
