﻿using UnityEngine;
using System;
using UnityEngine.UI;
using System.Linq;
using Stove.StoveSDK;

public class EnvironmentDialog : MonoBehaviour
{
    public Action<string, string, string> action;

    public GameObject panel;
    public ToggleGroup toggleGroup;
    public InputField version;
    public InputField packageName;

    public static void Show(Action<string, string, string> action)
    {
        Present(action);   
    }

    public static void Present(Action<string, string, string> action)
    {
        GameObject environmentDialogPanel = GameObject.Find("EnvironmentDialogPanel");

        Image image = environmentDialogPanel.GetComponent<Image>();
        image.enabled = true;

        EnvironmentDialog environmentDialog = environmentDialogPanel.GetComponent<EnvironmentDialog>();
        environmentDialog.panel.SetActive(true);
        environmentDialog.action = action;
    }

    public static void Dismiss()
    {
        GameObject environmentDialogPanel = GameObject.Find("EnvironmentDialogPanel");

        Image image = environmentDialogPanel.GetComponent<Image>();
        image.enabled = false;

        EnvironmentDialog environmentDialog = environmentDialogPanel.GetComponent<EnvironmentDialog>();
        environmentDialog.panel.SetActive(false);
    }

    public void OnClick(int tag)
    {
        if (tag == 0)
        {
            Toggle toggle = toggleGroup.ActiveToggles().FirstOrDefault();
            if (toggle.name.Equals("Dev")) {
                action.Invoke(packageName.text, version.text, "dev");
            } else if (toggle.name.Equals("QA")) {
                action.Invoke(packageName.text, version.text, "qa");
            } else if (toggle.name.Equals("QA2")) {
                action.Invoke(packageName.text, version.text, "qa2");
            } else if (toggle.name.Equals("Sandbox")) {
                action.Invoke(packageName.text, version.text, "sandbox");
            } else if (toggle.name.Equals("Live")) {
                action.Invoke(packageName.text, version.text, "live");
            } else {
                action.Invoke(null, null, null);
            }
        }
        else
        {
            action.Invoke(null, null, null);
        }

        Dismiss();
    }
}
