//===============================================================================
// 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.Configuration;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using System.IO;
using Microsoft.Win32;
using Microsoft.Practices.ComponentModel;
using System.ComponentModel.Design;
using Microsoft.Practices.Common.Services;
using System.Web.Configuration;
using EnvDTE;
using Config = System.Configuration.Configuration;
using Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.Configuration.Helpers;
using Microsoft.Practices.RecipeFramework.Library;

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.Configuration
{
    /// <summary>
    /// Action that gets a configuration object
    /// </summary>
    [ServiceDependency(typeof(ITypeResolutionService))]
    public class GetConfigurationAction : ConfigurableAction
    {
        #region Input Properties

        Project project;
        /// <summary>
        /// Gets or sets the project.
        /// </summary>
        /// <value>The project.</value>
        [Input(Required = true)]
        public Project Project
        {
            get { return project; }
            set { project = value; }
        }

        #endregion

        #region Output Properties

        Config configuration;
        /// <summary>
        /// Gets or sets the configuration.
        /// </summary>
        /// <value>The configuration.</value>
        [Output]
        public Config Configuration
        {
            get { return configuration; }
            set { configuration = value; }
        }

        /// <summary>
        /// Gets the configuration file path.
        /// </summary>
        /// <value>The configuration file path.</value>
        [Output]
        public string ConfigurationFilePath
        {
            get { return configuration.FilePath; }
        }

        #endregion

        #region Action Implementation

        private bool configAdded;

        /// <summary>
        /// See <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Execute"/>.
        /// </summary>
        public override void Execute()
        {
            this.Configuration = ConfigurationVSHelper.GetConfiguration(project);
            
            if (this.Configuration == null)
            {
                //Add a new configuration
                this.Configuration = ConfigurationVSHelper.AddNewConfiguration(project, base.Container);
                configAdded = true;
            }
        }

        /// <summary>
        /// See <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Undo"/>.
        /// </summary>
        public override void Undo()
        {
            if (configAdded)
            {
                ProjectItem item = ConfigurationVSHelper.GetConfigurationItem(this.project);
                if (item != null)
                {
                    item.Delete();
                }
            }
            this.Configuration = null;
        }

        #endregion
    }
}
