//
// HostDataMapper.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 System.Diagnostics;
using System.IO;
using System.Linq;
using Ntp.Analyzer.Log;
using Ntp.Data;
using Ntp.Analyzer.Gui.Model;

namespace Ntp.Analyzer.Gui.Data
{
    public sealed class HostDataMapper : DataMapper<Host>
    {
        public HostDataMapper (string file, LogBase log)
            : base(log)
        {
            this.file = file;
            initialized = false;
            items = new Dictionary<Int32, Host> ();
        }

        private readonly string file;
        private bool initialized;
        private Dictionary<Int32, Host> items;

        public override IEnumerator<Host> GetEnumerator ()
        {
            if (!initialized) {
                Read ();
                initialized = true;
            }

            return items.Values.GetEnumerator ();
        }

        public void Add (Host item)
        {
            items.Add (item.Id, item);
        }

        public void Remove (Host item)
        {
            items.Remove (item.Id);
        }

        private void Read ()
        {
            StreamReader reader = null;

            try {
                reader = new StreamReader (file);

                while (reader.Peek() != -1) {
                    string line = reader.ReadLine ();

                    if (line != null && line.Trim () == String.Empty)
                        continue;

                    Host item = Parse (line);

                    if (item != null)
                        items.Add (item.Id, item);
                }
            } catch (Exception ex) {
                Debug.WriteLine (ex.Message);
            } finally {
                if (reader != null)
                    reader.Close ();
            }
        }

        private Host Parse (string line)
        {
            if (line.Trim ().StartsWith ("#"))
                return null;

            string name = line.Substring (0, 20).Trim ();
            string address = line.Substring (20, 20).Trim ();
            string portText = line.Substring (40, 5).Trim ();

            int port = 0;
            Int32.TryParse (portText, out port);

            return new Host (name, address, port);
        }

        public void WriteToDisk ()
        {
            StreamWriter writer = null;

            try {
                writer = new StreamWriter (file);

                foreach (Host host in items.Values) {
                    writer.Write (host.Name.PadRight (20, ' '));
                    writer.Write (host.Address.PadRight (20, ' '));
                    writer.Write (host.Port.ToString ().PadRight (5, ' '));
                    writer.WriteLine ();
                }
            } finally {
                writer.Close ();
            }
        }
    }
}
