//===============================================================================
// Microsoft patterns & practices
// Web Service Software Factory
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.WizardFramework;
using System.ComponentModel.Design;
using System.Collections;
using Howest.Guidance.DataAccess.CodeGeneration.ObjectModel;
using Howest.Guidance.DataAccess.CodeGeneration.ObjectModel.Mappings.Helpers;

namespace Howest.Guidance.DataAccess.CustomWizardPages
{
    public partial class EntitiesFromClassesSelectorCP : CustomWizardPage
    {
        #region Fields

        private ArrayList entities;

        #endregion

        #region Arguments

        /// <summary>
        /// Gets or sets the entities.
        /// </summary>
        /// <value>The entities.</value>
        [RecipeArgument]
        public ArrayList Entities
        {
            get
            {
                return this.entities;
            }
            set
            {
                this.entities = value;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="T:EntitiesFromClassesSelectorCP"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public EntitiesFromClassesSelectorCP(WizardForm parent)
            : base(parent)
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Called when [activated].
        /// </summary>
        public override void OnActivated()
        {
            FillEntitiesListBox();
            SaveArgument();
            base.OnActivated();
        }

        /// <summary>
        /// 	<see cref="P:Microsoft.WizardFramework.WizardPage.IsDataValid"/>
        /// Search for all the arguments that this Wizard gather
        /// If all of them are not null, then the Wizard can finish
        /// </summary>
        /// <value></value>
        public override bool IsDataValid
        {
            get
            {
                return (lstEntities.CheckedItems.Count > 0);
            }
        }

        private void lstEntities_MouseLeave(object sender, EventArgs e)
        {
            SaveArgument();
        }
        #endregion

        #region Private Implementation

        private void FillEntitiesListBox()
        {
            IDictionaryService dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));
            ArrayList entities = (ArrayList)dictservice.GetValue("ProjectEntities");
			List<Entity> list = new List<Entity>();
			foreach (Entity ent in entities)
			{
				list.Add(ent);
			}
            List<Entity> clonedEntities = ClonerHelper.CloneEntities(list);

			lstEntities.DisplayMember = "Entity.Name";// Properties.Resources.EntityNameDisplayMember;
            lstEntities.Items.Clear();

            foreach(Entity entity in clonedEntities)
            {
                lstEntities.Items.Add(entity);
            }

            SelectItems();
        }

        private void SelectItems()
        {
            IDictionaryService dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));
            ArrayList entities = (ArrayList)dictservice.GetValue("Entities");

            if(entities != null)
            {
                for(int index = 0; index < lstEntities.Items.Count; index++)
                {
                    if(FindEntity(entities, ((Entity)lstEntities.Items[index]).Name) != null)
                    {
                        lstEntities.SetItemChecked(index, true);
                    }
                }
            }
            else
            {
                if(lstEntities.Items.Count > 0)
                {
                    lstEntities.SelectedIndex = 0;
                }
            }
        }

        private ArrayList GetEntities()
        {
            ArrayList entities = new ArrayList();

            foreach(object item in lstEntities.CheckedItems)
            {
                entities.Add((Entity)item);
            }

            return entities;
        }

        private void SaveArgument()
        {
            ArrayList entities = GetEntities();
            IDictionaryService dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));
            dictservice.SetValue("Entities", entities);
        }
        #endregion

        #region Private Finders

        private Entity FindEntity(ArrayList entities, string entityName)
        {
            foreach(Entity entity in entities)
            {
                if(entity.Name.Equals(entityName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return entity;
                }
            }

            return null;
        }
        #endregion
    }
}
