using System.Collections.Generic;
using UnityEngine;

public class GameInput_Manager : MonoBehaviour
{
    public bool isEnabled = true;
    public List<GameObject> subscribers = new List<GameObject>();

    public SceneField replayScene;

    public void enable(bool enable)
    {
        isEnabled = enable;
    }

    public void subscribe (GameObject sub)
    {
        subscribers.Add(sub);
    }
    public void unsubscribe(GameObject sub)
    {
        subscribers.Remove(sub);
    }

    private void notifySubs(EnumActionCommand command)
    {
        foreach (GameObject sub in subscribers)
        {
            sub.GetComponent<InputController>().notifyInput(command);
        }
    }

    // Keyboard Input
    public void Update()
    {
        if (isEnabled)
        {
            if (Input.GetKeyDown("left")) notifySubs(EnumActionCommand.LEFT_PRESS);
            if (Input.GetKeyUp("left")) notifySubs(EnumActionCommand.LEFT_RELEASE);

            if (Input.GetKeyDown("right")) notifySubs(EnumActionCommand.RIGHT_PRESS);
            if (Input.GetKeyUp("right")) notifySubs(EnumActionCommand.RIGHT_RELEASE);

            if (Input.GetKeyDown("up")) notifySubs(EnumActionCommand.UP_PRESS);
            if (Input.GetKeyUp("up")) notifySubs(EnumActionCommand.UP_RELEASE);

            if (Input.GetKeyDown("down")) notifySubs(EnumActionCommand.DOWN_PRESS);
            if (Input.GetKeyUp("down")) notifySubs(EnumActionCommand.DOWN_RELEASE);

            if (Input.GetKeyDown("z")) notifySubs(EnumActionCommand.ZOOM_IN_PRESS);
            if (Input.GetKeyUp("z")) notifySubs(EnumActionCommand.ZOOM_IN_RELEASE);

            if (Input.GetKeyDown("x")) notifySubs(EnumActionCommand.ZOOM_OUT_PRESS);
            if (Input.GetKeyUp("x")) notifySubs(EnumActionCommand.ZOOM_OUT_RELEASE);
        }
    }

    //UI Button input
    public void actionUILeft()
    {
        if (isEnabled) notifySubs(EnumActionCommand.LEFT_PRESS);
    }
    public void actionUILeftRelease()
    {
        if (isEnabled) notifySubs(EnumActionCommand.LEFT_RELEASE);
    }

    public void actionUIRight()
    {
        if (isEnabled) notifySubs(EnumActionCommand.RIGHT_PRESS);
    }
    public void actionUIRightRelease()
    {
        if (isEnabled) notifySubs(EnumActionCommand.RIGHT_RELEASE);
    }

    public void actionUIUp()
    {
        if (isEnabled) notifySubs(EnumActionCommand.UP_PRESS);
    }
    public void actionUIUpRelease()
    {
        if (isEnabled) notifySubs(EnumActionCommand.UP_RELEASE);
    }

    public void actionUIDown()
    {
        if (isEnabled) notifySubs(EnumActionCommand.DOWN_PRESS);
    }
    public void actionUIDownRelease()
    {
        if (isEnabled) notifySubs(EnumActionCommand.DOWN_RELEASE);
    }

    public void actionZoomIn()
    {
        if (isEnabled) notifySubs(EnumActionCommand.ZOOM_IN_PRESS);
    }
    public void actionZoomInRelease()
    {
        if (isEnabled) notifySubs(EnumActionCommand.ZOOM_IN_RELEASE);
    }

    public void actionZoomOut()
    {
        if (isEnabled) notifySubs(EnumActionCommand.ZOOM_OUT_PRESS);
    }
    public void actionZoomOutRelease()
    {
        if (isEnabled) notifySubs(EnumActionCommand.ZOOM_OUT_RELEASE);
    }

    public void actionGOReplay()
    {
        //if (isEnabled) GameObject.FindGameObjectsWithTag("MasterManager")[0].GetComponent<Scene_Manager>().loadScene(replayScene,0);
        if (isEnabled) GameObject.FindGameObjectsWithTag("MasterManager")[0].GetComponent<Scene_Manager>().loadSceneWithTransition(replayScene, 0.5f);
    }
}
