using Habanero.BO;
using Habanero.Faces.Base;
using <<|=BOPROJECTNAME|>>;

namespace <<|=PROJECTNAME|>>
{
    public class <<|=PARENTCLASS+|>>And<<|=CHILDRELATIONSHIPNAME+|>>Control : ControlManager
    {
        private IReadOnlyGridControl _<<|=#PARENTCLASS+|>>Grid;
        private IReadOnlyGridControl _<<|=#CHILDRELATIONSHIPNAME+|>>Grid;

        public <<|=PARENTCLASS+|>>And<<|=CHILDRELATIONSHIPNAME+|>>Control(IControlFactory controlFactory) : base(controlFactory)
        {
        }

        protected override void InitialiseControl()
        {
            SetupGrids();
            AddGridFilters();
            Load<<|=PARENTCLASS+|>>Collection();
        }

        private void SetupGrids()
        {
            _<<|=#PARENTCLASS+|>>Grid = _controlFactory.CreateReadOnlyGridControl();
            _<<|=#CHILDRELATIONSHIPNAME+|>>Grid = _controlFactory.CreateReadOnlyGridControl();
            ISplitter splitter = _controlFactory.CreateSplitter();

            _<<|=#CHILDRELATIONSHIPNAME+|>>Grid.Dock = DockStyle.Fill;
            _<<|=#PARENTCLASS+|>>Grid.Dock = DockStyle.Top;
            splitter.Dock = DockStyle.Top;

            _<<|=#PARENTCLASS+|>>Grid.Height = 200;

            Control.Controls.Add(_<<|=#CHILDRELATIONSHIPNAME+|>>Grid);
            Control.Controls.Add(splitter); 
            Control.Controls.Add(_<<|=#PARENTCLASS+|>>Grid);

            _<<|=#PARENTCLASS+|>>Grid.Grid.BusinessObjectSelected += delegate { Set<<|=CHILDRELATIONSHIPNAME+|>>Collection(); };
        }

        private void AddGridFilters()
        {
            _<<|=#PARENTCLASS+|>>Grid.FilterControl.Visible = false;
            _<<|=#CHILDRELATIONSHIPNAME+|>>Grid.FilterControl.Visible = false;
            //TODO: Add the filters you want and remove the above lines
        }

        private void Load<<|=PARENTCLASS+|>>Collection()
        {
            BusinessObjectCollection<<<|=PARENTCLASS|>>> <<|=#PARENTCLASS+|>> = new BusinessObjectCollection<<<|=PARENTCLASS|>>>();
            <<|=#PARENTCLASS+|>>.LoadAll();
            _<<|=#PARENTCLASS+|>>Grid.SetBusinessObjectCollection(<<|=#PARENTCLASS+|>>);
        }

        private void Set<<|=CHILDRELATIONSHIPNAME+|>>Collection()
        {
            <<|=PARENTCLASS|>> selected<<|=PARENTCLASS|>> = (<<|=PARENTCLASS|>>)_<<|=#PARENTCLASS+|>>Grid.SelectedBusinessObject;
            if (selected<<|=PARENTCLASS|>> == null) return;
            _<<|=#CHILDRELATIONSHIPNAME+|>>Grid.SetBusinessObjectCollection(selected<<|=PARENTCLASS|>>.<<|=CHILDRELATIONSHIPNAME+|>>);
        }
    }
}