using GreenTree.Nachtragsmanagement.Services.Configuration;
using GreenTree.Nachtragsmanagement.Web.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GreenTree.Nachtragsmanagement.Web.Models.Config
{
    public class ConfigItemDataModel
    {
        public static Dictionary<string, string> FullTypeTranslations = new Dictionary<string, string>
        {
            { typeof(System.String).FullName, "Text" },
            { typeof(System.Int32).FullName, "Ganze Zahl" },
            { typeof(System.Int64).FullName, "Große ganze Zahl" },
            { typeof(System.Boolean).FullName, "Wahrheitswert" },
            { typeof(System.Double).FullName, "Kommazahl" },
            { typeof(System.DateTime).FullName, "Datum" },
            { typeof(System.Drawing.Color).FullName, "Farbe" },
        };

        public int Id { get; set; }
        public string Name { get; set; }
        public string TypeFullName { get; set; }
        public string TypeDescription { get; set; }
        public string Value { get; set; }
        public bool IsValueCollection { get; set; }
        public string[] Values { get; set; }
        public string Description { get; set; }

        public static ConfigItemDataModel FromConfigItem(Core.Domain.Config.ConfigItem configItemEntity, bool newWhenIsNull)
        {
            if (configItemEntity == null && newWhenIsNull)
                return new ConfigItemDataModel
                {
                    Id = -1
                };

            if (configItemEntity == null && !newWhenIsNull)
                throw new ArgumentNullException("configItemEntity", "Cannot create ConfigItemDataModel from NULL configItem entity.");

            var configItemModel = new ConfigItemDataModel
            {
                Id = configItemEntity.Id,
                Name = configItemEntity.Name,
                TypeFullName = configItemEntity.TypeFullName,
                Description = configItemEntity.Description
            };

            if (FullTypeTranslations.ContainsKey(configItemEntity.TypeFullName))
            {
                configItemModel.Value = configItemEntity.Value;
                configItemModel.TypeDescription = FullTypeTranslations[configItemEntity.TypeFullName];
                configItemModel.IsValueCollection = false;
            }
            else
            {
                var configurationReference = ConfigurationHelper.GetConfigurationReferenceByName(configItemEntity.TypeFullName);

                if (configurationReference != null)
                {
                    configItemModel.TypeDescription = configurationReference.DisplayName;
                    configItemModel.IsValueCollection = configurationReference.IsMultipleSelection;
                    configItemModel.Value = configItemEntity.Value;

                    if (configurationReference.IsMultipleSelection)
                        configItemModel.Values = configurationReference.TransformValueToValueCollection(configItemEntity.Value);
                }
                else
                {
                    configItemModel.TypeDescription = "Unbekannt";
                }
            }

            return configItemModel;
        }

        public Core.Domain.Config.ConfigItem ToConfigItem()
        {
            var configItem = new Core.Domain.Config.ConfigItem
            {
                Id = this.Id,
                Name = this.Name,
                TypeFullName = this.TypeFullName,
                Description = this.Description
            };

            if (FullTypeTranslations.ContainsKey(this.TypeFullName))
            {
                configItem.Value = this.Value;
            }
            else
            {
                var configurationReference = ConfigurationHelper.GetConfigurationReferenceByName(this.TypeFullName);

                if (configurationReference != null)
                {
                    if (configurationReference.IsMultipleSelection)
                        configItem.Value = configurationReference.TransformValueCollectionToValue(this.Values);
                    else
                        configItem.Value = this.Value;
                }
            }

            return configItem;
        }
    }
}