//===============================================================================
// 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 System.IO;
using Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.Configuration.Helpers;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Extensions.CommonHelpers;

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.Configuration
{
    /// <summary>
    /// Given an <see cref="T:EnvDTE.Project" /> object, obtains the <see cref="T:System.Configuration.Configuration" /> object
    /// </summary>
    public class GetWebConfigurationAction : ConfigurableAction
    {
        private Project project;
        private string template;
        private System.Configuration.Configuration configuration;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:GetWebConfigurationAction"/> class.
        /// </summary>
        public GetWebConfigurationAction()
        {
              this.template = string.Empty;
        }
 
        /// <summary>
        /// <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Execute" />
        /// </summary>
        public override void Execute()
        {
            this.configuration = ConfigurationVSHelper.GetConfiguration(this.Project);
            if (this.configuration == null)
            {
                ProjectItem item = this.Project.ProjectItems.AddFromTemplate(this.Template, "Web.config");
                this.configuration = ConfigurationVSHelper.GetConfiguration(this.Project);
            }
        }

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

        /// <summary>
        /// The output <see cref="T:System.Configuration.Configuration" /> object
        /// </summary>
        [Output]
        public System.Configuration.Configuration Configuration
        {
            get { return this.configuration; }
        }

        /// <summary>
        /// The project that owns the configuration file
        /// </summary>
        [Input(Required = true)]
        public Project Project
        {
          get { return this.project; }
          set { this.project = value; }
        }

        /// <summary>
        /// The template to unfold in case that the project does not have a configuration file
        /// </summary>
        [Input(Required = false)]
        public string Template
        {
            get
            {
                if (string.IsNullOrEmpty(this.template))
                {
                    this.template = Path.GetFullPath(
                        Path.Combine(
                        Environment.GetEnvironmentVariable("VS80COMNTOOLS", EnvironmentVariableTarget.Process),
                        "..\\IDE\\ItemTemplatesCache\\Web"));

                    if (DteHelperEx.IsWebCSharpProject(this.project))
                    {
                        this.template = Path.Combine(this.template, @"CSharp\1033\WebConfig.zip\WebConfig.vstemplate");
                    }
                    else
                    {
                        this.template = Path.Combine(this.template, @"VisualBasic\1033\WebConfig.zip\WebConfig.vstemplate");
                    }
                }
                return this.template;
            }
            set { this.template = value; }
        }
    } 
}
