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

public class PlayerControl : MonoBehaviour {

	public float speed = 6f;
	Vector2 movement;
	public Rigidbody2D playerRigidbody;
	public GameObject BustBox;
	public Rigidbody2D bustBoxRb;
	public GameObject left;
	public GameObject right;
	public GameObject up;
    public float maxspeed;

    bool atkEnable = true, atkcooldown = false;
    float atkcount;
	int status;

	void Awake(){
		playerRigidbody = GetComponent <Rigidbody2D> ();	
		bustBoxRb = BustBox.GetComponent<Rigidbody2D> ();
        maxspeed = 8;
	}
	void Start () {
		status = 0;

	}

	void Update()
	{
		Bust ();

	}

	void FixedUpdate()
	{
		float h = Input.GetAxisRaw ("Horizontal");
		float v = Input.GetAxisRaw ("Vertical");
		Move (h, v);
		Animate ();
	}
	void Move(float h,float v){
		movement.Set (h, v);
		playerRigidbody.AddForce (movement * speed);
		if (movement.x == 0) {
			playerRigidbody.velocity = new Vector2 (0,playerRigidbody.velocity.y);}
		if (movement.y == 0) {
			playerRigidbody.velocity = new Vector2 (playerRigidbody.velocity.x,0);}

        if (playerRigidbody.velocity.x >= maxspeed)
        {
            
            playerRigidbody.velocity = new Vector2( maxspeed, playerRigidbody.velocity.y);
        }
        if (  playerRigidbody.velocity.x <= -maxspeed)
        {
            playerRigidbody.velocity = new Vector2(-maxspeed, playerRigidbody.velocity.y);
        }

        if (playerRigidbody.velocity.y >= maxspeed)
        {
            playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, maxspeed);
        }
        if ( playerRigidbody.velocity.y <= -maxspeed)
        {
            playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, -maxspeed);
        }

    }

	void Animate(){
		if(Input.GetKeyDown(KeyCode.DownArrow)){
			//playerRigidbody.AddForce (new Vector2 (0f, -20.0f * speed));
			up.GetComponent<SpriteRenderer> ().enabled = true;
			left.SetActive (false);
			right.SetActive (false);
			status = 0;
		}

		if (Input.GetKeyDown (KeyCode.UpArrow)) {
			//playerRigidbody.AddForce(new Vector2(0f,20.0f * speed));
			up.GetComponent<SpriteRenderer> ().enabled = true;
			left.SetActive (false);
			right.SetActive (false);
			status = 1;
		}

		if(Input.GetKeyDown(KeyCode.LeftArrow)){
			//playerRigidbody.AddForce(new Vector2(-20.0f * speed,0f));
			left.SetActive (true);
			up.GetComponent<SpriteRenderer> ().enabled = false;
			right.SetActive (false);
			status = 2;
		}

		if(Input.GetKeyDown(KeyCode.RightArrow)){
			//playerRigidbody.AddForce (new Vector2 (20.0f * speed, 0f));
			right.SetActive (true);
			up.GetComponent<SpriteRenderer> ().enabled = false;
			left.SetActive (false);
			status = 3;
		}
	}

	void Bust(){
		if (Input.GetKeyDown (KeyCode.Space) && atkEnable) {
            atkEnable = false;
            atkcooldown = true;
			BustBox.SetActive (true);
			BustBox.GetComponent<HitBox> ().countHit = 0;
            Event_System.instance.PlayeSound(6, 0.5f);
            switch (status) {
			case 0:
				bustBoxRb.transform.position = new Vector2 (bustBoxRb.transform.position.x, this.gameObject.GetComponent<Transform>().position.y -1);
				break;
			case 1:
				bustBoxRb.transform.position = new Vector2 (bustBoxRb.transform.position.x,this.gameObject.GetComponent<Transform>().position.y+1);
				break;
			case 2:
				bustBoxRb.transform.position = new Vector2 (this.gameObject.GetComponent<Transform>().position.x-1, bustBoxRb.transform.position.y);
				break;
			case 3:
				bustBoxRb.transform.position = new Vector2 (this.gameObject.GetComponent<Transform>().position.x+1, bustBoxRb.transform.position.y);
				break;
			}

		}

        if (atkcooldown)
        {
            atkcount += Time.deltaTime;
            Debug.Log(atkcount);
        }

		if (atkcount>=0.1 && atkcooldown) {
            atkcooldown = false;
            atkcount = 0;

            BustBox.SetActive (false);
			bustBoxRb.transform.position = new Vector2 (this.gameObject.GetComponent<Transform>().position.x, this.gameObject.GetComponent<Transform>().position.y);
        }
        if (Input.GetKeyUp(KeyCode.Space) && !atkEnable)
        {
            atkEnable = true;
         }
    }
}
