using UnityEngine;
using System.Collections;

using HamJam.Character;

/// <summary>
/// The class that deals with parsing all user input. The class
/// is designed to be attached as a script to an empty game object
/// </summary>
public class InputManagerScript : MonoBehaviour {

    [SerializeField]
    private string _playerTag;
    private GameObject _playerGameObj;
    private CharacterInteractionScript _playerController;
    private ArmFollowScript _playerArmController;
    private Rigidbody2D _playerRBody;
    [SerializeField]
    private PauseMenuScript _pauseMenu;
    [SerializeField]
    private GameObject _crawlerRune;
    [SerializeField]
     private GameObject _spiritRune;
    [SerializeField]
     private GameObject _spitterRune;
    [SerializeField]
     private GameObject _tarTile;
    [SerializeField]
    private GameObject _lavaTile;



    void Awake() {
        this._playerGameObj = GameObject.FindGameObjectWithTag(this._playerTag);
        this._playerController = this._playerGameObj.GetComponent<CharacterInteractionScript>();
        this._playerRBody = this._playerGameObj.GetComponent<Rigidbody2D>();
        this._playerArmController = this._playerGameObj.GetComponent<ArmFollowScript>();
    }

    // Update is called once per frame
    void Update() {
        Vector2 movementDirection = new Vector2();
        if (!_pauseMenu.paused()) {
            if (Input.GetKey(KeyCode.W)) {
                movementDirection.y += 1;
            }
            if (Input.GetKey(KeyCode.A)) {
                movementDirection.x -= 1;
            }
            if (Input.GetKey(KeyCode.S)) {
                movementDirection.y -= 1;
            }
            if (Input.GetKey(KeyCode.D)) {
                movementDirection.x += 1;
            }
            if (Vector2.zero != movementDirection) {
                this._playerController.Move(movementDirection);
            }


            //Building Base
            Vector2 spawnpoint = new Vector2(this.transform.position.x + Random.value, this.transform.position.y + Random.value);
            if (Input.GetKeyDown(KeyCode.Alpha1)) {
                Instantiate(_crawlerRune, spawnpoint, Quaternion.identity, this.transform);
            } else if (Input.GetKeyDown(KeyCode.Alpha2)) {
                Instantiate(_spiritRune, spawnpoint, Quaternion.identity, this.transform);
            } else if (Input.GetKeyDown(KeyCode.Alpha3)) {
                Instantiate(_spitterRune, spawnpoint, Quaternion.identity, this.transform);
            } else if (Input.GetKeyDown(KeyCode.Alpha4)) {
                Instantiate(_tarTile, spawnpoint, Quaternion.identity, this.transform);
            } else if (Input.GetKeyDown(KeyCode.Alpha5)) {
                Instantiate(_lavaTile, spawnpoint, Quaternion.identity, this.transform);
            }




            // Look towards the mouse
            Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 playerPos = this._playerRBody.position;
            Vector2 vecFromPlayerToMouse = mousePos - playerPos;
            if (Vector2.zero != vecFromPlayerToMouse) {
                this._playerController.Look(vecFromPlayerToMouse);
            }

            if (Input.GetMouseButtonDown(0)) {
                this._playerController.PerformAttack(vecFromPlayerToMouse, AttackTypes.RaidBossFireBarrage);
            }
            //When weapon is switched, reposition virtual mouse
            if (Input.GetMouseButtonDown(1)) {
                this._playerArmController.UseSword = (!this._playerArmController.UseSword);
            }
        }
	}
}
