using Autofac;
using GreenTree.Nachtragsmanagement.Core;
using GreenTree.Nachtragsmanagement.Services.Appendix;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GreenTree.Nachtragsmanagement.Services.Configuration
{
    public class StateConfigurationReference : IConfigurationReference
    {
        /// <summary>
        /// Id
        /// </summary>
        public Guid Id
        {
            get
            {
                return Guid.Parse("DF5E3C83-357C-458D-AA03-9BF7C5C49203");
            }
        }

        /// <summary>
        /// Internal name
        /// </summary>
        public string Name
        {
            get
            {
                return "ConfigurationReference.StateConfigurationReference";
            }
        }

        /// <summary>
        /// Display name
        /// </summary>
        public string DisplayName
        {
            get
            {
                return "Nachtragsstatus";
            }
        }

        /// <summary>
        /// Determines if mutiple values are selectable
        /// </summary>
        public bool IsMultipleSelection
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Get available values
        /// </summary>
        public ConfigurationReferenceElement[] GetAvailableValues()
        {
            var appendixService = Singleton<IContainer>.Instance.Resolve<IAppendixService>();

            return 
                appendixService
                    .GetAllStates()
                    .Select(s => new ConfigurationReferenceElement
                    {
                        Value = s.Id,
                        Text = s.Description
                    })
                    .ToArray();
        }

        /// <summary>
        /// Transforms mutiple selected values into a single line value
        /// </summary>
        /// <param name="values">Selected values.</param>
        public string TransformValueCollectionToValue(string[] values)
        {
            return
                String.Join(", ", values);
        }

        /// <summary>
        /// Transforms the single line value into a mutiple selected values
        /// </summary>
        /// <param name="value">Single line value.</param>
        public string[] TransformValueToValueCollection(string value)
        {
            return
                String.IsNullOrEmpty(value)
                    ? new string[0]
                    : value.Split(',')
                        .Select(s => s.Trim())
                        .ToArray();
        }

        /// <summary>
        /// Converts the string converted value into strongly typed value
        /// </summary>
        /// <param name="values">The selected value.</param>
        public T GetValue<T>(string value)
        {
            return (T)((object)Convert.ToInt32(value));
        }

        /// <summary>
        /// Converts the string converted values into strongly typed values
        /// </summary>
        /// <param name="values">The selected values.</param>
        public T GetValues<T>(string[] values)
        {
            if (values == null)
                return default(T);

            return 
                (T)(
                    (object)
                        (values
                            .Select(v => Convert.ToInt32(v))
                            .ToArray()));
        }
    }
}
