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

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

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


        [Index]
        [Index("IX_DovominParameterUniqueIndex", Order=2, IsUnique=true)]
        [Required]
        [StringLength(128)]
        public string DovominId { get; set; }

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

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

        public DovominParameter()
        {}

        public DovominParameter(Dovomin dovomin, Parameter parameter)
        {
            this.Dovomin = dovomin;
            this.Parameter = parameter;
            this.StringValue = "";
            this.NumericValue = null;
        }

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

    }
}
