﻿/*
<copyright file="BGDatabaseExcelImportEditorTool.cs" company="BansheeGz">
    Copyright (c) 2019-2024 All Rights Reserved
</copyright>
*/

using System;
using UnityEditor;
using UnityEngine;

namespace BansheeGz.BGDatabase.Editor
{
    //this file adds Excel file monitoring for Unity Editor
    [InitializeOnLoad]
    [BGPlugin(Version = "1.1")]
    public static class BGDatabaseExcelImportEditorTool
    {
        private const string Key_Mode = "BGD.EEM.Mode";

        private static readonly BGEditorEMExecutor executor;
        private static Worker worker;


        static BGDatabaseExcelImportEditorTool()
        {
            executor = new BGEditorEMExecutor();
            AssignWorker();
        }

        private static void AssignWorker()
        {
            worker?.Dispose();
            var mode = BGEditorEMParameters.GetInt(Key_Mode);
            switch (mode)
            {
                case 0:
                {
                    worker = new BGEditorEMSingleFile(executor);
                    break;
                }
                case 1:
                {
                    worker = new BGEditorEMJobs(executor);
                    break;
                }
                default:
                {
                    throw new Exception("Unsupported mode=" + mode);
                }
            }
        }

        [MenuItem("Window/BGDatabaseExcelTools")]
        public static void Open() => ToolsWindow.Instance.Show();

        private class ToolsWindow : EditorWindow
        {
            public static ToolsWindow Instance => GetWindow<ToolsWindow>(null, false);

            private BGButtonGroup group;

            private void OnGUI()
            {
                try
                {
                    if (group == null)
                    {
                        group = new BGButtonGroup(
                            new GUIContent[] { new GUIContent("Single file"), new GUIContent("Jobs based") },
                            new int[] { 100, 100 }, null
                        )
                        {
                            Current = BGEditorEMParameters.GetInt(Key_Mode)
                        };
                        group.StateChanged = () =>
                        {
                            BGEditorEMParameters.SetInt(Key_Mode, group.Current);
                            AssignWorker();
                        };
                    }

                    BGEditorUtility.TabPanel("Excel file monitor", () =>
                    {
                        BGEditorUtility.Horizontal(() =>
                        {
                            BGEditorUtility.Label("Mode", 80);
                            group.Gui();
                            GUILayout.FlexibleSpace();
                        });
                        worker.Gui();
                    });
                }
                catch (ExitGUIException)
                {
                    Repaint();
                    throw;
                }
            }
        }

        public interface Worker : IDisposable
        {
            void Gui();
        }
    }
}