using System.IO;

namespace fis.Vid.Visuals
{
    public enum VinnoAreaIndicator
    {
        Global = 0,
        A,
        B,
        C,
        ThreeD
    }

    public class VinnoTissue3DArea
    {
        public VinnoRect Bounds { get; }

        public double CmPerPixel { get; }

        public VinnoPoint GlobalOffset { get; }

        public VinnoAreaIndicator Indicator { get; }

        public VinnoTissue3DArea(VinnoRect bounds, double cmPerPixel, VinnoPoint globalOffset, VinnoAreaIndicator indicator)
        {
            Indicator = indicator;
            GlobalOffset = globalOffset;
            CmPerPixel = cmPerPixel;
            Bounds = bounds;
        }

        public byte[] ToBytes()
        {
            byte[] result;
            using (var stream = new MemoryStream())
            {
                var writer = new VinnoStreamWriter(stream);
                writer.WriteByte((byte)Indicator);
                writer.WriteDouble(CmPerPixel);
                writer.WriteDouble(GlobalOffset.X);
                writer.WriteDouble(GlobalOffset.Y);
                writer.WriteDouble(Bounds.Left);
                writer.WriteDouble(Bounds.Top);
                writer.WriteDouble(Bounds.Right);
                writer.WriteDouble(Bounds.Bottom);
                result = stream.ToArray();
            }
            return result;
        }

        public static VinnoTissue3DArea FromBytes(byte[] tissue3DAreaData)
        {
            VinnoTissue3DArea result;
            using (var stream = new MemoryStream(tissue3DAreaData))
            {
                var reader = new VinnoStreamReader(stream);
                var indicator = (VinnoAreaIndicator)reader.ReadByte();
                var cmPerPixel = reader.ReadDouble();
                var x = reader.ReadDouble();
                var y = reader.ReadDouble();
                var globalOffset = new VinnoPoint(x,y);
                var left = reader.ReadDouble();
                var top = reader.ReadDouble();
                var right = reader.ReadDouble();
                var bottom = reader.ReadDouble();
                var bounds = new VinnoRect(new VinnoPoint(left, top), new VinnoPoint(right, bottom));
                result = new VinnoTissue3DArea(bounds,cmPerPixel,globalOffset,indicator);
            }
            return result;
        }
    }
}