//Created by Neodrop
//neodrop@unity3d.ru
using System.IO;
using UnityEngine;
using UnityEditor;
using System.Threading;
//#pragma warning disable

public class WizardAntaresAbout : EditorWindow
{
    public static WizardAntaresAbout wizard;
    public static string currentVersion = "", newVersion = "";
    private Vector2 _scroll;

    public static void CreateWindow()
    {
        wizard = (WizardAntaresAbout)GetWindow(typeof(WizardAntaresAbout));
        wizard.title = "About Antares";
        wizard.minSize = new Vector2(512, 512);
        wizard.maxSize = new Vector2(513, 512);
        wizard.Show();
        wizard.ShowUtility();
    }

    void OnGUI() 
    {
        if (EditorApplication.isCompiling) return;
        Rect middleRect = new Rect((wizard.position.width * .5f) - 128, -15, 256, 60);
        EditorGUI.DropShadowLabel(middleRect, "ABOUT ANTARES PROJECT\n\nCURRENT VERSION : " + GetVersion());
        float y = 15f;
        if(!string.IsNullOrEmpty(newVersion))
        {
            GUI.color = AntaresEdWindow.colorSelected;
            if(GUI.Button(new Rect(middleRect.x + 64, middleRect.yMax + 10, 128, 25), "Get new version now!", EditorStyles.miniButton))
            {
                Application.OpenURL(AntaresGameObjectCustom.AntaresLoadUrl);
            } 
            y = 25;
            GUI.color = Color.white;
        }

        

        if (GUI.Button(new Rect(middleRect.x + 64, middleRect.yMax + y, 128, 25), "Donation", EditorStyles.miniButton))
        {
            Application.OpenURL(AntaresGameObjectCustom.Routines.donationURL);
        }

        GUILayout.Space(90);
        GUILayout.BeginArea(new Rect(5, 90, wizard.position.width - 10, 365));
        _scroll = GUILayout.BeginScrollView(_scroll, EditorStyles.textField);

        ShowAbout();

        GUILayout.EndScrollView();
        GUILayout.EndArea();

        AntaresGameObjectCustom.Routines.DrawBackground(new Rect(wizard.position.width - 512, 0, 512, 512));
        AntaresGameObjectCustom.Routines.ShowCredits(new Rect(wizard.position.width - 115,
                                                                      wizard.position.height - 60,
                                                                      100, 32));
    }

    private void ShowAbout()
    {
        GUILayout.Label(
            "   Antares Browser & Addon is C# class package extending the functionality and capabilities\n of Unity Editor, also adding certain auxiliary methods, in order to improve your project\n management and its code.\n   Antares Browser & Addon is an Open Source project and you may use it cost free in\nany number of projects.  ");
        GUILayout.Space(10);

        if (GUILayout.Button("Antares topic on http://forum.unity3d.com (the link)", GUIStyle.none, GUILayout.Width(300)))
            Application.OpenURL("http://forum.unity3d.com/viewtopic.php?t=50843");
        GUILayout.Space(10);

        GUILayout.Label("AUTORS : ");
        GUILayout.Space(5);
        GUILayout.Label("Andrey Paramonov (aka Neodrop) : neodrop@unity3d.ru");
        GUILayout.Space(5);
        GUILayout.Label("Yevgeny Pleshkov (aka Ferz) : fer-7@ya.ru");
        GUILayout.Space(5);
        GUILayout.Label("Andrey Pakhomov (aka PAX): pax83@mail.ru");
        GUILayout.Space(5);
        GUILayout.Label("Alexandr Khomiak (aka Norgen): rivia@mail.ru");
        GUILayout.Space(5);
        GUILayout.Label("Slav Pankratov (aka Gnoblin): gnoblin@gmail.com");
        GUILayout.Space(5);
        GUILayout.Label("Special thanks to http://www.reignofsteel.net");
    }

    private static Thread _loadThread;
    public static string GetVersion()
    {
        if (string.IsNullOrEmpty(currentVersion) && _loadThread == null)
        {
            _loadThread = new Thread(TestCore);
            _loadThread.Start();
        }

        return currentVersion;
    }

    void OnApplicationQuit()
    {
        if(_loadThread != null)_loadThread.Abort();
    }

    static void TestCore()
    {
        WWW wwwLocal = new WWW("file://" + Application.dataPath + Path.DirectorySeparatorChar + "Antares" + Path.DirectorySeparatorChar + "AntaresVersion.txt");
        while (!wwwLocal.isDone)
        {
        }
        if (string.IsNullOrEmpty(wwwLocal.error))
        {
            currentVersion = wwwLocal.data;
        }

        WWW www = new WWW("http://unity3d.ru/ANTARES/AntaresVersion.php?vers=" + currentVersion);
        while (!www.isDone)
        {
        }
        if (string.IsNullOrEmpty(www.error))
        {
            newVersion = www.data;
            if (newVersion == "ok") newVersion = "";
        }
    }
}
