using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageBlockz.Core;
using ImageBlockz.Api;

namespace ImageBlockz.Gui
{
    public partial class OperationBlock : UserControl
    {
        private IOperation operation;
        private OperationControl control;

        private event EventHandler blockUp;
        private event EventHandler blockDown;

        private event EventHandler blockDelete;
        
        public MainWindow MainWindow
        {
            get;
            set;
        }

        public IOperation Operation
        {
            get
            {
                return operation;
            }
        }

        public OperationBlock()
        {
            InitializeComponent();
        }

        public void Init(IOperation operation)
        {
            control = ControlFactory.GetDefaultFactory().CreateControl(operation.GetType().FullName);

            if (control == null)
            {
                btnEdit.Visible = false;
                btnEdit.Enabled = false;
            }

            labelBlockCaption.Text = operation.GetFormattedName();
            labelProperties.Text = operation.GetInfo();

            switch (operation.GetOperationType())
            {
                case OperationType.Input:
                    panel.BackColor = Color.FromArgb(181, 244, 94);
                    break;
                case OperationType.Filter:
                    panel.BackColor = Color.FromArgb(254, 230, 92);
                    break;
                case OperationType.Output:
                    panel.BackColor = Color.Salmon;
                    break;
            }

            this.operation = operation;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            control.SetOperation(operation);
            control.IsNew = false;

            OperationDialog dialog = MainWindow.EditDialog;

            dialog.SetControl(control);
            dialog.ShowDialog(false);

            labelProperties.Text = operation.GetInfo();
        }

        private void btnBlockUp_Click(object sender, EventArgs e)
        {
            if (blockUp != null)
            {
                blockUp(this, e);
            }
        }

        private void btnBlockDown_Click(object sender, EventArgs e)
        {
            if (blockDown != null)
            {
                blockDown(this, e);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (blockDelete != null)
            {
                blockDelete(this, e);
            }
        }

        public event EventHandler BlockUp
        {
            add
            {
                blockUp += value;
            }
            remove
            {
                blockUp -= value;
            }
        }

        public event EventHandler BlockDown
        {
            add
            {
                blockDown += value;
            }
            remove
            {
                blockDown -= value;
            }
        }

        public event EventHandler BlockDelete
        {
            add
            {
                blockDelete += value;
            }
            remove
            {
                blockDelete -= value;
            }
        }
    }
}
