//
// ConfigurationReader.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 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.Generic;
using System.IO;
using Ntp.Analyzer.Localize;
using Ntp.Config;

namespace Ntp.Analyzer.Config
{
    public sealed class ConfigurationReader
    {
        public ConfigurationReader(string file)
        {
            this.file = file;
            errors = new List<String>();
            config = new List<String> {Environment.NewLine};
            configuration = null;
            succeed = true;
            marker = ConfigurationKeyword.Include + " ";
        }

        private readonly List<String> config;
        private readonly List<String> errors;
        private readonly string file;
        private readonly string marker;
        private Configuration configuration;
        private string directory;
        private bool succeed;

        public Configuration Configuration
        {
            get { return configuration; }
        }

        public IEnumerable<String> Errors
        {
            get { return errors; }
        }

        public bool Execute()
        {
            directory = Path.GetDirectoryName(file);

            AddLines(file);

            if (!succeed)
                return false;

            try
            {
                var block = new ConfigurationBlock(config.ToArray());
                configuration = Configuration.Create(block);
            }
            catch (Exception ex)
            {
                errors.Add(String.Format(InitializationMessage.ConfigError, file));
                errors.Add(ex.Message);
                return false;
            }

            if (ConfigurationBase.Errors != String.Empty)
            {
                errors.Add(InitializationMessage.ConfigParseError);
                errors.Add(ConfigurationBase.Errors);
                return false;
            }

            return true;
        }

        private void AddLines(string configFile)
        {
            string[] configLines;

            try
            {
                configLines = File.ReadAllLines(configFile);
            }
            catch (Exception ex)
            {
                errors.Add(String.Format(InitializationMessage.ConfigError, configFile));
                errors.Add(ex.Message);
                succeed = false;
                return;
            }

            foreach (string line in configLines)
            {
                if (line.Trim().StartsWith(marker))
                {
                    string includeFile = line.Trim().Remove(0, marker.Length);
                    config.Add(Environment.NewLine);
                    AddLines(String.Concat(directory, "/", includeFile));
                }
                else
                {
                    config.Add(line);
                }
            }
        }
    }
}