using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Singleton;
public enum EventTypes {
    Windmills,
    App,
    Kraken,
    Rotate,
    Quit,
    Tickle,
    None
}


public class TerminalManager : Singleton<TerminalManager> {

    [SerializeField] private GameObject termGameObject;
    [SerializeField] private Animator animator;
    [SerializeField] private GameObject termButton;

    [Header("First")]
    [SerializeField] private Dropdown dropdown;

    [Header("Second")]
    [SerializeField] private Dropdown firstDropdown;
    [SerializeField] private Dropdown secondDropdown;

    private int loggedInBefore = 0;
    private int loggedInBefore21 = 0;
    private int loggedInBefore22 = 0;

    public void ShowActivateTerminalButton() {
        termButton.SetActive(true);
    }

    public void HideActivateTerminalButton() {
        termButton.SetActive(false);
    }

    public void Enable() {
        if (!termGameObject.activeSelf) {
            animator.SetTrigger("TerminalAppear");
        }
    }

    public void Toggle() {
        if (termGameObject.activeSelf) {
            Disable();
        } else {
            Enable();
        }
    }

    public void Disable() {
        if (termGameObject.activeSelf) {
            animator.SetTrigger("TerminalDisappear");

        }
    }

    public void EvaluateDropdownInput() {
        if (GameManager.Instance.First) {
            First();
        } else if (GameManager.Instance.Second) {
            Second();
        } 
    }

    #region First

    private bool CheckLoggedInBefore(int value) {
        var isloggedin = value == loggedInBefore;
        if (loggedInBefore == -1) {
            return true;
        }

        return !isloggedin;
    }

    private bool firsttime = false;
    private void First() {
        if (CheckLoggedInBefore(dropdown.value)) {
            loggedInBefore = dropdown.value;

            switch (dropdown.value) {
                case 0: // lose head
                    UnsinkShip();
                    //ResetNessieRotate();
                    SetNessieLoseHead();
                    Debug.Log("Losing head...");
                    break;
                case 1: // rotate
                    UnsinkShip();
                    Subtitles.Instance.DisplayTextFirst("Sailing beyond the sunset...");
                    if (!firsttime) {
                        Subtitles.Instance.DisplayTextFirst("That looks... kinda alright? Maybe better try again...");
                        firsttime = true;
                    }
                    SetNessieRotate();
                    Debug.Log("Rotate Ness...");
                    Nessie.Instance.headlost = false;

                    break;
                case 2: // sink
                    ResetNessieLoseHead();
                    ResetNessieRotate();

                    SinkShip();
                    Debug.Log("Sink ship...");

                    break;
                case 3: // loadcannon
                    ResetNessieRotate();
                    Debug.Log("Loading cannon...");
                    UnsinkShip();
                    ResetNessieLoseHead();
                    HideActivateTerminalButton();
                    Disable();
                    GameManager.Instance.First = false;
                    break;

            }
            Disable();

        } else {
            Debug.Log("Already logged in!");
        }
    }

    private void SetNessieLoseHead() {
        Nessie.Instance.LoseHead();
    }

    private void ResetNessieLoseHead() {
        Nessie.Instance.GetHead();
    }

    private void SetNessieRotate() {
        Nessie.Instance.SetRotate();
    }

    private void ResetNessieRotate() {
        Nessie.Instance.UndoRotate();
    }

    private void SinkShip() {
        Ship.Instance.Sink();
    }

    private void UnsinkShip() {
        Ship.Instance.Unsink();
    }

    private void ShipLoadCannon() {

    }

    #endregion

    #region Second

    private bool CheckLoggedInBeforeTwice(int first, int second) {
        var isloggedin = first == loggedInBefore21 && second == loggedInBefore22;
        if (loggedInBefore21 == -1 ||loggedInBefore22 == -1) {
            return true;
        }

        return !isloggedin;
    }

    
    private void Second() {

        if (CheckLoggedInBeforeTwice(firstDropdown.value, secondDropdown.value)) {
            loggedInBefore21 = firstDropdown.value;
            loggedInBefore22 = secondDropdown.value;

            EventTypes arg1 = EventTypes.None;
            EventTypes arg2 = EventTypes.None;

            switch (firstDropdown.value) {
                case 0: // windmills
                    arg1 = EventTypes.Windmills;

                    Debug.Log("Chose windmills");
                    break;
                case 1: // application
                    arg1 = EventTypes.App;
                    Debug.Log("Chose app");

                    break;
                case 2: // kraken
                    arg1 = EventTypes.Kraken;
                    
                    Debug.Log("Chose kraken");

                    break;
            }

            switch (secondDropdown.value) {
                case 0: // rotate
                    arg2 = EventTypes.Rotate;

                    Debug.Log("rotate");
                    break;
                case 1: // quit
                    arg2 = EventTypes.Quit;
                    Debug.Log("quit");

                    break;
                case 2: // tickle
                    arg2 = EventTypes.Tickle;

                    Debug.Log("tickle");

                    break;
            }
            Send(arg1, arg2);
            Disable();

        } else {
            Debug.Log("Already logged in!");
        }
    }

