using Sevomin.Models.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
//استخدام نیروی «کنترل پروژه» با ×× سال سابقه ×× دارای ×مدرک× ×× برای کار در ×کشور×شهر×

namespace Sevomin.Models
{
    
    public class JobMiniViewModel
    {
        private int MaxNumberOfMiniParams = 6;
        [Key]
        public long Id { get; set; }

        [Required]
        [StringLength(128)]
        public string AvalinId { get; set; }

        public string AvalinUsername { get; set; }

        [DisplayName("آخرین مهلت اعلام آمادگی")]
        [DataType(DataType.Date)]
        public DateTime ExpireDate { get; set; }

        [DisplayName("آخرین مهلت اعلام آمادگی")]
        [DataType(DataType.Date)]
        public string JalaliExpireDate
        {
            get
            {
                return DateAssist.ToShamsi(this.ExpireDate);
            }
            set
            {
                ExpireDate = DateAssist.ValidateAndSetToMiladi(value) ?? DateTime.MinValue;
            }
        }

        [DisplayName("کار تمام وقت")]
        public bool IsFullTime { get; set; }

        public bool ShowCompanyLogo { get; set; }
        public bool ShowCompanyName { get; set; }

        public string CompanyName { get; set; }
        public string CompanyLogo { get; set; }

        public IList<Tuple<string, string>> Parameters;

        public JobMiniViewModel(Job job, int maxNumberOfMiniParams = 6)
        {
            this.MaxNumberOfMiniParams = maxNumberOfMiniParams;
            this.Id = job.Id;
            this.AvalinId = job.AvalinId;
            this.ExpireDate = job.ExpireDate == DateTime.MinValue ? DateTime.Now.AddDays(14) : job.ExpireDate;
            this.IsFullTime = job.IsFullTime;
            this.AvalinUsername = job.Avalin.UserName;
            this.ShowCompanyLogo = job.ShowCompanyLogo;
            this.ShowCompanyName = job.ShowCompanyName;            
            if (this.ShowCompanyName)
                this.CompanyName = job.Avalin.DisplayName;

            if (job.JobParameters == null)
                return;

            Parameters = new List<Tuple<string,string>>();

            var jpz = job.JobParameters.Where(x=>!string.IsNullOrWhiteSpace(x.StringValue)).OrderByDescending(x=>x.Moscow).Take(Math.Min(MaxNumberOfMiniParams * 3, job.JobParameters.Count()))
                .OrderBy(x => x.Parameter.GroupName).ThenByDescending(x => x.Moscow).Take((int)(MaxNumberOfMiniParams*1.5)).ToList();

            for (int i = 0; i < jpz.Count(); i++)
            {
                string format = jpz[i].Parameter.DisplayFormat;
                format = format.Replace("$", jpz[i].Parameter.Name);

                string value = jpz[i].StringValue;
                if (jpz[i].Parameter.DisplayMethod == 4)
                {
                    value = jpz[i].Parameter.ParameterValues.ToList().Where(x =>x.NumbericValue == jpz[i].NumericValue).Select(x => x.Value)
                        .FirstOrDefault();
                }
                Parameters.Add(new Tuple<string,string>(format, value));
            }
        }
    }
}
