﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace SburbServer
{
    public partial class Game : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern uint SendMessage(IntPtr hWnd,
        uint Msg,
        uint wParam,
        uint lParam);
        private Cursor gameCursor; 
        private Tools currentlySelected = Tools.None;
        private List<Control> gristCacheItems = new List<Control>();
        private RenderEngine re;
        private bool isFullscreen = false;
        private bool mouseIsDown = false;
        private int previousX = 0;
        private int previousY = 0;
        
        public Game()
        {
            InitializeComponent();
            SetLabelFonts();
            timer1.Start();
            gameCursor = ActuallyLoadCursor("cursor2.cur");
            this.Cursor = gameCursor;
            //Disable progress bar animation
            SendMessage(progressBar1.Handle,
            0x400 + 16, //WM_USER + PBM_SETSTATE
            0x0003, //PBST_PAUSED
             0);
            gristCacheItems.Add(pictureBox5);
            gristCacheItems.Add(pictureBox6);
            gristCacheItems.Add(pictureBox7);
            gristCacheItems.Add(progressBar1);
            re = new RenderEngine(glControl1,label1);
            
        }

        private void Game_FormClosing(object sender, FormClosingEventArgs e)
        {
            Program.form.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Click!");
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            if(currentlySelected == Tools.Move)
            {
                pictureBox3.Image = Properties.Resources.move;
                currentlySelected = Tools.None;
                
            }
            else if(currentlySelected == Tools.None)
            {
                pictureBox3.Image = Properties.Resources.moveselected;
                currentlySelected = Tools.Move;
            }
        }

        private void label1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
        }

        private void SetLabelFonts()
        {
            //Create your private font collection object.
            PrivateFontCollection pfc = new PrivateFontCollection();

            //Select your font from the resources.
            //My font here is "Digireu.ttf"
            int fontLength = Properties.Resources.FONTSTUCK.Length;

            // create a buffer to read in to
            byte[] fontdata = Properties.Resources.FONTSTUCK;

            // create an unsafe memory block for the font data
            IntPtr data = Marshal.AllocCoTaskMem(fontLength);

            // copy the bytes to the unsafe memory block
            Marshal.Copy(fontdata, 0, data, fontLength);

            // pass the font to the font collection
            pfc.AddMemoryFont(data, fontLength);

            // free up the unsafe memory
            Marshal.FreeCoTaskMem(data);

            selectedToolLabel.Font = new Font(pfc.Families[0], selectedToolLabel.Font.Size);
            label1.Font = new Font(pfc.Families[0], label1.Font.Size);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            switch (currentlySelected)
            {
                case Tools.None:
                    selectedToolLabel.Text = " ";
                    break;
                case Tools.Move:
                    selectedToolLabel.Text = "MOVE";
                    break;
                case Tools.Revise:
                    selectedToolLabel.Text = "REVISE";
                    break;
                case Tools.GristCache:
                    selectedToolLabel.Text = "GRIST CACHE";
                    break;
            }
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            if (currentlySelected == Tools.Revise)
            {
                pictureBox4.Image = Properties.Resources.revise;
                currentlySelected = Tools.None;
                
            }
            else if(currentlySelected == Tools.None)
            {
                pictureBox4.Image = Properties.Resources.reviseselected;
                currentlySelected = Tools.Revise;
            }
            
        }

        public Cursor ActuallyLoadCursor(String path)
        {
            return new Cursor(LoadCursorFromFile(path));
        }
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern IntPtr LoadCursorFromFile(string fileName);

        private void SetGristCacheVisibility(bool isVisible)
        {
            foreach(Control c in gristCacheItems)
            {
                c.Visible = isVisible;
                c.Enabled = isVisible;
            }
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            if (currentlySelected == Tools.GristCache)
            {
                pictureBox8.Image = Properties.Resources.gristCache;
                currentlySelected = Tools.None;
                SetGristCacheVisibility(false);
            }
            else if (currentlySelected == Tools.None)
            {
                pictureBox8.Image = Properties.Resources.gristCacheSelected;
                currentlySelected = Tools.GristCache;
                SetGristCacheVisibility(true);
            }
        }

        private void Game_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyData == Keys.F11)
            {
                if(!isFullscreen)
                {
                    this.TopMost = true;
                    this.FormBorderStyle = FormBorderStyle.None;
                    this.Bounds = Screen.FromControl(this).Bounds;
                    isFullscreen = true;
                    this.Invalidate();
                }
                else
                {
                    this.FormBorderStyle = FormBorderStyle.Sizable;
                    this.WindowState = FormWindowState.Normal;
                    this.TopMost = false;
                    this.Size = new Size(1168, 591);
                    isFullscreen = false;
                    this.Invalidate();
                }
                
            }
        }

        private void Game_MouseDown(object sender, MouseEventArgs e)
        {
            mouseIsDown = true;
        }

        private void Game_MouseUp(object sender, MouseEventArgs e)
        {
            mouseIsDown = false;
        }

        private void Game_MouseMove(object sender, MouseEventArgs e)
        {
            int diffX = previousX - e.X;
            int diffY = previousY - e.Y;
            if(mouseIsDown)
            {
                re.TranslateBy(diffX,diffY);
                previousX = e.X;
                previousY = e.Y;
            }
        }
    }

    public enum Tools
    {
        None,
        Move,
        Revise,
        Deploy,
        PRegistry,
        GristCache,
        Atheneum,
        Alchemize
    }
}
