using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;


public class Player_control : MonoBehaviour
{

    //Player Setup
    static public float score;
    public float speed = 1;
    public float jump_power = 1;
    static public bool Is_Hidden = false;

    private bool look_right = true;
    private bool onGround;
    private bool is_jumping; // false = stop jump animation

    AudioSource audio;
    Animator animator;

    public Text countText;

    // Use this for initialization
    void Start()
    {
        animator = GetComponent<Animator>();
        audio = GetComponent<AudioSource>();
        score = 0;
    }

    // Update is called once per frame
    void Update()
    {
        controller_input();
        SetCountText();
    }

    void controller_input()
    {

        // Walk
        if (Input.GetKey(KeyCode.RightArrow) | Input.GetAxis("Horizontal") > 0)
        {
            if (look_right == false) { transform.Rotate(0, 180, 0); }
            look_right = true;
            animator.SetBool("walk", true);
            transform.Translate(Vector3.right * speed * Time.deltaTime);
        }

        else if (Input.GetKey(KeyCode.LeftArrow) | Input.GetAxis("Horizontal") < 0)
        {
            if (look_right == true) { transform.Rotate(0, 180, 0); }
            look_right = false;
            animator.SetBool("walk", true);
            transform.Translate(Vector3.left * speed * Time.deltaTime * -1);
        }

        else
        {
            animator.SetBool("walk", false);
        }


        // Jump
        if (Input.GetKeyDown("space") & onGround == true | Input.GetButtonDown("Jump") & onGround == true)
        {
            audio.Play();
            GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jump_power), ForceMode2D.Impulse);
            animator.SetBool("jump", true);
            is_jumping = true;
        }
        else if (is_jumping & onGround == false)
        {
            is_jumping = false;
        }
        else if (is_jumping == false & onGround == true)
        {
            animator.SetBool("jump", false);
        }

        
        }



    void OnCollisionStay2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Flore")
        {
            onGround = true;
            //Debug.Log("ground");
        }


        // hit from enemy
        if (coll.gameObject.tag == "Enemy")
        {
            SceneManager.LoadScene("Level");
            Debug.Log("GAME OVER");
        }


        }

    void OnCollisionExit2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Flore")
        {
            onGround = false;
            //Debug.Log("no ground");
        }

    }

    void OnTriggerEnter2D(Collider2D other)
    {
        // Player is hidden
        if (other.gameObject.tag == "Hide")
        {
            Is_Hidden = true;
        }
    }

    void OnTriggerExit2D(Collider2D other)
    {
        // Player is not hidden
        if (other.gameObject.tag == "Hide")
        {
            Is_Hidden = false;
        }
    }

    void SetCountText()
    {
        countText.text = "Score: " + score.ToString();
    }


}