//===============================================================================
// 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.
//===============================================================================

#region Using Statements
using System;
using System.Text;
using Microsoft.Practices.ComponentModel;
using EnvDTE;
using System.Diagnostics;
using System.IO;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.VisualStudio;
using EnvDTE80;
using Microsoft.Practices.RecipeFramework.Library;
using System.Collections.Generic;
#endregion

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.VisualStudio
{
    /// <summary>
    /// The action adds the files to the corresponding project item based on its name
    /// </summary>
    [ServiceDependency(typeof(DTE))]
    public class AddItemFromFilesToXsdProjectItemAction : ConfigurableAction
    {
        #region Constants
        private const string XSDExtension = ".xsd";
        #endregion

        #region Input Properties
        /// <summary>
        /// Path of the source files
        /// </summary>
        [Input(Required = true)]
        public List<string> SourceFilesPath
        {
            get { return sourceFilesPath; }
            set { sourceFilesPath = value; }
        }
        private List<string> sourceFilesPath;

        private Project project;
        /// <summary>
        /// Project containing the project items
        /// </summary>
        [Input(Required = true)]
        public Project Project
        {
            get { return project; }
            set { project = value; }
        }

        private bool open = true;
        /// <summary>
        /// A flag to indicate if the newly created item should be shown
        /// in a window.
        /// </summary>
        [Input]
        public bool Open
        {
            get { return open; }
            set { open = value; }
        }
        #endregion Input Properties

        #region ConfigurableAction Implementation
        /// <summary>
        /// The method that creates a new item for each of the files as a subitem of the corresponding project item with the same name
        /// </summary>
        public override void Execute()
        {
            string itemPath;
            ProjectItem projectItem;
            ProjectItem subProjectItem;

            foreach (string file in this.SourceFilesPath)
            {
                itemPath = string.Concat(
                                Path.GetDirectoryName(file),
                                "\\",
                                Path.GetFileNameWithoutExtension(file),
                                XSDExtension);

                itemPath = itemPath.Replace(
                            Path.GetDirectoryName(
                                this.project.DTE.Solution.Properties.Item("Path").Value.ToString()),
                            "");

                projectItem = DteHelper.FindItemByPath(this.project.DTE.Solution, itemPath);

                if(projectItem != null)
                {
                    subProjectItem = projectItem.ProjectItems.AddFromFile(file);

                    if(open)
                    {
                        Window wnd = subProjectItem.Open(Constants.vsViewKindPrimary);
                        wnd.Visible = true;
                        wnd.Activate();
                    }
                }
            }
        }

        /// <summary>
        /// See <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Undo"/>.
        /// </summary>
        public override void Undo()
        {
            //Not Implemented
        }
        #endregion
    }
}
