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

public class PlayerControllerScript : MonoBehaviour {

    public float health;
    public bool invincible;

    public int playerState;
    public int roomNum;

    [SerializeField]
    float movementSpeed = 2f;

    float horizontal;
    float vertical;

    public GameObject player;

    Vector3 startPos;

    public GameObject camera;
    CameraControllerScript cameraScript;

    Rigidbody2D rb2d;
    Animator anim;

	// Use this for initialization
	void Start () {
        playerState = 0;
        startPos = this.transform.position;
        rb2d = player.GetComponent<Rigidbody2D>();
        anim = player.GetComponent<Animator>();
        anim.SetInteger("AnimCheck", 1);
    }

    private void Update()
    {
        if (horizontal > 0 || horizontal < 0)
        {
            anim.SetBool("Horizontal", true);
        }
        else
        {
            anim.SetBool("Horizontal", false);
        }

        if (vertical > 0 || vertical < 0)
        {
            anim.SetBool("Vertical", true);
        }
        else
        {
            anim.SetBool("Vertical", false);
        }

    }

    // Update is called once per frame
    void FixedUpdate () {
        horizontal = Input.GetAxisRaw("Horizontal");
        vertical = Input.GetAxisRaw("Vertical");

        float angle = (Mathf.Atan2(horizontal, vertical)) * Mathf.Rad2Deg;

        Vector2 playerMoveDir = new Vector2(horizontal, vertical);

        rb2d.velocity = playerMoveDir * movementSpeed;

        print(horizontal);
        

        //Vector3 playerMoveDir = Vector3(horizontal, vertical, 0);
        //player.transform.position += playerMoveDir * movementSpeed;

    }

    void UpdatePlayerSpeed()
    {
        switch(playerState)
        {
            case 0:
                movementSpeed = 2.0f;
                break;
            case 1:
                movementSpeed = 100.0f;
                break;
            case 2:
                movementSpeed = 4.0f;
                break;
            case 3:
                movementSpeed = 5.0f;
                break;
        }
    }

    public void TakeDamage(float damage)
    {
        health -= damage;
        StartCoroutine(InvincibleTimer());
    }

    IEnumerator InvincibleTimer()
    {
        invincible = true;
        yield return new WaitForSeconds(3);
        invincible = false;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "bodypart")
        {
            playerState++;
            anim.SetInteger("AnimCheck", 1);
            anim.SetInteger("PlayerState", playerState);
            Destroy(collision.gameObject);
        }
        if (collision.gameObject.tag == "up")
        {
            cameraScript.dir = CameraControllerScript.direction.up;
            cameraScript.MoveCamera();
        }
        if (collision.gameObject.tag == "down")
        {
            cameraScript.dir = CameraControllerScript.direction.down;
            cameraScript.MoveCamera();
        }
        if (collision.gameObject.tag == "left") 
        {
            cameraScript.dir = CameraControllerScript.direction.left;
            cameraScript.MoveCamera();
        }
        if (collision.gameObject.tag == "right") 
        {
            cameraScript.dir = CameraControllerScript.direction.right;
            cameraScript.MoveCamera();
        }
    }

    void OnTriggerEnter2D(Collider2D coll)
    {
        string trigName = coll.gameObject.name;
        Vector3 pos = new Vector3();

        switch (trigName)
        {
            case "Trans 1":
                if (roomNum == 1)
                {
                    pos = GameObject.Find("Room 2").transform.position;
                    roomNum = 2;
                }
                else
                {
                    pos = GameObject.Find("Room 1").transform.position;
                    roomNum = 1;
                }
                StartCoroutine(Lerp(camera, pos, 2));
                    break;
            case "Trans 2":
                if (roomNum == 2)
                {
                    pos = GameObject.Find("Room 3").transform.position;
                    roomNum = 3;
                }
                else
                {
                    pos = GameObject.Find("Room 2").transform.position;
                    roomNum = 2;
                }
                StartCoroutine(Lerp(camera, pos, 2));
                break;
            case "Trans 3":
                if (roomNum == 3)
                {
                    pos = GameObject.Find("Room 4").transform.position;
                    roomNum = 4;
                }
                else
                {
                    pos = GameObject.Find("Room 3").transform.position;
                    roomNum = 3;
                }
                StartCoroutine(Lerp(camera, pos, 2));
                break;
            case "Trans 4":
                if (roomNum == 3)
                {
                    pos = GameObject.Find("Room 5").transform.position;
                    roomNum = 5;
                }
                else
                {
                    pos = GameObject.Find("Room 4").transform.position;
                    roomNum =4;
                }
                StartCoroutine(Lerp(camera, pos, 2));
                break;

        }

        if (coll.gameObject.tag == "bodypart")
        {
            Destroy(coll.gameObject);
            playerState++;
            anim.SetInteger("AnimCheck", 1);
            anim.SetInteger("Player State", playerState);
            UpdatePlayerSpeed();
            
        }

    }

    IEnumerator Lerp(GameObject lerpObject, Vector3 finalPos, float timeTaken)
    {
        float currentTime = 0;

        while (currentTime < timeTaken)
        {
            currentTime += Time.deltaTime;
            lerpObject.transform.position =
                Vector3.Lerp(lerpObject.transform.position, finalPos, currentTime / timeTaken);
            yield return 0;
        }
    }
}
