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

public class MainMenuHandler : MonoBehaviour {
    public enum Menu { INITIALPAN, OPTIONSPAN, MENU, STARTGAME, CREDITS, EXIT }

    public Menu menuItem;
    float lerp;
    public bool inMenu;
    public bool gameStarted;
    public AudioHandler audioHandle;

    public Camera camera;

    public Vector3 menuCameraPosition;
    public Quaternion menuCameraRotation;
    public GameObject menuParent;
    public Vector3 menuParentTargetPosition;

    public Vector3 targetCameraPosition;
    Vector3 currentCameraPosition;
    public Vector3 creditsCameraPosition;
    Vector3 currentMenuPosition;

    Button play;
    Button credits;
    Button exit;

    public Transform creditsPaper;
    public Vector3 initialCreditsPosition;
    public Vector3 finalCreditsPosition;
    public bool printed;

    void Awake()
    {
        audioHandle.Play("Menu Music");
        this.menuItem = Menu.INITIALPAN;
        inMenu = true;
        camera.transform.position = menuCameraPosition + new Vector3(0, 3, 0);

        menuParentTargetPosition = menuParent.transform.position;
        menuParent.transform.position -= new Vector3(500, 0);

        play = menuParent.transform.GetChild(0).gameObject.GetComponent<Button>();
        credits = menuParent.transform.GetChild(1).gameObject.GetComponent<Button>();
        exit = menuParent.transform.GetChild(2).gameObject.GetComponent<Button>();

        initialCreditsPosition = creditsPaper.position;
        printed = false;
    }

    void PlayListener()
    {
        menuItem = Menu.STARTGAME;
        audioHandle.Stop("Menu Music");
        camera.orthographic = true;
    }

    void CreditsListener()
    {
        menuItem = Menu.CREDITS;
    }

    void ExitListener()
    {
        menuItem = Menu.EXIT;
    }

    void Update()
    {
        if (inMenu)
        {
            switch (menuItem)
            {
                case Menu.INITIALPAN:
                    camera.transform.position = Vector3.Lerp(menuCameraPosition + new Vector3(0, 3, 0), menuCameraPosition, lerp);
                    menuParent.transform.position = Vector3.Lerp(menuParentTargetPosition - new Vector3(500, 0), menuParentTargetPosition, lerp);
                    lerp += 0.01F;
                    lerp *= 0.992F;

                    if (camera.transform.position == menuCameraPosition && menuParent.transform.position == menuParentTargetPosition)
                    {
                        lerp = 0;

                        play.onClick.AddListener(PlayListener);
                        credits.onClick.AddListener(CreditsListener);
                        exit.onClick.AddListener(ExitListener);

                        currentCameraPosition = camera.transform.position;
                        currentMenuPosition = menuParent.transform.position;
                        menuItem = Menu.MENU;
                    }
                    break;
                case Menu.MENU:
                    camera.transform.position = Vector3.Lerp(currentCameraPosition, menuCameraPosition, lerp);
                    menuParent.transform.position = Vector3.Lerp(currentMenuPosition, menuParentTargetPosition, lerp);
                    lerp += 0.01F;

                    if (camera.transform.position == menuCameraPosition)
                    {
                        lerp = 0;
                        currentCameraPosition = camera.transform.position;
                        currentMenuPosition = menuParent.transform.position;
                    }
                    break;
                case Menu.CREDITS:
                    if (!printed)
                    {
                        creditsPaper.position = Vector3.Lerp(initialCreditsPosition, finalCreditsPosition, lerp);
                    }
                    camera.transform.position = Vector3.Lerp(menuCameraPosition, creditsCameraPosition, lerp);
                    menuParent.transform.position = Vector3.Lerp(menuParentTargetPosition, menuParentTargetPosition - new Vector3(500, 0), lerp);
                    lerp += 0.01F;

                    if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
                    {
                        currentCameraPosition = camera.transform.position;
                        currentMenuPosition = menuParent.transform.position;
                        printed = true;
                        menuItem = Menu.MENU;

                        lerp = 0;
                    }
                    break;
                case Menu.STARTGAME:
                    /*
                    camera.transform.position = Vector3.Lerp(menuCameraPosition, targetCameraPosition, lerp);
                    menuParent.transform.position = Vector3.Lerp(menuParentTargetPosition, menuParentTargetPosition - new Vector3(500, 0), lerp);
                    lerp += 0.01F;
                    lerp *= 0.992F;

                    if (camera.transform.position == targetCameraPosition)
                    {
                        gameStarted = true;
                        inMenu = false;
                        audioHandle.Play("Background Music");
                    }
                    */
                    menuParent.transform.position = menuParentTargetPosition - new Vector3(1000, 0);
                    camera.transform.position = targetCameraPosition;
                    gameStarted = true;
                    inMenu = false;
                    audioHandle.Play("Background Music");
                    break;
                case Menu.EXIT:
                    camera.transform.position = Vector3.Lerp(menuCameraPosition, menuCameraPosition + new Vector3(0, 3, 0), lerp);
                    menuParent.transform.position = Vector3.Lerp(menuParentTargetPosition, menuParentTargetPosition - new Vector3(300, 0), lerp);
                    lerp += 0.01F;
                    lerp *= 0.992F;

                    if (camera.transform.position == menuCameraPosition + new Vector3(0, 3, 0) && menuParent.transform.position == menuParentTargetPosition - new Vector3(300, 0))
                    {
                        Application.Quit();
                    }
                    break;
            }
        }
    }
}
