using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Aga.Controls.Tree;
using MySQL.Grt;
using MySQL.Utilities;

namespace MySQL.GUI.Workbench.Plugins
{
  public partial class ImageEditor : ObjectEditorPlugin
  {
    #region Member Variables

    private MySQL.Grt.ImageEditorBE imageEditorBE { get { return editorBE as MySQL.Grt.ImageEditorBE; } }
    private int originalWidth, originalHeight;

    #endregion

    #region Constructors

    public ImageEditor()
    {
      InitializeComponent();
    }

    public ImageEditor(GrtManager GrtManager, GrtValue GrtObject)
      : base(new MySQL.Grt.ImageEditorBE(GrtManager, GrtObject))
    {
      InitializeComponent();

      // Init BE

      InitializingControls = true;
      try
      {
        RefreshFormData(); 
      }
      finally
      {
        InitializingControls = false;
      }

      //TabText = nameTextBox.Text;
    }

    #endregion

    #region ObjectEditorPlugin Overrides

    public override bool ChangeGrtList(GrtManager GrtManager, GrtValue GrtList)
    {
      InitializingControls = true;
      SuspendLayout();

      try
      {
        grtManager = GrtManager;
        grtList = GrtList;

        if (editorBE != null)
          editorBE.disable_auto_refresh();
        editorBE = new MySQL.Grt.ImageEditorBE(GrtManager, GrtList);

        CallRefreshFormData();
      }
      finally
      {
        ResumeLayout();
        InitializingControls = false;
      }

      imageEditorBE.add_refresh_ui_handler(RefreshFormDataInvoke);

      Invalidate();

      return true;
    }

    #endregion

    #region Form implemenation

    protected override void RefreshFormData()
    {
      InitializingControls = true;
      nameTextBox.Text = imageEditorBE.get_filename();
      int w, h;
      imageEditorBE.get_size(out w, out h);
      widthText.Text = w.ToString();
      heightText.Text = h.ToString();

      keepAspectCheck.Checked = imageEditorBE.get_keep_aspect_ratio();

      String path= imageEditorBE.get_attached_image_path();

      Image image = Image.FromFile(path);
      originalWidth = image.Width;
      originalHeight = image.Height;
      pictureBox2.Image = image;

      InitializingControls = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      openFileDialog1.Title = "Select PNG Image";
      openFileDialog1.Filter = "PNG Images (*.png)|*.png";
			openFileDialog1.FileName = "";
      openFileDialog1.RestoreDirectory = true;
      if (openFileDialog1.ShowDialog() == DialogResult.OK)
      {
        imageEditorBE.set_filename(openFileDialog1.FileName);
        RefreshFormData();
      }
    }

    private void resetSizeButton_Click(object sender, EventArgs e)
    {
      imageEditorBE.set_size(originalWidth, originalHeight);

      RefreshFormData();
    }

    private void widthText_KeyPress(object sender, KeyPressEventArgs e)
    {
      if (e.KeyChar == '\r')
      {
        if (!InitializingControls)
        {
          int w = Convert.ToInt32(widthText.Text);
          imageEditorBE.set_width(w);
          RefreshFormData();
        }
      }
    }

    private void heightText_KeyPress(object sender, KeyPressEventArgs e)
    {
      if (e.KeyChar == '\r')
      {
        if (!InitializingControls)
        {
          int h = Convert.ToInt32(heightText.Text);
          imageEditorBE.set_height(h);
          RefreshFormData();
        }
      }
    }

    private void keepAspectCheck_CheckedChanged(object sender, EventArgs e)
    {
      if (!InitializingControls)
      {
        imageEditorBE.set_keep_aspect_ratio(keepAspectCheck.Checked);
        RefreshFormData();
      }
    }

    private void widthText_Leave(object sender, EventArgs e)
    {
      if (!InitializingControls)
      {
        int w = Convert.ToInt32(widthText.Text);
        imageEditorBE.set_width(w);
        RefreshFormData();
      }
    }

    private void heightText_Leave(object sender, EventArgs e)
    {
      if (!InitializingControls)
      {
        int h = Convert.ToInt32(heightText.Text);
        imageEditorBE.set_height(h);
        RefreshFormData();
      }
    }

    #endregion
  }
}