

namespace Hawkeye.VisionBuilder.Workflow.Datatypes;

public class LutData
{
    public LutData()
    {
        LUT = new byte[256 * 4];

        Points = new Vector2[4][];
        for (int i = 0; i < 4; i++)
        {
            Points[i] = new Vector2[2];

            Points[i][0] = new Vector2(0, 0);
            Points[i][1] = new Vector2(255, 255);
        }
        


        for (int i = 0; i < 256; i++)
        {
            LUT[i] = (byte)i;
            LUT[i+256] = (byte)i;
            LUT[i+256*2] = (byte)i;
            LUT[i+256*3] = (byte)i;
        }
    }

    public byte[] LUT { get; set; }
    public Vector2[][] Points { get; set; }

    public void Serialize(BinaryWriter bw)
    {
        bw.Write(LUT.Length);
        bw.Write(LUT);
        bw.Write(Points.Length);
        for (int i = 0; i < Points.Length; i++)
        {
            bw.Write(Points[i].Length);
            for (int j = 0; j < Points[i].Length; j++)
            {
                bw.Write(Points[i][j].X);
                bw.Write(Points[i][j].Y);
            }
        }
    }

    public void Deserialize(BinaryReader br)
    {
        var length = br.ReadInt32();
        LUT = br.ReadBytes(length);
        length = br.ReadInt32();
        Points = new Vector2[length][];
        for (int i = 0; i < length; i++)
        {
            var length2 = br.ReadInt32();
            Points[i] = new Vector2[length2];
            for (int j = 0; j < length2; j++)
            {
                Points[i][j] = new Vector2(br.ReadSingle(), br.ReadSingle());
            }
        }
      
    }

}