using UnityEngine;
using System.Collections;

public class PlayerMousePicker : MonoBehaviour {


    PlayerObject m_playerObj;

	// Use this for initialization
	void Start () 
    {
        m_wasMouseDown = false;
        //m_mouseDownHit = false;

        m_playerObj = GetComponent<PlayerObject>();
	}

    const int HIGHLIGHT_MASK = (1 << 8);
    const int ENEMY_MASK = (1 << 9);

    private bool m_wasMouseDown;
    //private bool m_mouseDownHit;

	// Update is called once per frame
	void Update ()
    {
        if (Input.GetMouseButton(0))
        {
            if (m_wasMouseDown == false)
            {
                //m_mouseDownHit = false;
                m_wasMouseDown = true;

                RaycastHit hitInfo;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hitInfo, 1000.0f, HIGHLIGHT_MASK))
                {
                    m_playerObj.MouseDown(hitInfo.transform);
                    //m_mouseDownHit = true;
                }
                else
                {
                    m_playerObj.MouseDownNoHits();
                }

                if (Physics.Raycast(ray, out hitInfo, 1000.0f, ENEMY_MASK))
                {
                    CharacterObject charObj = hitInfo.transform.GetComponent<CharacterObject>();
                    if ( charObj != null )
                    {
                        if ( charObj.IsPlayer == false )
                        {
                            // Enemy!
                            CharacterStatsGui.SetEnemy(charObj.transform);
                        }
                    }
                }
            }
        }
        else if ( m_wasMouseDown )
        {
            // Not needed. Playerobj handles it
            //if (m_mouseDownHit)
            {
                m_playerObj.MouseUp();
            }
            m_wasMouseDown = false;
        }
	}
}
