using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using o0.IMU;
using o0.Geometry;
using o0;

namespace SmartBowSDK
{
    public class AttitudeJson : CustomJson
    {
        public AttitudeJson() : base()
        {
            InitJsonClass<_9AxisPreProcessor>(() => new _9AxisPreProcessor(),
                "AccByteIndex",
                "GyrByteIndex",
                "MagByteIndex",
                "ByteToAcc",
                "ByteToGyr",
                "ByteToMag",
                "GyrCalibrater",
                "MagCalibrater",
                "ByteReverseMS"
            );
            InitJsonClass<MagnetometerAutoCalibrater>(() => new MagnetometerAutoCalibrater(),
                "CountPerLength",
                "EllipsoidFitting",
                "FitCountLeft",
                "FitThreshold",
                "LastTimestamp",
                "Lock",
                "MaxCount",
                "NewBlock",
                "NewBlockAccumulation",
                "Variance",
                "VectorByBlock"
            );
            InitJsonClass<ByteToShorts>(() => new ByteToShorts(3),
                "ByteToShort"
            );
            InitJsonClass<ByteToShort>(() => new ByteToShort(null),
                "MinCount",
                "Reverse",
                "Short"
            );
            InitJsonClass<MeanMaintainer<Vector<double>>>(() => new MeanMaintainer<Vector<double>>(),
                "Count",
                "Mean"
            );
            InitJsonClass<ByteToShortByVariance>(() => new ByteToShortByVariance(),
                "Count",
                "Reverse",
                "Short",
                "ThresholdRate"
            );
            InitJsonClass<EllipsoidFitting>(() => new EllipsoidFitting(default, null, default),
                "Center",
                "CorrectMatrixArray",
                "Radius"
            );
            InitJsonClass<Dictionary<Vector<int>, Vector<double>>>(
                o => JToken.FromObject(o),
                jt =>
                {
                    var o = new Dictionary<Vector<int>, Vector<double>>();
                    foreach (JProperty item in jt)
                        o[ParseByExporter<Vector<int>>(JArray.Parse(item.Name))]
                            = ParseByExporter<Vector<double>>(item.Value);
                    return o;
                }
            );
            InitJsonClass<Vector<int>>(
                o => JToken.FromObject(o),
                jt => new Vector<int>(jt.ToObject<int[]>())
            );
            InitJsonClass<Vector<double>>(
                o => JToken.FromObject(o),
                jt => new Vector<double>(jt.ToObject<double[]>())
            );
        }
    }
}
