using UnityEngine;
using System.Collections;

public class ArmFollowScript : MonoBehaviour {

    [SerializeField]
    private float _armDistance;
    [SerializeField]
    private float _verticalOffset;
    [SerializeField]
    private GameObject _swordArm;
    [SerializeField]
    private GameObject _offArm;

    //private Vector2 mousePos;
    //Vector2 previousMousePosition;
    private bool _usingSword = true;
    private Rigidbody2D _rBody;

    void Awake() {
        this._rBody = this.GetComponent<Rigidbody2D>();
        //this.mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        //this.previousMousePosition = this.mousePos;
    }

    void Update() {
        //mousePos = mousePos + ((Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - previousMousePosition );// applying the movement of the real mouse to musePos
        //previousMousePosition = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 ourPos = this._rBody.position;
        Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 vectorFromShaToMouse = mousePos - ourPos;
        //move mousePos by a vector, equal to the vector the real mouse moved, but allows us to reposition it, to avoid instant repositioning of the arms. 
        vectorFromShaToMouse = (vectorFromShaToMouse.normalized * this._armDistance);
        Vector2 tmpVect = new Vector2(vectorFromShaToMouse.x, vectorFromShaToMouse.y + this._verticalOffset);

        float directionAngle = Mathf.Atan2(vectorFromShaToMouse.y, vectorFromShaToMouse.x) * Mathf.Rad2Deg;

        if (this._usingSword) {
            this._swordArm.transform.localPosition = tmpVect;
            this._swordArm.transform.rotation = Quaternion.AngleAxis(directionAngle + 90, Vector3.forward);
        } else {
            this._offArm.transform.localPosition = tmpVect;
            this._offArm.transform.rotation = Quaternion.AngleAxis(directionAngle + 90, Vector3.forward);
        }
    }

    public bool UseSword {
        get {
            return this._usingSword;
        }
        set {
            this._usingSword = value;
            /*if (value) {
                mousePos = this._swordArm.transform.position;
            }else{
                mousePos = this._offArm.transform.position;
            }*/
        }
    }
}
