using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using QuadEngine;
using Bass;
using HumanLikeBattleRobots.Logic;
using HumanLikeBattleRobots.Control;

namespace HumanLikeBattleRobots
{
    public partial class MainForm : Form
    {
        private IQuadDevice quadDevice;
        private IQuadRender quadRender;
        private IQuadTimer quadTimer;

        private TimerProcedure timer;

        private GameManager gm;

        public MainForm()
        {
            InitializeComponent();

            bool bassInit = Bass.Bass.BASS_Init(1, 44100, 0, this.Handle, IntPtr.Zero);
            bool bassStart = Bass.Bass.BASS_Start();

            QuadEngine.QuadEngine.CreateQuadDevice(out quadDevice);
            quadDevice.CreateRender(out quadRender);

            this.SetClientSizeCore(1024, 768);

            quadRender.Initialize(this.Handle, 1024, 768, false);
            quadRender.SetTextureAdressing(TQuadTextureAdressing.qtaWrap);

            if (SystemInformation.PrimaryMonitorSize.Height == 768)
            {
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.StartPosition = FormStartPosition.CenterScreen;
                this.Location = new Point(this.Location.X, 0);
            }
            else
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;

            //Cursor.Hide();

            // инициализация контролов и логики
            gm = new GameManager(quadDevice, quadRender);

            Keyboard.Initialize();
            Mouse.Initialize();

            quadDevice.CreateTimer(out quadTimer);
            quadTimer.SetInterval(16);
            timer = (TimerProcedure)OnTimer;
            quadTimer.SetCallBack(Marshal.GetFunctionPointerForDelegate(timer));
            quadTimer.SetState(true);
        }

        private void OnTimer(ref double delta, UInt32 Id)
        {
            gm.Proceed(delta);

            quadRender.BeginRender();
            quadRender.Clear(QuadColor.Black);


            quadRender.SetBlendMode(TQuadBlendMode.qbmSrcAlpha);
            gm.Draw();
            //GraphicResources.TestFont.TextOut(960, 30, 1, quadTimer.GetFPS().ToString());
            quadRender.EndRender();
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            Mouse.Move(e.X, e.Y);
        }

        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            Mouse.SetButtonDown(e.Button);
        }

        private void MainForm_MouseUp(object sender, MouseEventArgs e)
        {
            Mouse.SetButtonUp(e.Button);
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
            Keyboard.SetKeyDown(e.KeyCode);
        }

        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            Keyboard.SetKeyUp(e.KeyCode);
        }
    }
}
