using UnityEngine;
using System.Collections;

public class PlayerShapeInput : MonoBehaviour {

    /*  COMMENTS OUTDATED, i've experimented a lot these aren't accurate anymore
     * Player Input Controls
     * 
     * Player can move his shape
     * Player can rotate his shape
     * 
     * Holding left click says move
     * Holding right click says rotate
     * Holding both if fine
     * 
     * MOVE:
     * When left click is down, shape moves the mouse deltaPos w/ some max speed
     * Must allow for slower movement, not a static direction, so it has magnitude from 0..max
     * 
     * ROTATE:
     * When right click is down, shape rotates with movement
     * Right and/or Down are clockwise
     * Left and/or Up are counter-clockwise
     * We take dx + dy, cap it by some max
     * Apply that as rotation
     * 
     * Concerns:
     * - What about the edge of the screen, where mouse will stop moving?
     * - What do we do with the visible cursor, discourage player to "grab and drag" shape, he can click anywhere
     * - What if the shape can't move through something, will input feel disconnected?
     * 
     * This is the first file I'm writing in LD35 compo
     * 
     */

    public float MaxRotate;
    public float MaxMoveMagnitude;
    public float Sensitivity_Move=1.0f;
    public float Sensitivity_Rotate = 10f;


    private bool leftDown = false;
    private bool rightDown = false;

    private Vector3 lastMousePos;
    private ShapeMovementController mover;

	void Start () {
        mover = GetComponent<ShapeMovementController>();
	}
	
	void UpdateOLD () {
        // no click = rotate
        // left click = move
        // right click = both

        if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
        {
            //Vector2 shift = (Vector2)(Input.mousePosition - lastMousePos) / Sensitivity_ScreenToWorldUnits;
            
            Vector2 shift = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
            //if (shift.magnitude != 0)
            //    Debug.Log("INPUT: " + shift.magnitude + " clipped to " + Mathf.Min(shift.magnitude, MaxMoveMagnitude));
            shift = shift.normalized * Mathf.Min(shift.magnitude, MaxMoveMagnitude) * Sensitivity_Move;

            mover.Move = shift;
        }
        else
        {
            mover.Move = Vector2.zero;
        }

        if (!Input.GetMouseButton(0) || Input.GetMouseButton(1))
        {
            // + is counter
            // left/up counter (-x, +y)

            // - is clock
            // right/down clock (+x, -y)
            //Vector2 d = (Vector2)(Input.mousePosition - lastMousePos);
            //Vector2 d = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")) * Sensitivity_Rotate;
            float rot = -Input.GetAxis("Mouse X"); //- d.x + d.y;
            rot = Mathf.Clamp(rot, -MaxRotate, MaxRotate) * Sensitivity_Rotate;
            mover.Rotate = rot;
        }
        else
        {
            mover.Rotate = 0;
        }

        lastMousePos = Input.mousePosition;
	}

    void Update()
    {

        if (Input.GetMouseButton(0))
        {
            mover.Move = Vector2.zero;
            Recenter();
        }
        else
        {
            DoMove();
            DoRotate();
        }
    }

    private void DoRotate()
    {
        float rot = -Input.GetAxis("Mouse X"); //- d.x + d.y;
        rot = Mathf.Clamp(rot, -MaxRotate, MaxRotate) * Sensitivity_Rotate;
        mover.Rotate = rot;
    }

    private void DoMove()
    {
        Vector2 shift = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
        shift = shift.normalized * Mathf.Min(shift.magnitude, MaxMoveMagnitude) * Sensitivity_Move;
        mover.Move = shift;
    }

    private void Recenter()
    {
        mover.RecenterTrigger = true;
    }

}
