//
// Configuration.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013-2016 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.Linq;
using System.Text;
using Ntp.Analyzer.Config.Root;
using Ntp.Analyzer.Localize;
using Ntp.Common;
using Ntp.Config;
using Ntp.Data;
using Ntp.Log;

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

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

        private readonly List<ReadingBulkConfiguration> bulks;
        private readonly ClusterConfiguration cluster;
        private readonly DatabaseConfiguration database;
        private readonly HeartbeatConfiguration heartbeat;
        private readonly List<LogConfiguration> logs;
        private readonly List<ListenerConfiguration> monitors;
        private readonly NotifyConfiguration notify;
        private readonly PermissionConfiguration permission;
        private readonly List<HostConfiguration> servers;

        public DatabaseConfiguration Database
        {
            get { return database; }
        }

        public HeartbeatConfiguration Heartbeat
        {
            get { return heartbeat; }
        }

        public PermissionConfiguration Permission
        {
            get { return permission; }
        }

        public ClusterConfiguration Cluster
        {
            get { return cluster; }
        }

        public NotifyConfiguration Notify
        {
            get { return notify; }
        }

        public IEnumerable<ReadingBulkConfiguration> Bulks
        {
            get { return bulks; }
        }

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

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

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

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

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

            var text = new StringBuilder();

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

                if (values[i] is IList)
                {
                    var 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);
            if (database == null)
                ConfigError(ConfigurationMessage.DatabaseMissing);

            ConfigurationBlock heartbeatBlock = FindSingleBlock(HeartbeatConfiguration.Identifier, block);
            HeartbeatConfiguration heartbeat = HeartbeatConfiguration.Create(heartbeatBlock);

            ConfigurationBlock permissionBlock = FindSingleBlock(PermissionConfiguration.Identifier, block);
            PermissionConfiguration permission = PermissionConfiguration.Create(permissionBlock);

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

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

            ConfigurationBlock[] bulkBlocks = FindBlocks(ReadingBulkConfiguration.Identifier, block);
            List<ReadingBulkConfiguration> bulks = bulkBlocks.Select(ReadingBulkConfiguration.Create).ToList();

            ConfigurationBlock[] logBlocks = FindBlocks(LogConfiguration.Identifier, block);
            List<LogConfiguration> logs = logBlocks.Select(LogConfiguration.Create).ToList();

            ConfigurationBlock[] monitorBlocks = FindBlocks(ListenerConfiguration.Identifier, block);
            List<ListenerConfiguration> monitors = monitorBlocks.Select(ListenerConfiguration.Create).ToList();

            ConfigurationBlock[] serverBlocks = FindBlocks(HostConfiguration.Identifier, block);
            List<HostConfiguration> servers =
                serverBlocks.Select(serverBlock => HostConfiguration.Create(bulks.ToArray(), serverBlock)).ToList();

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