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

public class GameInputManager : MonoBehaviour
{
    public bool isControllable = true;
    public bool walking = false;
    public bool running = false;
    public bool jumping = false;
    public Vector2 input = Vector2.zero;
    public float accelerate = 0.0f;
    public float decelerate = 0.0f;
    public float rollRight = 0.0f;
    public float rollLeft = 0.0f;
    public bool escaping = false;
    [HideInInspector]
    public static GameInputManager main;
    //Inputs that are used in many places are static
    public static bool pressingActionButton = false;
    public static bool actionButtonDown = false;
    private float actionButtonDownTime = 0.0f;
    public static bool skipping = false;
    public static bool clicking = false;
    //Private variables
    private MouseOrbit mo;

    // Start is called before the first frame update
    void Awake()
    {
        main = this;
        ResetStaticVars();
        mo = Camera.main.GetComponent<MouseOrbit>();
    }

    // Update is called once per frame
    void Update()
    {
        if (actionButtonDown && Mathf.Abs(Time.unscaledTime - actionButtonDownTime) > Time.unscaledDeltaTime * 1.5f) {
            actionButtonDown = false;
        }
    }

    public void OnDestroy() {
        main = null;
        ResetStaticVars();
    }

    void ResetStaticVars() {
        pressingActionButton = false;
        actionButtonDown = false;
        skipping = false;
        clicking = false;
    }

    public void OnMove(InputValue value) {
        if (!PauseMenu.paused) {
            input = value.Get<Vector2>();
            if (input.magnitude > 1.0f) {
                input = input.normalized;
            }
            if (UIObjectsContainer.main != null) {
                UIObjectsContainer.main.ChoiceSelector.UpdateInput2(input);
            }
        }
        else {
            input = Vector2.zero;
        }
    }

    public void OnMenuMove(InputValue value) {
        Vector2 inp = value.Get<Vector2>();
        if (inp.magnitude > 1.0f) {
            inp = inp.normalized;
        }
        if (UIObjectsContainer.main != null) {
            UIObjectsContainer.main.ChoiceSelector.UpdateInput1(inp);
        }
    }

    public void OnRun(InputValue value) {
        running = value.Get<float>() > 0.5;
    }

    public void OnAction(InputValue value) {
        bool pressing = value.Get<float>() > 0.5;
        if (!pressingActionButton && pressing && !PauseMenu.paused) {
            actionButtonDown = true;
            actionButtonDownTime = Time.unscaledTime;
        }
        else {
            actionButtonDown = false;
        }
        pressingActionButton = pressing;
    }

    public void OnSkip(InputValue value) {
        bool pressing = value.Get<float>() > 0.5;
        skipping = pressing && !isControllable;
    }

    public void OnClick(InputValue value) {
        bool pressing = value.Get<float>() > 0.5;
        clicking = pressing;
    }

    public void OnWalk(InputValue value) {
        walking = value.Get<float>() > 0.5;
    }

    public void OnLook(InputValue value) {
        Vector2 v2val = value.Get<Vector2>();
        Vector3 inp = v2val;
        if (mo != null) {
            mo.ReceiveInput(inp);
        }
    }
    

    public void OnCancel() {
    }

    public void OnJump() {
        if (ThirdPersonController.main != null) {
            ThirdPersonController.main.DoJump();
        }
    }


    public void OnPause() {
        if (ThirdPersonController.main.entity.parameters.currentHP > 0) {
            PauseMenu.paused = !PauseMenu.paused;
        }
        else {
            PauseMenu.paused = false;
        }
    }
    
    public void OnAccelerate(InputValue value) {
        accelerate = value.Get<float>();
    }

    public void OnDecelerate(InputValue value) {
        decelerate = value.Get<float>();
    }

    public void OnRollRight(InputValue value) {
        rollRight = value.Get<float>();
    }

    public void OnRollLeft(InputValue value) {
        rollLeft = value.Get<float>();
    }
}
