using System;
using System.Drawing;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using SDL2;
using SlatedGameToolkit.Framework.Loaders;
using SlatedGameToolkit.Framework.Graphics.Render;
using SlatedGameToolkit.Framework.Graphics.Text;
using SlatedGameToolkit.Framework.Graphics.Textures;
using SlatedGameToolkit.Framework.Graphics.Window;
using SlatedGameToolkit.Framework.Input.Devices;
using SlatedGameToolkit.Framework.StateSystem;
using SlatedGameToolkit.Framework.StateSystem.States;
using StbTrueTypeSharp;

namespace SlatedGameToolkit.Tools.Utilities.Playground
{
    public class MainState : IState
    {
        private WindowContext window;
        private Camera2D camera;
        private MeshBatch renderer;
        private BitmapFont font;
        private Texture logoTexture, fillerTexture;
        private RectangleMesh logo, textureTester, untextured;
        private string lastPressed;

        public WindowContext CurrentWindow { get { return window;}}

        public bool Activate()
        {
            return true;
        }

        public bool Deactivate()
        {
            return true;
        }

        public void Deinitialize()
        {
            logoTexture.Dispose();
            fillerTexture.Dispose();
            font.Dispose();
            renderer.Dispose();
            window.Dispose();
        }

        public string getName()
        {
            return "main state";
        }

        public unsafe void Initialize(StateManager manager)
        {
            window = new WindowContext("SlatedGameToolkit Playground");
            camera = new Camera2D(2, 2);
            renderer = new MeshBatch(camera);

            logoTexture = TextureLoader.Load2DTexture("Resources/Playground/yhdnbgnc.png");
            logo = new RectangleMesh(logoTexture, Color.White);
            logo.Width = 0.5f;
            logo.Height = 0.5f;
            logo.X = -0.25f;
            logo.Y = -0.25f;


            fillerTexture = TextureLoader.Load2DTexture("Resources/Playground/filler.png");
            textureTester = new RectangleMesh(fillerTexture, Color.White);
            textureTester.Width = 0.15f;
            textureTester.Height = 0.15f;
            textureTester.X = 0.25f;

            untextured = new RectangleMesh(null, Color.Red);
            untextured.Width = 0.15f;
            untextured.Height = 0.1f;
            untextured.X = 0.25f;
            untextured.Y = - 0.15f;

            font = new BitmapFont("Resources/Playground/earwig_factory_rg.ttf");
            font.PixelHeight = 128;
            font.PrepareCharacterGroup("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray());
        }

        public void Render(double delta)
        {
            renderer.Begin(Matrix4x4.Identity, delta);
            renderer.Draw(logo);
            renderer.Draw(textureTester);
            renderer.Draw(untextured);
            font.WriteLine(renderer, 0.25f, -0.35f, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n1234567890", Color.White);
            renderer.End();
        }

        public void Update(double timeStep)
        {
            if (Keyboard.IsKeyPressed(SDL.SDL_Keycode.SDLK_DOWN)) {
                camera.MoveTo += new Vector2(0, (-0.25f)) * (float) timeStep;
            }
            if (Keyboard.IsKeyPressed(SDL.SDL_Keycode.SDLK_UP)) {
                camera.MoveTo += new Vector2(0, (0.25f)) * (float) timeStep;
            }
            if (Keyboard.IsKeyPressed(SDL.SDL_Keycode.SDLK_LEFT)) {
                camera.MoveTo += new Vector2((-0.25f) * (float) timeStep, 0);
            }
            if (Keyboard.IsKeyPressed(SDL.SDL_Keycode.SDLK_RIGHT)) {
                camera.MoveTo += new Vector2((0.25f) * (float) timeStep, 0);
            }
        }

        public void KeyPressed(SDL.SDL_Keycode keycode, bool pressed) {
            if (pressed) lastPressed = SDL.SDL_GetKeyName(keycode);
        }
    }
}