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

namespace Fork
{
    public partial class ForkGuiActionBar : UserControl
    {

        private Fork.Actions.ActionContext _Context;

        private Fork.Controls.ForkLayerEditor FLE;

        public void SetFLE(Fork.Controls.ForkLayerEditor fle)
        {
            FLE = fle;
        }

        public ForkGuiActionBar()
        {
            InitializeComponent();
        }

        private object _ContextOperand;

        public void MapCommand(string x)
        {
            Button B = new Button();
            B.Text = x;
            B.Width = 7 * x.Length + 10;

            B.Click += new System.EventHandler(this.Command_Click);
            B.UseVisualStyleBackColor = true;
            
            flowPanel.Controls.Add(B);
        }

        public void BuiltContextAndInit(ForkDOM.Layer Host, object o)
        {
            _Context = new Fork.Actions.ActionContext();
            _ContextOperand = o;
            _Context.HostingLayer = Host;
            flowPanel.Controls.Clear();
            foreach (Actions.IAction A in ZenReflection.InterfaceImplementors("Fork.Actions.IAction"))
            {
                if (A.CanAct(_Context, o))
                {
                    MapCommand(A.Name());
                }
            }
        }

        public void EvalCommand(string x)
        {
            foreach (Actions.IAction A in ZenReflection.InterfaceImplementors("Fork.Actions.IAction"))
            {
                if (A.Name().Equals(x))
                {
                    A.Act(_Context, _ContextOperand);
                    FLE.ReGenerate();
                }
            }
        }


        private void Command_Click(object sender, EventArgs e)
        {
            EvalCommand((sender as Button).Text);
        }
    }
}
