using System;
using Monkey.Configuration;
using Monkey.Game;
using Monkey.UI;
using Monkey.UI.Windows;

namespace Monkey.Core
{

    public class PluginBehaviour : UnityEngine.MonoBehaviour
    {

        private const string APPLICATION_LABEL = "{0} Ver. {1} | [{2}] Open {3} Window, [{4}] Potato Mode: {5}, [{6}] Camera Mode: {7}, [{8}] Male Character Scaled: {9}";

        private bool isPotatoModeEnabled;
        private bool isMaleCharacterScaled;

        public PluginBehaviour() : base()
        {
            this.isPotatoModeEnabled = false;
            this.isMaleCharacterScaled = false;
        }

        public void Update()
        {
            this.HandleInput();

            CameraManager.Update();
        }

        public void OnGUI()
        {
            this.DrawApplicationLabel();

            UIManager.Draw();
        }

        private void HandleInput()
        {
            if (UnityEngine.Input.GetKeyDown(ConfigurationManager.PluginConfiguration.TogglePluginWindowKeyCode))
            {
                UIManager.GetWindow<PluginWindow>().PositionWindowAtBottomRight();
                UIManager.SetWindowVisibility<PluginWindow>(!UIManager.IsWindowVisible<PluginWindow>());
            }

            if (UnityEngine.Input.GetKeyDown(ConfigurationManager.PluginConfiguration.TogglePotatoModeKeyCode))
            {
                QualityManager.IsFogVolumeEnabled = this.isPotatoModeEnabled;
                QualityManager.IsAmbientOcclusionEnabled = this.isPotatoModeEnabled;
                QualityManager.IsBloomEnabled = this.isPotatoModeEnabled;
                QualityManager.IsDepthOfFieldEnabled = this.isPotatoModeEnabled;
                QualityManager.IsMotionBlurEnabled = this.isPotatoModeEnabled;

                this.isPotatoModeEnabled = !this.isPotatoModeEnabled;
            }

            if (UnityEngine.Input.GetKeyDown(ConfigurationManager.PluginConfiguration.ToggleCameraModeKeyCode))
            {
                switch (CameraManager.CameraMode)
                {

                    case CameraMode.Stiff: CameraManager.CameraMode = CameraMode.Unlocked; break;
                    case CameraMode.Unlocked: CameraManager.CameraMode = CameraMode.Stiff; break;

                }
            }

            if (UnityEngine.Input.GetKeyDown(ConfigurationManager.PluginConfiguration.ToggleMaleCharacterScaleKeyCode))
            {
                UnityEngine.GameObject maleCharacterGameObject = MaleCharacterManager.GetMaleCharacterGameObject();

                if (maleCharacterGameObject != null)
                {
                    maleCharacterGameObject.transform.localScale = (this.isMaleCharacterScaled ? new UnityEngine.Vector3(1f, 1f, 1f) : new UnityEngine.Vector3(1f, 1.1f, 1f));

                    this.isMaleCharacterScaled = !this.isMaleCharacterScaled;
                }
            }
        }

        private void DrawApplicationLabel()
        {
            string formattedApplicationLabel = String.Format(
                APPLICATION_LABEL,
                Settings.PLUGIN_NAME,
                Settings.PLUGIN_VERSION,
                ConfigurationManager.PluginConfiguration.TogglePluginWindowKeyCode,
                Settings.PLUGIN_NAME,
                ConfigurationManager.PluginConfiguration.TogglePotatoModeKeyCode,
                this.isPotatoModeEnabled,
                ConfigurationManager.PluginConfiguration.ToggleCameraModeKeyCode,
                CameraManager.CameraMode,
                ConfigurationManager.PluginConfiguration.ToggleMaleCharacterScaleKeyCode,
                this.isMaleCharacterScaled);

            UnityEngine.GUIContent applicationlabelGUIContent = new UnityEngine.GUIContent(formattedApplicationLabel);
            UnityEngine.Vector2 applicationLabelSize = UnityEngine.GUI.skin.label.CalcSize(applicationlabelGUIContent);

            UnityEngine.Rect applicationLabelDimensions = new UnityEngine.Rect(
                ((UnityEngine.Screen.width / 2) - (applicationLabelSize.x / 2f)),
                10f,
                applicationLabelSize.x,
                applicationLabelSize.y);

            UnityEngine.GUI.Label(applicationLabelDimensions, applicationlabelGUIContent);
        }

    }

}
