﻿using UnityEngine;
using UnityEditor;

namespace Yodo1.FanCraft
{
    public class SDKWindow_iOS : EditorWindow
    {
        private static readonly string Message_iOS = "Please do not enable FanCraft push notifications if you have integrated Apple remote notification yourself or are using another remote notifications plugin";

        FanCraftSettings settings;

        [MenuItem("Yodo1/FanCraft/iOS Setting")]
        public static void Init()
        {
            EditorWindow window = GetWindow(typeof(SDKWindow_iOS), false, "iOS Settings");
            window.Show();
        }

        private void OnEnable()
        {
            this.settings = SettingsManager.Load();
        }

        private void OnDisable()
        {
            this.SaveSettings();
            this.settings = null;
        }

        private void OnGUI()
        {
            DrawIosSettings();

            EditorGUILayout.Separator();

            DrawSaveButton();
        }

        private void SaveSettings()
        {
            SettingsManager.Save(this.settings);
        }

        private void DrawSaveButton()
        {
            GUIStyle buttonStyle = new GUIStyle()
            {
                padding = new RectOffset(10, 10, 2, 2)
            };
            GUILayout.BeginVertical(buttonStyle, new GUILayoutOption[0]);
            if (GUILayout.Button("Save Configuration"))
            {
                this.SaveSettings();
            }

            GUILayout.EndVertical();
        }

        private void DrawIosSettings()
        {
            GUIStyle gUIStyle = new GUIStyle()
            {
                padding = new RectOffset(10, 10, 5, 5)
            };
            GUILayout.BeginVertical(gUIStyle, new GUILayoutOption[0]);

            GUIStyle fontStyle = new GUIStyle();
            fontStyle.fontSize = 20;
            fontStyle.normal.textColor = Color.white;
            fontStyle.fontStyle = FontStyle.Bold;
            GUILayout.Label("Push Notifications", fontStyle);
            EditorGUILayout.Separator();

            EditorGUILayout.HelpBox(Message_iOS, MessageType.Info);

            EditorGUILayout.Separator();

            bool isEnabled = EditorGUILayout.Toggle("Enable Push Notifications", settings.notificationSettingsIOS.Enable, new GUILayoutOption[0]);
            settings.notificationSettingsIOS.Enable = isEnabled;
            EditorGUILayout.Separator();

            if (settings.notificationSettingsIOS.Enable)
            {
                GUIStyle style = new GUIStyle()
                {
                    padding = new RectOffset(20, 10, 5, 5)
                };
                GUILayout.BeginVertical(style, new GUILayoutOption[0]);

                float originalValue = EditorGUIUtility.labelWidth;
                EditorGUIUtility.labelWidth = originalValue + 60;

                bool isRelease = EditorGUILayout.Toggle("Enable Release Environment for APS", !settings.notificationSettingsIOS.development, new GUILayoutOption[0]);
                settings.notificationSettingsIOS.development = !isRelease;
                EditorGUIUtility.labelWidth = originalValue;
                GUILayout.EndVertical();
            }
            GUILayout.EndVertical();

        }
    }
}

