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

[SelectionBase]
public class LeafPhysics : MonoBehaviour {
    private Rigidbody body;
	// Use this for initialization
	void Start () {
        body = GetComponentInChildren<Rigidbody>();
        PlusX = new TweeningForce(0, 1, 0.5f, 2);
        MinusX = new TweeningForce(0, 1, 0.5f, 2);
        PlusZ = new TweeningForce(0, 1, 0.5f, 2);
        MinusZ = new TweeningForce(0, 1, 0.5f, 2);

        TorqueX = new TweeningForce(-1, 1, 0.5f, 2);
        TorqueZ = new TweeningForce(-1, 1, 0.5f, 2);
    }

    //public float TorqueForceUpScale = 0.3f;
    public float TorqueScale = 1.0f;
    public float TorqueBounds = 5.0f;
    public float TorqueSideScale = 1.0f;

    public float SideRestitution = 0.5f;
    //public float MaxForceUp = 1.0f;
    //
    //public float VelDownForceScale = 0.5f;
    //public float VelDownForceDivide = 3f;
    //
    //public float StartPushVelDown = 3.0f;
    //public float PushUpCurrent = 0;
    //public float PushUpForce = 10;
    //public float PushUpDecayRate = 1;

    public bool Debug = false;

    private TweeningForce PlusX;
    private TweeningForce MinusX;
    private TweeningForce PlusZ;
    private TweeningForce MinusZ;

    private TweeningForce TorqueX;
    private TweeningForce TorqueZ;

    private class TweeningForce
    {
        private float min, max, minMoveSpeed, maxMoveSpeed, current, target, speed;
        public float Current { get { return current; } }

        public TweeningForce(float min, float max, float minMoveSpeed, float maxMoveSpeed)
        {
            this.min = min;
            this.max = max;
            this.minMoveSpeed = minMoveSpeed;
            this.maxMoveSpeed = maxMoveSpeed;
            this.current = UnityEngine.Random.Range(min, max);
            UpdateTarget();
        }

        private void UpdateTarget()
        {
            this.target = UnityEngine.Random.Range(min, max);
            this.speed = UnityEngine.Random.Range(minMoveSpeed, maxMoveSpeed);
        }

        public void Update(float timeElapsed)
        {
            float moveAmount = speed * timeElapsed;
            if (moveAmount >= Math.Abs(current - target))
            {
                current = target;
                UpdateTarget();
            }
            else
            {
                float move = Math.Max(-moveAmount, Math.Min(moveAmount, target - current));
                current += move;
            }
        }
    }

    public float BaseWindForce = 10;
    public float AnglePow = 2;

    // Update is called once per frame
    void Update () {
        if (body.velocity.sqrMagnitude > 0.01f)
        {
            if (Debug)
            {
                bool bp = true;
            }

            float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y);

            body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + PlusX.Current), transform.position + Vector3.right * 0.4f);
            body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + MinusX.Current), transform.position - Vector3.right * 0.4f);
            body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + PlusZ.Current), transform.position + Vector3.forward * 0.4f);
            body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + MinusZ.Current), transform.position - Vector3.forward * 0.4f);

            /*
            if (body.velocity.y < -StartPushVelDown)
            {
                PushUpCurrent = PushUpForce;
            }

            body.AddForce(Vector3.up * PushUpCurrent);

            PushUpCurrent = Mathf.Max(0, PushUpCurrent - PushUpDecayRate * Time.deltaTime);
            */

            float torqueX = -transform.up.x;
            float torqueZ = -transform.up.z;

            float velYScale = Math.Max(0, -body.velocity.y);

            body.AddRelativeForce(new Vector3(torqueX * TorqueSideScale, 0, torqueZ * TorqueSideScale) * velYScale);
            //body.AddForce(Vector3.up * Mathf.Pow(Mathf.Max(-body.velocity.y / VelDownForceDivide), 2) * VelDownForceScale);
            body.AddRelativeTorque(new Vector3(Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueX * TorqueScale * 1.2f)) + TorqueX.Current * 0.5f, 0, Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueZ * TorqueScale)) + TorqueZ.Current * 0.5f) * velYScale);

            body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution);
        }
    }
}