using GameApp.Entities;
using GameApp.Helper;
using GameApp.Model;
using Khronos;
using OpenGL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GameApp
{
    public partial class MainForm : Form
    {
        DisplayManager _displayManager = new DisplayManager();

        RawModel _model;
        Loader _loader;
        Renderer _renderer;
        StaticShader _shader;

        ModelTexture _texture;
        TextureModel _staticModel;

        Camera _camera;
        Entity _entity;

        public MainForm()
        {
            InitializeComponent();
            this.glControl.DepthBits = ((uint)(24u));
        }

        private void glControl_ContextCreated(object sender, OpenGL.GlControlEventArgs e)
        {
            GlControl glControl = (GlControl)sender;
            _displayManager.createDisplay(glControl);

            bool result = Soil.NET.WrapSOIL.Initialize();
            if (result == false)
            {
                MessageBox.Show("SOIL: Not initialized: " + Soil.NET.WrapSOIL.GetSoilLastError());
                return;
            }

            _loader = new Loader();

            // _model = _loader.loadToVAO(_vertices, _textureCoords, _indices);

            // _model = OBJLoader.loadObjModel("stall", _loader);
            // _model = OBJLoader.loadObjModel2("stall", _loader);

            _texture = new ModelTexture(_loader.loadTexture("stallTexture"));

            _model = OBJLoader.loadObjModel2("wooden watch tower2", _loader);
            _texture = new ModelTexture(_loader.loadTexture("Wood_Tower_Col"));

            _staticModel = new TextureModel(_model, _texture);
            _shader = new StaticShader();
            _renderer = new Renderer(_shader, glControl.ClientSize.Width, glControl.ClientSize.Height);

            _camera = new Camera();
            // _entity = new Entity(_staticModel, new Vertex3f(0, 0, -2), 0, 0, 0, 1);
            _entity = new Entity(_staticModel, new Vertex3f(0, -2, -10), 0, 0, 0, 1);
        }

        private void glControl_ContextUpdate(object sender, GlControlEventArgs e)
        {
            Control glControl = (Control)sender;

            _renderer.SetViewRect(glControl.ClientSize.Width, glControl.ClientSize.Height);

            // _entity.increaseRotation(1, 1, 0);
            _entity.increaseRotation(0, 1, 0);

            _camera.Move();
        }

        private void glControl_ContextDestroying(object sender, GlControlEventArgs e)
        {
            _loader.CleanUp();
            _shader.CleanUp();
        }

        private void glControl_Render(object sender, OpenGL.GlControlEventArgs e)
        {
            _renderer.Prepare();

            _shader.Start();
            {
                _shader.loadViewMatrix(_camera);
                _renderer.Render(_entity, _shader);
            }
            _shader.Stop();

            _displayManager.updateDisplay();
        }
    }
}
