using ColorMine.ColorSpaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using Tango.BL.Builders;
using Tango.BL.Serialization;
namespace Tango.BL.Entities
{
public partial class Rml : RmlBase
{
///
/// Initializes a new instance of the class.
///
public Rml() : base()
{
WhitePointL = 92.1815;
WhitePointA = 2.2555;
WhitePointB = -10.9325;
}
protected override void OnWhitePointLChanged(double whitepointl)
{
base.OnWhitePointLChanged(whitepointl);
UpdateColor();
}
protected override void OnWhitePointAChanged(double whitepointa)
{
base.OnWhitePointAChanged(whitepointa);
UpdateColor();
}
protected override void OnWhitePointBChanged(double whitepointb)
{
base.OnWhitePointBChanged(whitepointb);
UpdateColor();
}
private void UpdateColor()
{
RaisePropertyChanged(nameof(Color));
}
public Color Color
{
get
{
Lab lab = new Lab(WhitePointA, WhitePointA, WhitePointB);
Rgb rgb = lab.To();
return Color.FromRgb((byte)rgb.R, (byte)rgb.G, (byte)rgb.B);
}
}
public ProcessParametersTablesGroup GetActiveProcessGroup()
{
return ProcessParametersTablesGroups.FirstOrDefault(x => x.Active);
}
public Task ToRmlFile(ObservablesContext context)
{
return Task.Factory.StartNew(() =>
{
var rml = new RmlBuilder(context).Set(Guid).WithActiveParametersGroup().WithCCT().WithLiquidFactors().Build();
String result = rml.ToJson(new EntitySerializationStrategy()
.Include(() => rml.Cct)
.Include(() => rml.LiquidTypesRmls)
.Include(() => rml.ProcessParametersTablesGroups)
.Include(typeof(ProcessParametersTablesGroup).GetProperty(nameof(ProcessParametersTablesGroup.ProcessParametersTables))),
EntitySerializationFlags.IgnoreCollections | EntitySerializationFlags.IgnoreReferenceTypes | EntitySerializationFlags.Indented);
return result;
});
}
public static Task FromRmlFile(ObservablesContext context, String json)
{
return Task.Factory.StartNew(() =>
{
Rml rml = Rml.FromJson(json,new EntitySerializationStrategy(),EntitySerializationFlags.Indented);
if (context.Rmls.Any(x => x.Guid == rml.Guid || x.Name == rml.Name))
{
throw new InvalidOperationException($"Thread {rml.Name} already exist.");
}
if (rml.Cct != null)
{
if (context.Ccts.Any(x => x.Guid == rml.Cct.Guid))
{
rml.Cct = null;
}
}
rml.LastUpdated = DateTime.UtcNow;
rml.Code = context.Rmls.Max(x => x.Code) + 1;
return rml;
});
}
}
}