﻿using UnityEditor;
using UnityEngine;

public class EC_Menu : EditorWindow
{

    private const string documentationURL = "https://patolistudio.com/easycoppa/USER_GUIDE.pdf";

    EC_Settings ec_settings = null;

    public Texture banner;

    private GUIStyle bannerStyle;
    private GUIStyle headerStyle;

    [MenuItem("Window/Easy Coppa/Open Easy Coppa")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<EC_Menu>("Easy Coppa");
    }

    private void Update()
    {
        if (ec_settings == null)
        {
            ec_settings = (EC_Settings)ScriptableObject.CreateInstance(typeof(EC_Settings));
            AssetDatabase.CreateAsset(ec_settings, @"Assets\EasyCoppa\Resources\EC_Settings.asset");
        }
    }

    private void OnGUI()
    {
        SerializedObject serializedSettings = new SerializedObject(ec_settings);
        SerializedProperty translation = serializedSettings.FindProperty("translation");

        bannerStyle = new GUIStyle(GUIStyle.none)
        {
            fixedHeight = 42f,
            alignment = TextAnchor.MiddleLeft
        };

        headerStyle = new GUIStyle(GUIStyle.none);
        headerStyle.normal.background = MakeTex(10, 1, Color.black);

        EditorGUILayout.BeginHorizontal(headerStyle);
        GUILayout.Box(banner, bannerStyle);
        EditorGUILayout.EndHorizontal();
        GUILayout.Label("Easy Coppa Configuration", EditorStyles.boldLabel);
        ec_settings.showCoppa = EditorGUILayout.Toggle("Show Coppa", ec_settings.showCoppa);
        ec_settings.rejectMinors = EditorGUILayout.Toggle("Reject Minors", ec_settings.rejectMinors);
        ec_settings.blockInput = EditorGUILayout.Toggle("Block Input", ec_settings.blockInput);
        ec_settings.useTranslation = EditorGUILayout.Toggle("Use Translation", ec_settings.useTranslation);

        EditorGUILayout.PropertyField(translation, new GUIContent("Translation"));

        ec_settings.dateFormat = (EC_Settings.DateFormat)EditorGUILayout.EnumPopup(new GUIContent("Date Format"), ec_settings.dateFormat);

        ec_settings.termsOfUseURL = EditorGUILayout.TextField(new GUIContent("Terms URL"), ec_settings.termsOfUseURL);
        ec_settings.privacyPolicyURL = EditorGUILayout.TextField(new GUIContent("Privacy URL"), ec_settings.privacyPolicyURL);
        ec_settings.supportMail = EditorGUILayout.TextField(new GUIContent("Support Mail"), ec_settings.supportMail);

        serializedSettings.ApplyModifiedProperties();
    }

    [MenuItem("Window/Easy Coppa/Open Documentation")]
    static void OpenDocumentation()
    {
        Application.OpenURL(documentationURL);
    }

    private Texture2D MakeTex(int width, int height, Color col)
    {
        Color[] pix = new Color[width * height];

        for (int i = 0; i < pix.Length; i++)
            pix[i] = col;

        Texture2D result = new Texture2D(width, height);
        result.SetPixels(pix);
        result.Apply();

        return result;
    }
}
