using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace Bil_LD
{
    public partial class FormLD : Form
    {
        private GameEngine _GameEngine;
        public ViewControl TopView;

        public FormLD(GameEngine gameengine)
        {
            Control.CheckForIllegalCrossThreadCalls = false;

            _GameEngine = gameengine;

            TopView = new ViewControl(gameengine, this);
            TopView.Location = new Point(20, 20);
            TopView.Size = new Size(400, 400);
            Controls.Add(TopView);

            InitializeComponent();

            RefreshGameObjects();

            lboxGameModels.KeyUp += new KeyEventHandler(lboxGameObjects_KeyUp);
        }

        void lboxGameObjects_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.A)
                _GameEngine.Camera_Position.X -= 10;
            else if (e.KeyData == Keys.D)
                _GameEngine.Camera_Position.X += 10;

            if (e.KeyData == Keys.W)
                _GameEngine.Camera_Position.Z += 10;
            else if (e.KeyData == Keys.S)
                _GameEngine.Camera_Position.Z -= 10;

            if (e.KeyData == Keys.Up)
                _GameEngine.Camera_Position.Y += 5;
            else if (e.KeyData == Keys.Down)
                _GameEngine.Camera_Position.Y -= 5;

            Console.WriteLine("Pos: " + _GameEngine.Camera_Position.X + "," + _GameEngine.Camera_Position.Y + "," + _GameEngine.Camera_Position.Z);
        }

        public void RefreshGameObjects()
        {
            System.IO.DirectoryInfo folder_info = new System.IO.DirectoryInfo(Settings.GetSingleton().MediaPath);

            foreach (System.IO.FileInfo file_info in folder_info.GetFiles())
            {
                if (file_info.Name.ToLower().Contains(".x"))
                    lboxGameModels.Items.Add(file_info.Name);
            }
        }

        public void RefreshCurrentGameObjects()
        {
            gboxGameObjectEdit.Enabled = false;
            _GameEngine.SelectedObject = null;
            lboxGameObjectsCurrent.Items.Clear();
            lboxGameObjectsCurrent.Items.Add("None");

            foreach (GameObject go in _GameEngine._RenderableItems)
            {
                lboxGameObjectsCurrent.Items.Add(go);
            }
        }

        private void lboxGameObjects_MouseDown(object sender, MouseEventArgs e)
        {
            int index = lboxGameModels.IndexFromPoint(e.X, e.Y);

            if (index != -1)
                DoDragDrop(lboxGameModels.Items[index].ToString(), DragDropEffects.Copy);
        }

        private void lboxGameObjectsCurrent_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lboxGameObjectsCurrent.SelectedItem != null)
            {
                if (lboxGameObjectsCurrent.SelectedItem.ToString().Equals("None"))
                {
                    _GameEngine.SelectedObject = null;
                    lblSelected.Text = "Selected:";
                    gboxGameObjectEdit.Enabled = false;
                }
                else
                {
                    _GameEngine.SelectedObject = (GameObject)lboxGameObjectsCurrent.SelectedItem;
                    lblSelected.Text = "Selected: " + _GameEngine.SelectedObject;
                    gboxGameObjectEdit.Enabled = true;

                    tboxYaw.Text = "" + _GameEngine.SelectedObject.Yaw;
                    tboxPitch.Text = "" + _GameEngine.SelectedObject.Pitch;
                    tboxRoll.Text = "" + _GameEngine.SelectedObject.Roll;

                    tboxScaleX.Text = "" + _GameEngine.SelectedObject.Scale.X;
                    tboxScaleY.Text = "" + _GameEngine.SelectedObject.Scale.Y;
                    tboxScaleZ.Text = "" + _GameEngine.SelectedObject.Scale.Z;
                }
            }
        }

        private void buttonGameObjectUpdate_Click(object sender, EventArgs e)
        {
            _GameEngine.SelectedObject.Yaw = float.Parse(tboxYaw.Text);
            _GameEngine.SelectedObject.Pitch = float.Parse(tboxPitch.Text);
            _GameEngine.SelectedObject.Roll = float.Parse(tboxRoll.Text);

            _GameEngine.SelectedObject.Scale = new Microsoft.DirectX.Vector3(
                float.Parse(tboxScaleX.Text),
                float.Parse(tboxScaleY.Text),
                float.Parse(tboxScaleZ.Text));
            
        }

        private void ValidateInput(object sender, CancelEventArgs e)
        {
            TextBox tbox = (TextBox)sender;

            float parsed = 0;

            e.Cancel = !float.TryParse(tbox.Text, out parsed);

            if (e.Cancel)
                MessageBox.Show("Ikke en gyldig float værdi!");
        }
    }
}