//
// DatabaseConfiguration.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using Ntp.Config;

namespace Ntp.Analyzer.Config.Root
{
    public sealed class DatabaseConfiguration : ConfigurationBase
    {
        public const string Identifier = "Database";
        private static readonly String[] Params = new[] {
            Identifier,
            "Host",
            "Name",
            "User",
            "Pass",
            "Create"
        };
        private readonly string host;
        private readonly string name;
        private readonly string pass;
        private readonly string user;
        private readonly bool initialize;

        private DatabaseConfiguration (
            string configName,
            int indentLevel,
            string host,
            string name,
            string user,
            string pass,
            bool initialize
        )
            : base(configName, indentLevel)
        {
            this.host = host;
            this.name = name;
            this.user = user;
            this.pass = pass;
            this.initialize = initialize;
        }

        protected override IEnumerable<Object> Items {
            get {
                return new object[] {
                    ConfigName,
                    host,
                    name,
                    user,
                    pass,
                    initialize
                };
            }
        }

        public string Name { 
            get { 
                return name;
            }
        }

        public bool Initialize { 
            get { 
                return initialize;
            }
        }

        /// <summary>
        /// Gets the database connection string.
        /// </summary>
        /// <value>The database connection string.</value>
        public string ConnectionString {
            get {
                return String.Format (
                    "server={0};database={1};userid={2};password={3}",
                    host, name, user, pass);
            }
        }

        /// <summary>
        /// Gets the database initialization connection string.
        /// </summary>
        /// <value>The database initialization connection string.</value>
        public string InitializeConnectionString {
            get {
                return String.Format (
                    "server={0};userid={1};password={2}",
                    host, user, pass);
            }
        }

        public override string ToString ()
        {
            return ConfigText (Params, Items);
        }

        public static DatabaseConfiguration Create (ConfigurationBlock config)
        {
            if (config == null)
                return null;

            Dictionary<String, String> options = GetOptions (Params, config);

            return new DatabaseConfiguration (
                GetOptionConfigName (options, Params [0], null, false, Params [0]),
                config.IndentLevel,
                GetOptionString (options, Params [1], Params [0], true),
                GetOptionString (options, Params [2], Params [0], true),
                GetOptionString (options, Params [3], Params [0], true),
                GetOptionString (options, Params [4], Params [0], true),
                GetOptionBoolean (options, Params [5], Params [0], false, false));
        }
    }
}