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

namespace InterfaceOpenGL
{
    public partial class FormCamera : Form, DiagramUI.IUpdatableForm
    {
        private Panel pan = null;
        private OpenGLCamera camera;
        private bool first = true;
        private FormCamera()
        {
            InitializeComponent();
        }

        internal FormCamera(OpenGLCamera camera)
            : this()
        {
            this.camera = camera;
            ResourceService.Resources.LoadControlResources(this);
            pan = panelDesktopCenter;
            camera.Graphics = Graphics.FromHwnd(pan.Handle);
            pan.BackColor = Color.FromArgb(0, 0, 255);
            UpdateFormUI();
        }

        #region IUpdatableForm Members

        public void UpdateFormUI()
        {
            CategoryTheory.IAssociatedObject ao = camera;
            object o = ao.Object;
            if (o == null)
            {
                return;
            }
            if (!(o is DiagramUI.INamedComponent))
            {
                return;
            }
            DiagramUI.INamedComponent nc = o as DiagramUI.INamedComponent;
            Text = nc.Name;
        }

        #endregion


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (first)
            {
                camera.Prepare(camera.Graphics, 0);
                first = false;
            }
            camera.UpdateImage();
        }


        private void save(string filename)
        {
            BitmapConsumer.IBitmapProvider p = camera;
            Bitmap bmp = p.Bitmap;
            bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp);
        }

        private void saveAs()
        {
            if (saveFileDialogBmp.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }
            save(saveFileDialogBmp.FileName);
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            saveAs();
        }

    }
}