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

public class BeakAnimation : MonoBehaviour {
    public GameObject frontBeak;
    public GameObject backBeak;

    public enum State {Open, Closed};
    public TimerHelper timer;

    public State state = State.Closed;

    public float rotation = 0;

    public float timerLength = 5000;

    void Start() {
        timer = TimerHelper.Create();
    }

    void Update() {
        switch(state) {
            case State.Open:
                Update_Open();

                break;

            case State.Closed:
                Update_Closed();

                break;
        }

        float time = (Mathf.Cos(Time.realtimeSinceStartup * 5) * 25) * (rotation / 90);
        
        PlayerController player = PlayerController.Get();

        if (player.transform.localScale.x > 0) {
            frontBeak.transform.rotation = Quaternion.Euler(0, 0, rotation + time);
            backBeak.transform.rotation = Quaternion.Euler(0, 0, -rotation + time);
        } else {
             frontBeak.transform.rotation = Quaternion.Euler(0, 0, -rotation + time);
            backBeak.transform.rotation = Quaternion.Euler(0, 0, rotation + time);
        }
    }

    public void Update_Open() {
        float rot = rotation;
        
        rot = rot * 0.95f + 0.0f * 0.05f;

        rotation = rot;

        if (timer.GetMillisecs() > timerLength) {
            state = State.Closed;

            timerLength = 2000;

            timer.Reset();
        }      
    }

    public void Update_Closed() {
        float rot = rotation;
        
        rot = rot * 0.95f + 45 * 0.05f;

        rotation = rot;

         if (timer.GetMillisecs() > timerLength) {
            state = State.Open;

            timerLength = Random.Range(1000, 5000);

            timer.Reset();
        }
    }
}
