using System.IO;
using UnityEngine;

// A lightweight main-menu release-notes notice.  Change ReleaseVersion when a
// new release is announced: old dismissals will then no longer match and the
// notes will appear again until the player dismisses that version.
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
public class KrakensKreationsWelcome : MonoBehaviour
{
    private const string ConfigFileName = "krakens-kreations-welcome.cfg";
    private const string ReleaseVersion = "1.1";
    private Rect window = new Rect(0f, 0f, 530f, 290f);
    private bool positioned;
    private bool showWelcome;

    public void Awake()
    {
        showWelcome = !WasDismissed();
    }

    public void OnGUI()
    {
        if (!showWelcome || HighLogic.LoadedScene != GameScenes.MAINMENU)
            return;
        if (!positioned)
        {
            window.x = (Screen.width - window.width) * 0.5f;
            window.y = (Screen.height - window.height) * 0.5f;
            positioned = true;
        }
        window = GUI.Window(871442, window, DrawWindow, "Kraken's Kreations — Update 1.1");
    }

    private void DrawWindow(int id)
    {
        GUILayout.Space(8f);
        GUIStyle messageStyle = new GUIStyle(GUI.skin.label);
        messageStyle.wordWrap = true;
        GUILayout.Label(
            "Welcome to Kraken's Kreations Update 1.1.\n\n" +
            "This release expands the horror across the Kerbol system with the Jool Eye, Laythe Bloop, improved sounds and ambience, encounter fixes, and improved debug tools.\n\n" +
            "The unfinished Laythe land creature is deliberately not included in this release. Please report bugs on the Discord.",
            messageStyle, GUILayout.Width(495f));
        GUILayout.FlexibleSpace();
        if (GUILayout.Button("Open Kraken's Kreations Discord"))
            Application.OpenURL("https://discord.gg/pWRtp38dP");
        if (GUILayout.Button("Do not show again"))
        {
            SaveDismissal();
            showWelcome = false;
        }
        GUI.DragWindow(new Rect(0f, 0f, 10000f, 25f));
    }

    private static string ConfigPath
    {
        get { return Path.Combine(KSPUtil.ApplicationRootPath, "GameData", "Kraken's Kreations", ConfigFileName); }
    }

    private static bool WasDismissed()
    {
        if (!File.Exists(ConfigPath))
            return false;
        ConfigNode node = ConfigNode.Load(ConfigPath);
        bool dismissed;
        string dismissedVersion = node != null ? node.GetValue("dismissedVersion") : null;
        return node != null &&
               bool.TryParse(node.GetValue("dismissed"), out dismissed) && dismissed &&
               string.Equals(dismissedVersion, ReleaseVersion, System.StringComparison.Ordinal);
    }

    private static void SaveDismissal()
    {
        ConfigNode node = new ConfigNode("KRAKENS_KREATIONS_WELCOME");
        node.AddValue("dismissed", true);
        node.AddValue("dismissedVersion", ReleaseVersion);
        node.Save(ConfigPath);
    }
}
