import AggregatedDataPoint from "../../../tradingbot/data/asset/AggregatedDataPoint.js";
import AbstractConverter from "../AbstractConverter.js";
import ConverterFactory from "../ConverterFactory.js";
import OrderBookEntryConverter from "./OrderBookEntryConverter.js";

export default class AggregatedDataPointConverter extends AbstractConverter {

    constructor(){
        super();
    }

    /**
     * @param {AggregatedDataPoint} obj
     * @returns {object} 
     */
    toRestObject(obj) {
        const obeConverter = new OrderBookEntryConverter();
        return {
            _objectType: ConverterFactory.OBJECT_TYPE_AGGREGATED_DATA_POINT,
            startTime: obj.startTime,
            endTime: obj.endTime,
            duration: obj.duration,
            tradeCount: obj.tradeCount,
            tradeVolume: obj.tradeVolume,
            minPrice: obj.minPrice,
            maxPrice: obj.maxPrice,
            avgPrice: obj.avgPrice,
            startPrice: obj.startPrice,
            endPrice: obj.endPrice,
            bestAsk: obeConverter.toRestObject(obj.bestAsk),
            bestBid: obeConverter.toRestObject(obj.bestBid),
            peakStarts: obj.peakStarts,
            peakEnds: obj.peakEnds
        };
    }

    /**
     * @param {object} obj
     * @returns {AggregatedDataPoint} 
     */
    fromRestObject(obj) {
        const obeConverter = new OrderBookEntryConverter();
        const dp = new AggregatedDataPoint(obj.startTime, obj.duration);

        dp.startTime = obj.startTime;
        dp.endTime = obj.endTime;
        dp.duration = obj.duration;
        dp.tradeCount = obj.tradeCount;
        dp.tradeVolume = obj.tradeVolume;
        dp.minPrice = obj.minPrice;
        dp.maxPrice = obj.maxPrice;
        dp.avgPrice = obj.avgPrice;
        dp.startPrice = obj.startPrice;
        dp.endPrice = obj.endPrice;
        dp.bestAsk = obeConverter.fromRestObject(obj.bestAsk);
        dp.bestBid = obeConverter.fromRestObject(obj.bestBid);
        dp.peakStarts = obj.peakStarts;
        dp.peakEnds = obj.peakEnds;

        return dp;
    }
}