using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Sevomin.Models
{
    public class JobParameter
    {
        [Key]
        public long Id { get; set; }

        [Index("IX_JobParameterUniqueIndex", Order=1, IsUnique=true)]
        [Required]
        public long ParameterId { get; set; }


        [Index]
        [Index("IX_JobParameterUniqueIndex", Order=2, IsUnique=true)]
        [Required]
        public long JobId { get; set; }

        public string StringValue { get; private set; }
        public decimal? NumericValue { get; private set; }

        public byte Moscow { get; set; }

        public virtual Parameter Parameter { get; set; }
        public virtual Job Job { get; set; }

        public JobParameter()
        {}

        public JobParameter(Job job, Parameter parameter)
        {
            this.Job = job;
            this.Parameter = parameter;
            this.StringValue = "";
            this.NumericValue = null;
            this.Moscow = 0;
        }

        public void SetValue(string value)
        {
            decimal val;
            if (decimal.TryParse(value, out val))
            {
                NumericValue = val;
                StringValue = value;
            }
            else
            {
                NumericValue = null;
                StringValue = value;
            }
        }


    }
}
