using GreenTree.Strohrmann.ERP.Core.Domain.Business;
using GreenTree.Strohrmann.ERP.Web.Extension;
using GreenTree.Strohrmann.ERP.Web.Models.Search;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.EntityFrameworkCore.Internal;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace GreenTree.Strohrmann.ERP.Web.Models.Business
{
    public class CraftModel : ICustomerSearchable
    {
        #region Properties

        /// <summary>
        /// Craft id
        /// </summary>
        [Display(Name = "ID")]
        public int Id { get; set; }

        /// <summary>
        /// Craft name
        /// </summary>
        [Display(Name = "Name")]
        public string Name { get; set; }

        /// <summary>
        /// Craft creation date
        /// </summary>
        [Display(Name = "Gestartet am")]
        [DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:dd.MM.yyyy}")]
        public DateTime CreationDate { get; set; }

        /// <summary>
        /// Craft customer
        /// </summary>
        [Display(Name = "Kunde")]
        public CustomerModel Customer { get; set; }

        /// <summary>
        /// Craft customer id
        /// </summary>
        [Display(Name = "Kunde")]
        public int CustomerId { get; set; }

        /// <summary>
        /// Craft customer text
        /// </summary>
        [Display(Name = "Kundenname")]
        public string CustomerText { get; set; }

        /// <summary>
        /// Craft comment
        /// </summary>
        [Display(Name = "Kommentar")]
        public string Comment { get; set; }

        /// <summary>
        /// Craft employees
        /// </summary>
        [Display(Name = "Stunden")]
        public Dictionary<string, CraftEmployeeModel> CraftEmployees { get; set; }

        /// <summary>
        /// Craft employee value sum
        /// </summary>
        [Display(Name = "Wert (Stunden)")]
        [DisplayFormat(DataFormatString = "{0:c2}")]
        public float CraftEmployeeValue 
        { 
            get
            {
                return CraftEmployees.Sum(ce => ce.Value.Value);
            }
        }

        /// <summary>
        /// Craft materials
        /// </summary>
        [Display(Name = "Material")]
        public Dictionary<string, CraftMaterialModel> CraftMaterials { get; set; }

        /// <summary>
        /// Craft material value sum
        /// </summary>
        [Display(Name = "Wert (Material)")]
        [DisplayFormat(DataFormatString = "{0:c2}")]
        public float CraftMaterialValue
        {
            get
            {
                return CraftMaterials.Sum(ce => ce.Value.Value);
            }
        }

        #endregion

        #region Ctor

        /// <summary>
        /// Initializes a new instance of the CraftModel class
        /// </summary>
        public CraftModel() { }

        /// <summary>
        /// Initializes a new instance of the CraftModel class
        /// </summary>
        /// <param name="craft">The base craft.</param>
        public CraftModel(Craft craft)
        {
            if (craft == null) return;

            Id = craft.Id;
            Name = craft.Name;
            CreationDate = craft.CreationDate;
            Customer = new CustomerModel(craft.Customer);
            CustomerText = Customer.Fullname;
            CustomerId = craft.Customer.Id;
            Comment = craft.Comment;
            CraftEmployees = craft.CraftEmployees
                .Select(ce => new CraftEmployeeModel(ce))
                .ToDictionary(ce => HtmlContentExtension.RandomId(), ce => ce);
            CraftMaterials = craft.CraftMaterials
                .Select(cm => new CraftMaterialModel(cm))
                .ToDictionary(cm => HtmlContentExtension.RandomId(), cm => cm);

            var index = 0;

            foreach (var item in CraftEmployees)
            {
                item.Value.Index = index;
                index++;
            }

            index = 0;

            foreach (var item in CraftMaterials)
            {
                item.Value.Index = index;
                index++;
            }
        }

        #endregion
    }
}
