﻿using UnityEngine;
using UnityEditor;
using KAPStuff;

public class MainUI : EditorWindow
{
    static GameObject source;
    static GameObject destination;
    static GameObject beatfindersource;

    public bool fingerpoint = false;
    public bool fist = false;
    public bool victory = false;
    public bool handgun = false;
    public bool thumbsup = false;
    public bool rocknroll = false;
    public bool handopen = false;

    public bool gameobjects = true;
    public bool particle_system = true;
    public bool dynamic_bone = true;

    public static Color background;
    public static Color buttons;

    static TextAsset beatfinder;

    int toolbarInt = 0;

    private static Texture2D tex;
    static Vector2 scrollPosition = Vector2.zero;

    //BeatFinder options
    static Color beatcolor = Color.white;
    static Color betweencolor = Color.white;
    static float zoom = 1.013f;
    static float blur = 1;
    static AudioClip audio;

    [MenuItem("Korikas Avatar Plugin/Main")]
    public static void ShowWindow()
    {
        EditorWindow window = EditorWindow.GetWindow<MainUI>("KAP v3.0.0");
        window.minSize = new Vector2(265, 265);
    }
    void OnGUI()
    {
        tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
        initColor();
        tex.SetPixel(0, 0, background);
        tex.Apply();
        GUI.DrawTexture(new Rect(0, 0, position.width, position.height), tex, ScaleMode.StretchToFill);
        GUI.Label(new Rect(200, 200, 100, 100), "");
        GUI.TextField(new Rect(20, 20, 70, 30), "");
        GUI.backgroundColor = buttons;
        Texture logo = (Texture)AssetDatabase.LoadAssetAtPath("Assets/KorikasAvatarPlugin/Korikas Avatar Plugin/logo.png", typeof(Texture));
        GUI.DrawTexture(new Rect(0, 0, position.width, 70), logo, ScaleMode.ScaleAndCrop);

        string[] toolbarString = { "gestures", "generate", "copy", "credits" };
        toolbarInt = GUILayout.Toolbar(toolbarInt, toolbarString);

        GUILayout.Space(50);
        scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, true, GUILayout.Width(position.width), GUILayout.Height(position.height - 74));
        switch (toolbarInt)
        {
            case 0:
                toolbarInt = 0;
                RenderGestureTab();
                break;
            case 1:
                toolbarInt = 1;
                renderGenerationTab();
                break;
            case 2:
                toolbarInt = 2;
                RenderCopyContentsTab();
                break;
            case 3:
                RenderCreditsTab();
                break;
        }
        EditorGUILayout.EndScrollView();
    }

    public void renderGenerationTab()
    {
        GUILayout.Label("generate beatfinder animation", EditorStyles.boldLabel);
        beatfinder = (TextAsset)EditorGUILayout.ObjectField("your beatfinder file:", beatfinder, typeof(TextAsset), true);
        audio = (AudioClip)EditorGUILayout.ObjectField("your audio:", audio, typeof(AudioClip), true);
        bool beatbutton = false;
        beatcolor = EditorGUILayout.ColorField("beatcolor:", beatcolor);
        betweencolor = EditorGUILayout.ColorField("color:", betweencolor);
        GUILayout.Label("zoom:");
        zoom = EditorGUILayout.Slider(zoom, 1, 2);
        GUILayout.Label("blur:");
        blur = EditorGUILayout.Slider(blur, 0, 5);
        if (!(audio == null || beatfinder == null))
        {
            beatbutton = GUILayout.Button("generate beatfinder animation");
        }
        else
        {
            GUI.enabled = false;
            beatbutton = GUILayout.Button("somethings missing");
            GUI.enabled = true;
        }
        if (beatbutton)
        {
            BeatFinder.generateBeatAnimation(beatfinder, zoom, blur, beatcolor, betweencolor, audio);
        }
    }

    public void RenderCopyContentsTab()
    {
        string label = "select your avatar:";
        GUILayout.Label("source", EditorStyles.boldLabel);
        source = (GameObject)EditorGUILayout.ObjectField(label, source, typeof(GameObject), true);
        GUILayout.Label("destination", EditorStyles.boldLabel);
        destination = (GameObject)EditorGUILayout.ObjectField(label, destination, typeof(GameObject), true);
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        gameobjects = EditorGUILayout.Toggle("GameObjects", gameobjects);
        if (gameobjects)
        {
            GUI.enabled = false;
            particle_system = EditorGUILayout.Toggle("Particle Systems", true);
            GUI.enabled = true;
        }
        else
        {
            particle_system = EditorGUILayout.Toggle("Particle Systems", particle_system);
        }
        dynamic_bone = EditorGUILayout.Toggle("Dynamic Bones", dynamic_bone);
        if (source != null && destination != null)
        {
            bool copy = GUILayout.Button("copy contents!");
            if (copy)
            {
                bool[] options = { gameobjects, particle_system, dynamic_bone };
                AvatarContentsCopy.copy(source, destination, options);
            }
        }
        else
        {
            GUI.enabled = false;
            GUILayout.Button("please select 2 GameObjects");
            GUI.enabled = true;
        }
        GUI.enabled = false;
        GUILayout.Label("It only copies mapped bones childs");
        GUILayout.Label("It wont copy contents inside the fingers");
        GUI.enabled = true;
    }
    public void RenderCreditsTab()
    {
        GUILayout.Label("These tools been coded by Korika!", EditorStyles.boldLabel);
        bool discord = GUILayout.Button("Discord");
        if (discord)
        {
            Application.OpenURL("https://discord.gg/z2SmFqg");
        }
        bool vrcmods = GUILayout.Button("VRCMods");
        if (vrcmods)
        {
            Application.OpenURL("https://vrcmods.com/user/Korika");
        }
        bool youtube = GUILayout.Button("YouTube");
        if (youtube)
        {
            Application.OpenURL("https://www.youtube.com/channel/UCRyYgGgXM6ugIjfmgQ6Go3A?view_as=subscriber");
        }
        bool github = GUILayout.Button("GitHub");
        if (github)
        {
            Application.OpenURL("https://github.com/KorikaDEV");
        }
    }
    public void disableEveryCheck()
    {
        fingerpoint = false;
        fist = false;
        victory = false;
        handgun = false;
        thumbsup = false;
        rocknroll = false;
        handopen = false;
    }
    public void RenderGestureTab()
    {
        if (EditorApplication.isPlaying)
        {
            fingerpoint = EditorGUILayout.Toggle("fingerpoint", fingerpoint);
            if (fingerpoint)
            {
                disableEveryCheck();
                fingerpoint = true;
                string s = "FINGERPOINT";
                GestureDisplay.show(s);
            }
            fist = EditorGUILayout.Toggle("fist", fist);
            if (fist)
            {
                disableEveryCheck();
                fist = true;
                string s = "FIST";
                GestureDisplay.show(s);
            }
            victory = EditorGUILayout.Toggle("victory", victory);
            if (victory)
            {
                disableEveryCheck();
                victory = true;
                string s = "VICTORY";
                GestureDisplay.show(s);
            }
            handgun = EditorGUILayout.Toggle("handgun", handgun);
            if (handgun)
            {
                disableEveryCheck();
                handgun = true;
                string s = "HANDGUN";
                GestureDisplay.show(s);
            }
            thumbsup = EditorGUILayout.Toggle("thumbsup", thumbsup);
            if (thumbsup)
            {
                disableEveryCheck();
                thumbsup = true;
                string s = "THUMBSUP";
                GestureDisplay.show(s);
            }
            rocknroll = EditorGUILayout.Toggle("rocknroll", rocknroll);
            if (rocknroll)
            {
                disableEveryCheck();
                rocknroll = true;
                string s = "ROCKNROLL";
                GestureDisplay.show(s);
            }
            handopen = EditorGUILayout.Toggle("handopen", handopen);
            if (handopen)
            {
                disableEveryCheck();
                handopen = true;
                string s = "HANDOPEN";
                GestureDisplay.show(s);
            }
            GUILayout.Label("check to play the animation", EditorStyles.boldLabel);
        }
        else
        {
            disableEveryCheck();
            GUI.enabled = false;
            fingerpoint = EditorGUILayout.Toggle("fingerpoint", fingerpoint);
            fist = EditorGUILayout.Toggle("fist", fist);
            victory = EditorGUILayout.Toggle("victory", victory);
            handgun = EditorGUILayout.Toggle("handgun", handgun);
            thumbsup = EditorGUILayout.Toggle("thumbsup", thumbsup);
            rocknroll = EditorGUILayout.Toggle("rocknroll", rocknroll);
            handopen = EditorGUILayout.Toggle("handopen", handopen);
            GUILayout.Label("play animations in playmode!", EditorStyles.boldLabel);
            GUI.enabled = true;
        }
    }
    public static void initColor()
    {
        if (EditorGUIUtility.isProSkin)
        {
            background = MainContainer.fromRGB(124f, 96f, 77f);
            buttons = MainContainer.fromRGB(193f, 144f, 92f);
        }
        else
        {
            background = MainContainer.fromRGB(209f, 189f, 169f);
            buttons = MainContainer.fromRGB(203f, 164f, 112f);
        }
    }
}
