using System;
using System.Windows.Forms;
using OpenTK.Graphics.OpenGL;
using OpenTK;

namespace Blackjack
{
    public partial class Form1 : Form
    {
        private Card _tenOfHerts;
        private Card _jackOfClubs;
        private int _uProjMatrixLoc;
        private const float _worldHeight = 17f;

        public Form1()
        {
            InitializeComponent();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Save Game");
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Load Game");
        }

        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Restart Game");
        }

        private void glControl_Load(object sender, EventArgs e)
        {
            glControl.MakeCurrent();
            VertexBuffers.InitVertexBuffers();

            // Allows transperency with textures
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.Enable(EnableCap.Blend);

            int program = ShaderProgram.InitShadersAndGetProgram();

            const float cardSize = 9f;

            _tenOfHerts = new Card(program, "Textures/Cards/10_of_hearts.png");
            _tenOfHerts.X = -3f;
            _tenOfHerts.Y = -7f;
            _tenOfHerts.Angle = 12f;
            _tenOfHerts.Size = cardSize;

            _jackOfClubs = new Card(program, "Textures/Cards/jack_of_clubs2.png");
            _jackOfClubs.X = -3f;
            _jackOfClubs.Y = -6f;
            _jackOfClubs.Angle = -12f;
            _jackOfClubs.Size = cardSize;

            // Set the texture unit 0 to the sampler
            int samplerLocation = GL.GetUniformLocation(program, "uSampler");
            GL.Uniform1(samplerLocation, 0);

            _uProjMatrixLoc = GL.GetUniformLocation(program, "uProjMatrix");
            int uViewMatrixLoc = GL.GetUniformLocation(program, "uViewMatrix");

            // Define the view matrix
            Matrix4 viewMatrix = Matrix4.LookAt(
                new Vector3(0f, 0f, 10f),
                new Vector3(0f, 0f, 0f),
                new Vector3(0f, 1f, 0f));
            GL.UniformMatrix4(uViewMatrixLoc, false, ref viewMatrix);

            UpdateProjMatrix();
        }

        private void glControl_Paint(object sender, PaintEventArgs e)
        {
            GL.Viewport(0, 0, glControl.Width, glControl.Height);

            _tenOfHerts.Draw();
            _jackOfClubs.Draw();

            glControl.SwapBuffers();
        }

        private void glControl_Resize(object sender, EventArgs e)
        {
            UpdateProjMatrix();
        }

        private void UpdateProjMatrix()
        {
            float aspect = (float)glControl.Width / glControl.Height;
            float worldWidth = aspect * _worldHeight;
            // Define the project matrix
            Matrix4 projMatrix = Matrix4.CreateOrthographic(worldWidth, _worldHeight, 0.1f, 1000f);
            GL.UniformMatrix4(_uProjMatrixLoc, false, ref projMatrix);
        }
    }
}
