using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Reflection.Metadata;

namespace LunaLightXMG
{
    public class LunaLightGame : Game
    {
        // Class Objects
        private Player player;
        private PlayerInput playerInput;
        private GraphicsDeviceManager graphics;
        private Camera2D camera2D;
        private SpriteBatch spriteBatch;

        // Native retro scale
        private int retroWidth = 320;
        private int retroHeight = 180;
        
        // Test Render Target Handler
        private RenderHandler renderHandler;
        bool resizing;

        // Level Object
        private Level level;

        // Input
        // We store our input states so that we only poll once per frame, 
        // then we use the same input state wherever needed
        private KeyboardState keyboardState;

        public LunaLightGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;

            //Vector2 initPlayerPosition = new Vector2(163,0);
            //player = new Player(initPlayerPosition);
            //playerInput = new PlayerInput();


            Window.ClientSizeChanged += OnClientSizeChanged;
            Window.AllowUserResizing = true;
            IsMouseVisible = true;
        }
        
        protected override void Initialize()
        {
            // Set initial window size
            graphics.PreferredBackBufferWidth = 1920;
            graphics.PreferredBackBufferHeight = 1080;
            graphics.ApplyChanges();
            base.Initialize();
        }
        
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            renderHandler = new RenderHandler(GraphicsDevice, spriteBatch, retroWidth, retroHeight);

            if (level != null)
                level.Dispose();

            level = new Level(Services, spriteBatch, renderHandler);
        }
        
        protected override void Update(GameTime gameTime)
        {
            HandleInput(gameTime);
            level.Update(gameTime, keyboardState);
            base.Update(gameTime);
        }
        
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            level.Draw(gameTime, spriteBatch);
            base.Draw(gameTime);
        }

        #region Methods

        private void HandleInput(GameTime gameTime)
        {
            keyboardState = Keyboard.GetState();

            // Temporary quit game - eventually handle in a pause menu class
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            if (keyboardState.IsKeyDown(Keys.R))
                level.ResetGame();
        }

        private void OnClientSizeChanged(object sender, EventArgs eventArgs)
        {
            if (!resizing && Window.ClientBounds.Width > 0 && Window.ClientBounds.Height > 0)
            {
                resizing = true;
                renderHandler.CalculateRenderDestination();
                resizing = false;
            }
        }


        #endregion

    }
}
