using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VisimBox.Renderer
{
    public class BaseRenderWindow : Form, IRenderWindow
    {
        IRenderContext renderc;
        Control NativeWin;

        public BaseRenderWindow(RenderContextSettings settings)
        {
            if ((settings.WindowTitle == null) || (settings.WindowTitle == ""))
                settings.WindowTitle = "VISIMBOX OPENGL RENDER WINDOW";
            this.Text = settings.WindowTitle;
            if (settings.Width == 0)
                settings.Width = 640;
            if (settings.Height == 0)
                settings.Height = 480;
            
            this.ClientSize = new System.Drawing.Size(settings.Width, settings.Height);
            
            settings.SetIRW(this);


            //TODO: use other parameters too!
/*      
            FullScreen = false;
            FullScreenSwitch = true;
            Resizable = true;
            KeepAspectRatio = false;
            var size = win.ClientRectangle;
            Width = win.Width;
            Height = win.Height;
            MinWidth = 0;
            MinHeight = 0;
            MaxWidth = 0;
            MaxHeight = 0;
*/

        }

        protected override bool IsInputKey(Keys keyData)
        {
            if ((keyData == Keys.Left) || (keyData == Keys.Up) || (keyData == Keys.Right) || (keyData == Keys.Down))
                return false;

            return base.IsInputKey(keyData);
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            if (renderc != null)
            {
                renderc.Renderer.RemoveContext(renderc);
            }
            if (NativeWin!= null)
                NativeWin.Dispose();
        }

        #region IRenderWindow Members

        public IRenderContext RenderContext
        {
            get { return renderc; }
        }

        #endregion

        #region IRenderWindow Members


        public void AttachContext(IRenderContext rc, Control nativewin)
        {
            renderc = rc;
            NativeWin = nativewin;

            NativeWin.MouseDown += new MouseEventHandler(NativeWin_MouseDown);
            NativeWin.MouseUp += new MouseEventHandler(NativeWin_MouseUp);
            NativeWin.MouseMove += new MouseEventHandler(NativeWin_MouseMove);
            NativeWin.KeyDown += new KeyEventHandler(NativeWin_KeyDown);
            NativeWin.KeyPress += new KeyPressEventHandler(NativeWin_KeyPress);
            NativeWin.KeyUp += new KeyEventHandler(NativeWin_KeyUp);
            NativeWin.MouseWheel += new MouseEventHandler(NativeWin_MouseWheel);
            NativeWin.MouseClick += new MouseEventHandler(NativeWin_MouseClick);
            NativeWin.MouseDoubleClick += new MouseEventHandler(NativeWin_MouseDoubleClick);
            NativeWin.MouseHover += new EventHandler(NativeWin_MouseHover);
            //this.components.Add(nativewin);
        }

        void NativeWin_MouseHover(object sender, EventArgs e)
        {
            OnMouseHover(e);
        }

        void NativeWin_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            OnMouseDoubleClick(e);
        }

        void NativeWin_MouseClick(object sender, MouseEventArgs e)
        {
            OnMouseClick(e);
        }

        void NativeWin_MouseWheel(object sender, MouseEventArgs e)
        {
            OnMouseWheel(e);
        }

        void NativeWin_KeyUp(object sender, KeyEventArgs e)
        {
            OnKeyUp(e);
        }

        void NativeWin_KeyPress(object sender, KeyPressEventArgs e)
        {
            OnKeyPress(e);            
        }

        void NativeWin_KeyDown(object sender, KeyEventArgs e)
        {
            OnKeyDown(e);
        }

        void NativeWin_MouseMove(object sender, MouseEventArgs e)
        {
            OnMouseMove(e);
        }

        void NativeWin_MouseUp(object sender, MouseEventArgs e)
        {
            OnMouseUp(e);
        }

        void NativeWin_MouseDown(object sender, MouseEventArgs e)
        {
            OnMouseDown(e);
        }

        #endregion
    
    
    }
}
