using GreenTree.Strohrmann.ERP.Core.Domain.Rights;
using GreenTree.Strohrmann.ERP.Web.Models.Shared;
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 CustomerModel : TrackingModel
    {
        #region Properties

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

        /// <summary>
        /// Customer title
        /// </summary>
        [Display(Name = "Anrede")]
        public TitleModel Title { get; set; }

        /// <summary>
        /// Customer forename
        /// </summary>
        [Display(Name = "Vorname")]
        public string Firstname { get; set; }

        /// <summary>
        /// Customer lastname
        /// </summary>
        [Display(Name = "Nachname")]
        public string Lastname { get; set; }

        /// <summary>
        /// Customer full name
        /// </summary>
        [Display(Name = "Voller Name")]
        public string Fullname 
        { 
            get
            {
                return String.Format("{0} {1}", Firstname, Lastname);
            }
        }

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

        /// <summary>
        /// Customer business state
        /// </summary>
        [Display(Name = "Geschäftskunde")]
        public bool IsBusiness { get; set; }

        /// <summary>
        /// Customer address
        /// </summary>
        [Display(Name = "Adresse")]
        public string Address { get; set; }

        /// <summary>
        /// Customer town
        /// </summary>
        [Display(Name = "Stadt")]
        public string Town { get; set; }

        /// <summary>
        /// Customer zip code
        /// </summary>
        [Display(Name = "Postleitzahl")]
        public string ZipCode { get; set; }

        /// <summary>
        /// Local full name
        /// </summary>
        [Display(Name = "Lokale Adresse")]
        public string LocalAddress
        {
            get
            {
                return String.Format("{0}, {1} {2}", Address, ZipCode, Town);
            }
        }

        /// <summary>
        /// Customer country
        /// </summary>
        [Display(Name = "Land")]
        public string Country { get; set; }

        /// <summary>
        /// Customer tax
        /// </summary>
        [Display(Name = "Steuersatz")]
        public TaxModel Tax { get; set; }

        /// <summary>
        /// Customer crafts
        /// </summary>
        [Display(Name = "Gewerke")]
        public string[] Crafts { get; set; }

        #endregion

        #region Ctor

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

        /// <summary>
        /// Initializes a new instance of the CustomerModel class
        /// </summary>
        /// <param name="customer">Base customer entity.</param>
        public CustomerModel(Core.Domain.Business.Customer customer)
            : base(customer)
        {
            if (customer == null) return;

            Id = customer.Id;
            Title = new TitleModel(customer.Title);
            Firstname = customer.Firstname;
            Lastname = customer.Lastname;
            CompanyName = customer.CompanyName;
            IsBusiness = customer.IsBusiness;
            Address = customer.Address;
            Town = customer.Town;
            ZipCode = customer.ZipCode;
            Country = customer.Country;
            Tax = new TaxModel(customer.Tax);
            Crafts = customer.Crafts
                .Select(c => c.Name)
                .ToArray();
        }

        #endregion
    }
}
