//===============================================================================
// 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 Microsoft.Practices.WizardFramework.Configuration;
using Microsoft.Practices.Common;
using Microsoft.Practices.RecipeFramework.Library;
using EnvDTE;

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.CustomPages
{
    public partial class ResponsibilitiesPage : CustomWizardPage
    {
        private List<string> webResponsibilities;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ResponsibilitiesPage"/> class.
        /// </summary>
        public ResponsibilitiesPage()
            : base()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ResponsibilitiesPage"/> class.
        /// </summary>
        /// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
        public ResponsibilitiesPage(WizardForm wizard)
            : base(wizard)
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets the project.
        /// </summary>
        /// <value>The project.</value>
        public Project Project
        {
            get
            {
                IDictionaryService dict = (IDictionaryService)GetService(typeof(IDictionaryService));
                return dict.GetValue("Project") as Project;
            }
        }

		private List<string> WebResponsibilities
		{
			get
			{
				if (webResponsibilities == null)
				{
					IDictionaryService dict = (IDictionaryService)GetService(typeof(IDictionaryService));
					string webResponsibilitiesArgument = dict.GetValue("WebResponsibilities") as string;
					if (webResponsibilitiesArgument != null)
						webResponsibilities = new List<string>(webResponsibilitiesArgument.Split(','));
					else
						webResponsibilities = new List<string>();
				}

				return webResponsibilities;
			}
		}

		

        /// <summary>
        /// Ends initialization of the component
        /// </summary>
        public override void EndInit()
        {
            base.EndInit();

            if (Configuration != null)
            {
                IDictionaryService dict = (IDictionaryService)GetService(typeof(IDictionaryService));

                foreach (Field field in Configuration.Fields)
                {
                    string valueName = field.ValueName;
                    CheckBox checkbox = new CheckBox();
                    checkbox.AutoSize = true;
                    checkbox.Text = field.Label;
                    if (dict.GetValue(valueName) is bool)
                        checkbox.Checked = (bool)dict.GetValue(valueName);
                    else
                        dict.SetValue(valueName, false);
                    if (Project != null && DteHelper.IsWebProject(Project) && !WebResponsibilities.Contains(valueName))
                        checkbox.Enabled = false;
                    checkbox.CheckedChanged += delegate
                    {
                        dict.SetValue(valueName, checkbox.Checked);
                    };

                    flowPanel.Controls.Add(checkbox);
                }
            }
        }
    }
}
