//===============================================================================
// 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.Text;
using Microsoft.Practices.RecipeFramework;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Extensions.CommonHelpers;

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.CodeDom
{
    /// <summary>
    /// This action generates the types from all the schemas included in a project
    /// </summary>
    public class TypeGeneratorFromProjectSchemasAction : ConfigurableAction
    {
        private const string FileExtensionProperty = "Extension";
        private const string SchemaFileExtension = ".xsd";
        private string targetNamespace;
        private Project project;

        /// <summary>
        /// The namespace for the generated classes
        /// </summary>
        [Input]
        public string TargetNamespace
        {
            get { return targetNamespace; }
            set { targetNamespace = value; }
        }

        /// <summary>
        /// The project item containing the xsd schema
        /// </summary>
        [Input]
        public Project Project
        {
            get { return project; }
            set { project = value; }
        }

        /// <summary>
        /// Executes the action
        /// </summary>
        public override void Execute()
        {
            // Searches for all the schema project items
            List<IFileSource> schemaSources = new List<IFileSource>();
            foreach (EnvDTE.ProjectItem item in new DteHelperEx.ProjectItemIterator(project))
            {
                foreach (Property property in item.Properties)
                {
                    if (property.Name.Equals(FileExtensionProperty, StringComparison.InvariantCultureIgnoreCase) &&
                        property.Value.ToString().Equals(SchemaFileExtension, StringComparison.InvariantCultureIgnoreCase))
                    {
                        schemaSources.Add(new ProjectItemFileSource(item));
                    }
                }
            }

            // Generate the type files
            SchemaTypeGenerator typeGenerator = new SchemaTypeGenerator(targetNamespace);
            typeGenerator.GenerateTypes(schemaSources.ToArray());
        }

        /// <summary>
        /// Does nothing
        /// </summary>
        public override void Undo()
        {
        }
    }
}