    public void Send(EventTypes first, EventTypes second) {
        if (first == EventTypes.None || second == EventTypes.None) {
            Debug.Log("None event type found. this should not happen");
            return;
        }

        if (first == EventTypes.App && second == EventTypes.Rotate) { 
            StartAppRotate();
            DisableTickle();
            WindDisableRotate();
            WindDisableTickle();
            DisableKrakken();
            DisableWindQuit();
            DisableKrakkenTicklish();
        } else if (first == EventTypes.App && second == EventTypes.Tickle) { 
            StopAppRotate();
            EnableTickle();
            WindDisableRotate();
            WindDisableTickle();
            DisableWindQuit();
            DisableKrakken();

            DisableKrakkenTicklish();
        } else if (first == EventTypes.App && second == EventTypes.Quit) { // done
            Application.Quit(1);
        } else if (first == EventTypes.Windmills && second == EventTypes.Rotate) {
            WindDisableRotate();
            StopAppRotate();
            DisableTickle();
            WindEnableRotate();
            WindDisableTickle();
            DisableWindQuit();
            DisableKrakken();

            DisableKrakkenTicklish();
        } else if (first == EventTypes.Windmills && second == EventTypes.Tickle) {
            StopAppRotate();
            DisableTickle();
            WindDisableRotate();
            WindEnableTickle();
            DisableWindQuit();
            DisableKrakken();
            DisableKrakkenTicklish();
        } else if (first == EventTypes.Windmills && second == EventTypes.Quit) {
            StopAppRotate();
            DisableTickle();
            //WindDisableRotate();
            WindDisableTickle();
            EnableWindQuit();
            DisableKrakken();
            DisableKrakkenTicklish();
        } else if (first == EventTypes.Kraken && second == EventTypes.Rotate) {
            StopAppRotate();
            DisableTickle();
            WindDisableRotate();
            WindDisableTickle();
            KrakenRotate();
            DisableKrakkenTicklish();
            EnableWindQuit();
        } else if (first == EventTypes.Kraken && second == EventTypes.Tickle) {
            StopAppRotate();
            DisableTickle();
            WindDisableRotate();
            WindDisableTickle();

            EnableKrakkenTicklish();
            EnableWindQuit();

            GameManager.Instance.Second = false;
        } else if (first == EventTypes.Kraken && second == EventTypes.Quit) {
            StopAppRotate();
            DisableTickle();
            WindDisableRotate();
            WindDisableTickle();


            DisableKrakken();
            DisableKrakkenTicklish();
            EnableWindQuit();
            windAnimator.SetTrigger("Disappear");
        }
    }

    #region App
    public bool approtate = false;
    public Animator camera;
    public void StartAppRotate() {
        if (approtate) return;
        approtate = true;
        camera.SetTrigger("Start");
    }
    public void StopAppRotate() {
        if (!approtate) return;
        approtate = false;

        camera.SetTrigger("Stop");

    }

    public GameObject tickleplane;
    public bool tickling = false;
    public void EnableTickle() {
        if (tickling) return;
        tickling = true;
        tickleplane.SetActive(true);
    }

    public void DisableTickle() {
        if (!tickling) return;
        tickling = false;
        tickleplane.SetActive(false);
    }
    #endregion

    #region Windmills

    [SerializeField] private Animator windAnimator;
    private bool windrotate = true;
    public void WindEnableRotate() {
        if (windrotate) return;
        windrotate = true;
        windAnimator.SetTrigger("Rotate");
    }

    public void WindDisableRotate() {
        if (!windrotate) return;
        windrotate = false;
        windAnimator.SetTrigger("StopRotate");

    }

    [SerializeField] private WindTickle tick;
    [SerializeField] private WindTickle tick2;
    private bool windtickle = false;
    public void WindEnableTickle() {
        if (windtickle) return;
        windtickle = true;
        tick.Enable();
        tick2.Enable();
    }

    public void WindDisableTickle() {
        if (!windtickle) return;
        windtickle = false;
        tick.Disable();
        tick2.Disable();
    }

    private bool windquit = false;
    public void EnableWindQuit() {
        if (windquit) return;
        windquit = true;
        windAnimator.SetTrigger("Disappear");
    }
    public void DisableWindQuit() {
        if (!windquit) return;
        windquit = false;
        windAnimator.SetTrigger("StopRotate");
    }
    public void DisableWindQuitNvm() {
        windquit = false;
        windAnimator.SetTrigger("StopRotate");
    }


    #endregion

    #region Kraken

    private bool krakenrotating = false;
    [SerializeField] private Animator krAnimator;
    public void KrakenRotate() {
        if (krakenrotating) return;
        krakenrotating = true;
        krAnimator.SetTrigger("Rotate");
    }

    public void KrakenDisableRotate() {
        if (krakenrotating) return;
        krakenrotating = false;
        krAnimator.SetTrigger("Disappear");
    }

    public void DisableKrakken() {
        krakenrotating = false;
        krAnimator.SetTrigger("Disappeared");
    }

   [SerializeField]private WindTickle krakktickle;
   [SerializeField]private WindTickle krakktickle2;
   [SerializeField]private WindTickle krakktickle3;
   [SerializeField]private WindTickle krakktickle4;
    private bool krakticklish = false;
    public void EnableKrakkenTicklish() {
        if (krakticklish) return;
        krakticklish = true;
        krAnimator.SetTrigger("Appear");
        krakktickle.Enable();
        krakktickle2.Enable();
        krakktickle3.Enable();
        krakktickle4.Enable();
    }

    public void DisableKrakkenTicklish() {
        if (!krakticklish) return;
        krakticklish = false;
        krakktickle2.Disable();
        krakktickle.Disable();
        krakktickle3.Disable();
        krakktickle4.Disable();
    }
    #endregion

    #endregion

}
