//
// Configuration.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;
using System.Collections.Generic;
using System.Text;
using Ntp.Analyzer.Log;
using Ntp.Config;
using Ntp.Analyzer.Config.Root;

namespace Ntp.Analyzer.Config
{
    public sealed class Configuration : ConfigurationBase
    {
        public Configuration (
            string configName,
            int indentLevel,
            DatabaseConfiguration database,
            ClusterConfiguration cluster,
            NotifyConfiguration notify,
            ReadingBulkConfiguration bulk,
            IEnumerable<ListenerConfiguration> monitors,
            IEnumerable<LogConfiguration> logs,
            IEnumerable<ServerConfiguration> servers)
            : base(configName, indentLevel)
        {
            this.database = database;
            this.cluster = cluster;
            this.notify = notify;
            this.bulk = bulk;
            this.monitors = new List<ListenerConfiguration> (monitors);
            this.logs = new List<LogConfiguration> (logs);
            this.servers = new List<ServerConfiguration> (servers);
        }

        private readonly ReadingBulkConfiguration bulk;
        private readonly DatabaseConfiguration database;
        private readonly ClusterConfiguration cluster;
        private readonly List<LogConfiguration> logs;
        private readonly List<ListenerConfiguration> monitors;
        private readonly NotifyConfiguration notify;
        private readonly List<ServerConfiguration> servers;

        public DatabaseConfiguration Database {
            get { return database; }
        }

        public ClusterConfiguration Cluster {
            get { return cluster; }
        }

        public NotifyConfiguration Notify {
            get { return notify; }
        }

        public ReadingBulkConfiguration Bulk {
            get { return bulk; }
        }

        public IEnumerable<ListenerConfiguration> Monitors {
            get { return monitors; }
        }

        public IEnumerable<LogConfiguration> Log {
            get { return logs; }
        }

        public IEnumerable<ServerConfiguration> Servers {
            get { return servers; }
        }

        private static readonly String[] Params = new[] {
            DatabaseConfiguration.Identifier,
            ClusterConfiguration.Identifier,
            NotifyConfiguration.Identifier,
            ReadingBulkConfiguration.Identifier,
            LogConfiguration.Identifier,
            ListenerConfiguration.Identifier,
            ServerConfiguration.Identifier
        };

        protected override IEnumerable<Object> Items {
            get {
                return new object[] {
                    database,
                    cluster,
                    notify,
                    bulk,
                    monitors,
                    logs,
                    servers
                };
            }
        }

        public override string ToString ()
        {
            List<Object> values = new List<object> (Items);

            StringBuilder text = new StringBuilder ();

            for (int i = 0; i < Params.Length; i++) {
                if (values [i] == null)
                    continue;

                if (values [i] is IList) {
                    IEnumerable collection = values [i] as IEnumerable;

                    foreach (object item in collection) {
                        text.Append (item);
                    }
                } else if (values [i] is ConfigurationBase) {
                    text.Append (values [i]);
                } else {
                    text.AppendFormat ("{0} {1}", Params [i], values [i]);
                    text.AppendLine ();
                }
            }

            return text.ToString ();
        }

        public static Configuration Create (ConfigurationBlock block)
        {
            ConfigurationBlock databaseBlock = FindSingleBlock (DatabaseConfiguration.Identifier, block);
            DatabaseConfiguration database = DatabaseConfiguration.Create (databaseBlock);

            ConfigurationBlock clusterBlock = FindSingleBlock (ClusterConfiguration.Identifier, block);
            ClusterConfiguration cluster = ClusterConfiguration.Create (clusterBlock);

            ConfigurationBlock notifyBlock = FindSingleBlock (NotifyConfiguration.Identifier, block);
            NotifyConfiguration notify = NotifyConfiguration.Create (notifyBlock);

            ConfigurationBlock bulkBlock = FindSingleBlock (ReadingBulkConfiguration.Identifier, block);
            ReadingBulkConfiguration bulk = ReadingBulkConfiguration.Create (bulkBlock);

            List<LogConfiguration> logs = new List<LogConfiguration> ();
            ConfigurationBlock[] logBlocks = FindBlocks (LogConfiguration.Identifier, block);
            foreach (ConfigurationBlock logBlock in logBlocks) {
                logs.Add (LogConfiguration.Create (logBlock));
            }

            List<ListenerConfiguration> monitors = new List<ListenerConfiguration> ();
            ConfigurationBlock[] monitorBlocks = FindBlocks (ListenerConfiguration.Identifier, block);
            foreach (ConfigurationBlock monitorBlock in monitorBlocks) {
                monitors.Add (ListenerConfiguration.Create (monitorBlock));
            }

            List<ServerConfiguration> servers = new List<ServerConfiguration> ();
            ConfigurationBlock[] serverBlocks = FindBlocks (ServerConfiguration.Identifier, block);
            foreach (ConfigurationBlock serverBlock in serverBlocks) {
                servers.Add (ServerConfiguration.Create (bulk, serverBlock));
            }

            return new Configuration (
                String.Empty,
                block.IndentLevel,
                database,
                cluster,
                notify,
                bulk,
                monitors,
                logs,
                servers);
        }
    }
}