//
// DataFace.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 Ntp.Analyzer.Log;
using Ntp.Analyzer.Data.Cache;
using Ntp.Analyzer.Data.Static;

namespace Ntp.Analyzer.Data
{
    /// <summary>
    /// Singleton facade class used to access memory persistent data.
    /// </summary>
    public sealed class DataFace
    {
        private static readonly DataFace instance = new DataFace ();
        private readonly Object locker = new Object ();
        private readonly LogBase log;
        private NtpConfCache ntpConfigCache;
        private NtpqCache ntpqCache;
        private HostDatabaseMapper hostMapper;
        private PeerDatabaseMapper peerMapper;
        private TimeServerDatabaseMapper serverMapper;

        /// <summary>
        /// Initializes a new instance of the <see cref="Ntp.Analyzer.Data.DataFace" /> class.
        /// </summary>
        private DataFace ()
        {
            log = DataConnection.Log;
        }

        /// <summary>
        /// Gets the Singleton instance.
        /// </summary>
        /// <value>The instance.</value>
        public static DataFace Instance {
            get { return instance; }
        }

        /// <summary>
        /// Gets the ntp config cache.
        /// </summary>
        /// <value>The ntp config cache.</value>
        public NtpConfCache NtpConfigCache {
            get {
                lock (locker) {
                    if (ntpConfigCache == null) {
                        ntpConfigCache = new NtpConfCache (log);
                    }
                }

                return ntpConfigCache;
            }
        }

        /// <summary>
        /// Gets the ntpq cache.
        /// </summary>
        /// <value>The ntpq cache.</value>
        public NtpqCache NtpqCache {
            get {
                lock (locker) {
                    if (ntpqCache == null) {
                        ntpqCache = new NtpqCache (log);
                    }
                }

                return ntpqCache;
            }
        }

        /// <summary>
        /// Gets the host mapper.
        /// </summary>
        /// <value>The hosts.</value>
        public HostDatabaseMapper Hosts {
            get {
                lock (locker) {
                    if (hostMapper == null) {
                        hostMapper = new HostDatabaseMapper (log);
                    }
                }

                return hostMapper;
            }
        }

        /// <summary>
        /// Gets the peer mapper.
        /// </summary>
        /// <value>The peers.</value>
        public PeerDatabaseMapper Peers {
            get { 
                lock (locker) {
                    if (peerMapper == null) {
                        peerMapper = new PeerDatabaseMapper (Servers, log);
                    }
                }

                return peerMapper;
            }
        }

        /// <summary>
        /// Gets the time server mapper.
        /// </summary>
        /// <value>The servers.</value>
        public TimeServerDatabaseMapper Servers {
            get {
                lock (locker) {
                    if (serverMapper == null) {
                        serverMapper = new TimeServerDatabaseMapper (log);
                    }
                }

                return serverMapper;
            }
        }

        /// <summary>
        /// Gets the host reading mapper.
        /// </summary>
        /// <value>The host readings.</value>
        public HostReadingDatabaseMapper HostReadings {
            get { return new HostReadingDatabaseMapper (Hosts, Peers, log); }
        }

        /// <summary>
        /// Gets the host IO reading mapper.
        /// </summary>
        /// <value>The host IO reading mapper.</value>
        public HostIOReadingDatabaseMapper HostIOReadings {
            get { return new HostIOReadingDatabaseMapper (Hosts, log); }
        }

        /// <summary>
        /// Gets the peer reading mapper.
        /// </summary>
        /// <value>The peer readings.</value>
        public PeerReadingDatabaseMapper PeerReadings {
            get { return new PeerReadingDatabaseMapper (Hosts, Peers, log); }
        }
    }
}