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

public class PlayerController : MonoBehaviour {

    public float moveSpeed = 100;
    public float accelerationTime = 0.2f;
    public float decelerationTime = 0.1f;
    Vector2 speed;
    Rigidbody rb;
    float acc, decc;
    Vector2 deltaSpeed;

    public bool isRunning;
    public bool forwards = true;

    public Image uiShit;
    public float shitTotalTime = 30;
    float shitTime = 0;

    public bool lostGame = false;
    public bool gameOver = false;
    Vector3 shitPosition;

    // Start is called before the first frame update
    void Start() {
        speed = Vector2.zero;
        forwards = true;
    }

    void Awake() {
        rb = GetComponent<Rigidbody>();    
    }

    // Update is called once per frame
    void Update() {

        if (!gameOver) {
            Control();
            shitTime += Time.deltaTime;
            uiShit.fillAmount = shitTime / shitTotalTime;
        } else {
            if (lostGame) {
                forwards = true;
                float shake = 0.06f;
                transform.position = new Vector3(
                    shitPosition.x + Random.Range(-shake, shake),
                    shitPosition.y + Random.Range(-shake, shake),
                    shitPosition.z
                );
                GetComponentInChildren<PlayerAnimation>().renderer.sprite = GetComponentInChildren<PlayerAnimation>().sprites[0];

                GameObject.Find("Scream").GetComponent<SpriteRenderer>().enabled = true;

                uiShit.fillAmount = 1;
            } else {
                rb.isKinematic = true;
                GetComponent<Collider>().enabled = false;
                transform.position = Vector3.Lerp(transform.position, Vector3.zero, 0.1f);
            }
        }

        if (shitTime > shitTotalTime) {
            gameOver = true;
            lostGame = true;
            shitPosition = transform.position;
            shitTime = 0;
            GetComponent<AudioSource>().PlayOneShot(GetComponent<Sounds>().sounds[2]);
        }
    }

    void Control() {
        acc = moveSpeed / accelerationTime;
        decc = moveSpeed / decelerationTime;

        Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        float ang = Mathf.Atan2(input.y, input.x) * Mathf.Rad2Deg;

        if (input.x != 0) {
            speed.x += Mathf.Cos(ang) * acc * Time.deltaTime;
        } else {
            speed.x = Approach(speed.x, 0, decc * Time.deltaTime);
        }

        speed.x = Mathf.Clamp(speed.x, -moveSpeed, moveSpeed);

        if (input.y != 0) {
            speed.y += Mathf.Sin(ang) * acc * Time.deltaTime;
        } else {
            speed.y = Approach(speed.y, 0, decc * Time.deltaTime);
        }

        speed.y = Mathf.Clamp(speed.y, -moveSpeed, moveSpeed);

        if (input.x != 0 || input.y != 0) {
            isRunning = true;
        } else {
            isRunning = false;
        }

        if (input.y > 0) {
            forwards = false;
        } else if (input.y < 0) {
            forwards = true;
        }
    }

    private void FixedUpdate() {
        rb.velocity = new Vector3(
            speed.x, 
            0,
            speed.y
        );
    }

    float Approach(float a, float b, float amt) {
        if (a < b) {
            a += amt;
            if (a > b) {
                return b;
            }
        }

        if (a > b) {
            a -= amt;
            if (a < b) {
                return b;
            }
        }
        return a;
    }
}
