//
// AboutPageConfiguration.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.Generic;
using Ntp.Analyzer.Config.Destination;
using Ntp.Analyzer.Config.Navigation;
using Ntp.Analyzer.Localize;
using Ntp.Config;

namespace Ntp.Analyzer.Config.Page
{
    public sealed class AboutPageConfiguration : PageConfigurationBase, ILinkable
    {
        private AboutPageConfiguration(
            HostConfiguration server,
            string configName,
            int indentLevel,
            int frequency,
            bool initialRun,
            bool fixedRun,
            string theme,
            string title,
            int serverId,
            string header,
            string content,
            DestinationCollection destinations,
            string link)
            : base(
                server,
                configName,
                indentLevel,
                frequency,
                initialRun,
                fixedRun,
                theme,
                title,
                destinations,
                link)
        {
            this.header = header;
            this.content = content;
            this.serverId = serverId;
        }

        public const string Identifier = ConfigurationKeyword.AboutPage;

        private new const bool DefaultInitialRun = true;
        private new const bool DefaultFixedRun = false;
        private const int DefaultFrequency = 0;

        private static readonly String[] Params =
        {
            ConfigurationKeyword.AboutPage,
            ConfigurationKeyword.Frequency,
            ConfigurationKeyword.InitialRun,
            ConfigurationKeyword.FixedRun,
            ConfigurationKeyword.PageTemplate,
            ConfigurationKeyword.PageTitle,
            ConfigurationKeyword.AboutPageServerId,
            ConfigurationKeyword.AboutPageContentTitle,
            ConfigurationKeyword.AboutPageContent,
            FileCollection.Identifier,
            ConfigurationKeyword.PageLink
        };

        private readonly string content;
        private readonly string header;
        private readonly int serverId;

        public string Header
        {
            get { return header; }
        }

        public string Content
        {
            get { return content; }
        }

        public int ServerId
        {
            get { return serverId; }
        }

        protected override IEnumerable<Object> Items
        {
            get
            {
                var items = new List<object> {ConfigName};

                if (ShowDefault || (!ShowDefault && Frequency != DefaultFrequency))
                    items.Add(Frequency);

                if (ShowDefault || (!ShowDefault && InitialRun != DefaultInitialRun))
                    items.Add(InitialRun);

                if (ShowDefault || (!ShowDefault && FixedRun != DefaultFixedRun))
                    items.Add(FixedRun);

                if (ShowDefault || (!ShowDefault && Theme.ToLower() != DefaultTheme.ToLower()))
                    items.Add(Theme);

                items.Add(Title);

                if (ShowDefault || (!ShowDefault && ServerId != 0))
                    items.Add(ServerId);

                items.Add(Header);
                items.Add("{ " + Content + " }");
                items.Add(Destinations);

                if (ShowDefault || (!ShowDefault && Link != DefaultLink))
                    items.Add(Link);

                return items.ToArray();
            }
        }

        public override string ToString()
        {
            var list = new List<string> {ConfigurationKeyword.AboutPage};

            if (ShowDefault || (!ShowDefault && Frequency != DefaultFrequency))
                list.Add(ConfigurationKeyword.Frequency);

            if (ShowDefault || (!ShowDefault && InitialRun != DefaultInitialRun))
                list.Add(ConfigurationKeyword.InitialRun);

            if (ShowDefault || (!ShowDefault && FixedRun != DefaultFixedRun))
                list.Add(ConfigurationKeyword.FixedRun);

            if (ShowDefault || (!ShowDefault && Theme.ToLower() != DefaultTheme.ToLower()))
                list.Add(ConfigurationKeyword.PageTemplate);

            list.Add(ConfigurationKeyword.PageTitle);

            if (ShowDefault || (!ShowDefault && ServerId != 0))
                list.Add(ConfigurationKeyword.AboutPageServerId);

            list.Add(ConfigurationKeyword.AboutPageContentTitle);
            list.Add(ConfigurationKeyword.AboutPageContent);
            list.Add(FileCollection.Identifier);

            if (ShowDefault || (!ShowDefault && Link != DefaultLink))
                list.Add(ConfigurationKeyword.PageLink);

            return ConfigText(list.ToArray(), Items);
        }

        public static AboutPageConfiguration Create(
            HostConfiguration server,
            ConfigurationBlock config)
        {
            Dictionary<String, String> options = GetOptions(Params, config);

            ConfigurationBlock contentBlock = FindSingleBlock(ConfigurationKeyword.AboutPageContent, config);
            if (contentBlock == null)
            {
                ConfigError(ConfigurationMessage.AboutPageContentMissing);
                return null;
            }

            string content =
                String.Join(Environment.NewLine, contentBlock.Lines, 1, contentBlock.Lines.Length - 2)
                    .Trim().Replace("\t", String.Empty);

            return new AboutPageConfiguration(
                server,
                GetOptionConfigName(options, ConfigurationKeyword.AboutPage, Identifier, true),
                config.IndentLevel,
                GetOptionInteger(options, ConfigurationKeyword.Frequency, Identifier, false, DefaultFrequency),
                GetOptionBoolean(options, ConfigurationKeyword.InitialRun, Identifier, false, DefaultInitialRun),
                GetOptionBoolean(options, ConfigurationKeyword.FixedRun, Identifier, false, DefaultFixedRun),
                GetOptionString(options, ConfigurationKeyword.PageTemplate, Identifier, false, DefaultTheme),
                GetOptionString(options, ConfigurationKeyword.PageTitle, Identifier, true),
                GetOptionInteger(options, ConfigurationKeyword.AboutPageServerId, Identifier, false, 0),
                GetOptionString(options, ConfigurationKeyword.AboutPageContentTitle, Identifier, false, "ContentTitle"),
                content,
                FileCollection.Create(server, config),
                GetOptionString(options, ConfigurationKeyword.PageLink, Identifier, false, null)); // TODO: Autolink is not generated
        }
    }
}