using System;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.Faces.Base;
using <<|=BOProjectName|>>;

namespace <<|=UIBaseProjectName|>>
{
    /// <summary>
    /// The ControlFactory for this particular project, to allow for dynamic creation of the controls in the project
    /// using a different ControlFactory for Win and VWG.
    /// </summary>
    public partial interface I<<|=ProjectName|>>ControlFactory : IControlFactory
    {
        I<<|=ControlName|>> Create<<|=ControlName|>>();
    }

    /// <summary>
    /// The Interface for the <<|=ControlName|>>, so that the control can be treated the same in Win and VWG.
    /// </summary>
    public interface I<<|=ControlName|>> : IControlHabanero, IFormControl
    {

    }

    /// <summary>
    /// The common code shared between the Win and VWG version of this control.  For any common behaviour, place code
    /// in this manager.  For any specific behaviour for a platform, place the code in the Win or VWG controls themselves.
    /// </summary>
    public class <<|=ControlName|>>Manager
    {
        private readonly I<<|=ControlName|>> _<<|=#ControlName|>>;
        private readonly I<<|=ProjectName|>>ControlFactory _controlFactory;

        /// <summary>
        /// Constructor that takes the control itself (which this object manages), and the Control factory for this project
        /// so that you can make compound controls using the other controls of this project.
        /// </summary>
        /// <param name="<<|=#ControlName|>>">The control itself</param>
        /// <param name="controlFactory">The factory used to create any controls that are to placed on this control</param>
        public <<|=ControlName|>>Manager(I<<|=ControlName|>> <<|=#ControlName|>>, I<<|=ProjectName|>>ControlFactory controlFactory)
        {
            if (controlFactory == null) throw new ArgumentNullException("controlFactory");
            this._<<|=#ControlName|>> = <<|=#ControlName|>>;
            this._controlFactory = controlFactory;
        }

        public void Initialise()
        {

            IReadOnlyGridControl <<|=#ClassName+|>>GridControl = SetupParentControl();
            IReadOnlyGridControl <<|=#ChildRelationshipName|>>GridControl = SetupChildControl();
			
			      // Add the grids to the control. The <<|=ClassName+|>> grid fills the top part of the control.  
            // Change the size here depending on how many items you want to see in this grid.  
            // The <<|=ChildRelationshipName|>> grid fills the rest of the control.
            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(_<<|=#ControlName|>>);
            <<|=#ClassName+|>>GridControl.Height = 200;
            layoutManager.AddControl(<<|=#ChildRelationshipName|>>GridControl, BorderLayoutManager.Position.Centre);
            layoutManager.AddControl(<<|=#ClassName+|>>GridControl, BorderLayoutManager.Position.North, true);

            // Connect the <<|=ClassName+|>> and <<|=ChildRelationshipName|>> grids so that when an item is selected
            // in the <<|=ClassName+|>> grid, the correct <<|=ChildRelationshipName|>> are shown in the child grid.
            <<|=#ClassName+|>>GridControl.Grid.BusinessObjectSelected += 
                delegate
                    {
                        <<|=ClassName|>> selected<<|=ClassName|>> = <<|=#ClassName+|>>GridControl.Grid.SelectedBusinessObject as <<|=ClassName|>>;
                        if (selected<<|=ClassName|>> == null) return;
                        <<|=#ChildRelationshipName|>>GridControl.BusinessObjectCollection = selected<<|=ClassName|>>.<<|=ChildRelationshipName|>>;
                    };
        }
        
        private IReadOnlyGridControl SetupParentControl()
        {
            // Create the <<|=ClassName+|>> read-only grid
            IReadOnlyGridControl <<|=#ClassName+|>>GridControl = _controlFactory.CreateReadOnlyGridControl();

            // Used to specify a custom UI def
            <<|=#ClassName+|>>GridControl.Initialise(ClassDef.Get<<<|=ClassName|>>>(), "<<|=UIDefName|>>");

            // Load the collection of <<|=ClassName+|>>.  You can set the criteria and order by values here.
            <<|=#ClassName+|>>GridControl.BusinessObjectCollection = Broker.GetBusinessObjectCollection<<<|=ClassName|>>>("", "");

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            <<|=#ClassName+|>>GridControl.FilterControl.Visible = false;
            //<<|=#ClassName+|>>GridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            //<<|=#ClassName+|>>GridControl.Buttons.ShowDefaultDeleteButton = true;

            return <<|=#ClassName+|>>GridControl;
        }

        private IReadOnlyGridControl SetupChildControl()
        {
            // Create the <<|=ChildRelationshipName|>> read-only grid to show the child items
            IReadOnlyGridControl <<|=#ChildRelationshipName|>>GridControl = _controlFactory.CreateReadOnlyGridControl();

            // Used to specify a custom UI def
            <<|=#ChildRelationshipName|>>GridControl.Initialise(ClassDef.Get<<<|=ChildClassName|>>>(), "<<|=ChildUIDefName|>>");

            // Creates an empty collection of <<|=ChildRelationshipName|>> to put in the child grid
            <<|=#ChildRelationshipName|>>GridControl.BusinessObjectCollection = new BusinessObjectCollection<<<|=ChildClassName|>>>();

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            <<|=#ChildRelationshipName|>>GridControl.FilterControl.Visible = false;
            //<<|=#ChildRelationshipName|>>GridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            //<<|=#ChildRelationshipName|>>GridControl.Buttons.ShowDefaultDeleteButton = true;

            return <<|=#ChildRelationshipName|>>GridControl;
        }
    }
}
