using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GreenTree.Nachtragsmanagement.Web.Models.Site
{
    public class SiteTreeDataModel
    {
        public int Id { get; set; }
        public string TreeKey { get; set; }
        public string TreeParentKey { get; set; }
        public string CustomNumber { get; set; }
        public string Description { get; set; }
        public DateTime? ReceiptDate { get; set; }
        public decimal? Value { get; set; }
        public string KindDescription { get; set; }
        public int? StatusId { get; set; }
        public string StatusDescription { get; set; }
        public string StatusColor { get; set; }
        public ICollection<int> DistCatValues { get; set; }
        public ICollection<string> DistCatDescriptions { get; set; }
        public string DistCatDescription
        {
            get
            {
                if (DistCatDescriptions == null)
                    return String.Empty;
                else
                    return String.Join(", ", DistCatDescriptions);
            }
        }
        public string Comment { get; set; }

        public SiteTreeDataModel()
        {
            DistCatValues = new List<int>();
            DistCatDescriptions = new List<string>();
        }

        public static List<SiteTreeDataModel> TreeFromSite(Core.Domain.Site.Site siteEntity)
        {
            var result = new List<SiteTreeDataModel>();

            if (siteEntity == null)
                return result;

            var openDeviations = siteEntity.Deviations
                .Where(d => d.Appendix == null);

            var emptyAppendix = new SiteTreeDataModel
            {
                Id = 0,
                TreeKey = "a_0",
                TreeParentKey = String.Empty,
                CustomNumber = "Offene VA",
                Description = openDeviations.Count() > 0
                    ? String.Format("{0} offene VAs", openDeviations.Count())
                    : "Keine offenen VAs"
            };

            result.Add(emptyAppendix);

            foreach (var openDeviation in openDeviations)
            {
                result.Add(new SiteTreeDataModel
                {
                    Id = openDeviation.Id,
                    TreeKey = String.Format("d_{0}", openDeviation.Id),
                    TreeParentKey = emptyAppendix.TreeKey,
                    CustomNumber = openDeviation.CustomNumber,
                    Description = openDeviation.Description,
                    ReceiptDate = openDeviation.ReceiptDate,
                    DistCatValues = 
                        openDeviation.DisturbanceValues
                            .Select(d => d.Id)
                            .ToList(),
                    DistCatDescriptions =
                        openDeviation.DisturbanceValues
                            .Select(r => r.Disturbance == null
                                ? String.Empty
                                : r.Disturbance.Description)
                            .ToList(),
                    KindDescription = openDeviation.Kind == null
                        ? String.Empty
                        : openDeviation.Kind.Description,
                    StatusId = openDeviation.StatusId,
                    StatusDescription = openDeviation.Status == null
                        ? String.Empty
                        : openDeviation.Status.Description,
                    Value = openDeviation.Value.Value * openDeviation.Percentage.Value,
                    Comment = openDeviation.Comment
                });
            }

            var orderedAppendices = siteEntity
                .Appendices
                .OrderBy(a =>
                    a.CustomNumber.Any(c => Char.IsLetter(c))
                        ? 0
                        : Convert.ToInt32(a.CustomNumber));
                

            foreach (var appendix in orderedAppendices)
            {
                var parentAppendix = new SiteTreeDataModel
                {
                    Id = appendix.Id,
                    TreeKey = String.Format("a_{0}", appendix.Id),
                    TreeParentKey = String.Empty,
                    CustomNumber = appendix.CustomNumber,
                    Description = appendix.Description,
                    ReceiptDate = appendix.OfferingDate,
                    DistCatValues =
                        appendix.CategoryValues
                            .Select(d => d.Id)
                            .ToList(),
                    DistCatDescriptions =
                        appendix.CategoryValues
                            .Select(r => r.Category == null
                                ? String.Empty
                                : r.Category.Description)
                            .ToList(),
                    StatusId = appendix.StateId,
                    StatusDescription = appendix.State == null
                        ? String.Empty
                        : appendix.State.Description,
                    StatusColor = appendix.State == null
                        ? "#FFFFFF"
                        : appendix.State.HexColor,
                    Value = appendix.NegotiationValue,
                    Comment = appendix.Comment
                };

                result.Add(parentAppendix);

                foreach (var deviation in appendix.Deviations)
                {
                    var childDeviation = new SiteTreeDataModel
                    {
                        Id = deviation.Id,
                        TreeKey = String.Format("d_{0}", deviation.Id),
                        TreeParentKey = parentAppendix.TreeKey,
                        CustomNumber = deviation.CustomNumber,
                        Description = deviation.Description,
                        ReceiptDate = deviation.ReceiptDate,
                        DistCatValues =
                            deviation.DisturbanceValues
                                .Select(d => d.Id)
                                .ToList(),
                        DistCatDescriptions =
                            deviation.DisturbanceValues
                                .Select(r => r.Disturbance == null
                                    ? String.Empty
                                    : r.Disturbance.Description)
                                .ToList(),
                        KindDescription = deviation.Kind == null
                            ? String.Empty
                            : deviation.Kind.Description,
                        StatusId = deviation.StatusId,
                        StatusDescription = deviation.Status == null
                            ? String.Empty
                            : deviation.Status.Description,
                        Value = deviation.Value.Value * deviation.Percentage.Value,
                        Comment = deviation.Comment
                    };

                    result.Add(childDeviation);
                }
            }

            return result;
        }
    }
}