package com.gyee.wisdom.common.data.timeseries;

import lombok.Data;

import java.util.Date;

@Data
public class BaseTsQuery implements TsQuery {

    private final TsPoint tsPoint;
    private final long startTs;
    private final long endTs;
    private final int interval;
    private final int limit ;
    private final Interpolation interpolation;


    public BaseTsQuery(TsPoint tsPoint, long startTs, long endTs, int interval, int limit, Interpolation interpolation) {
        this.tsPoint = tsPoint;
        this.startTs = startTs;
        this.endTs = endTs;
        this.interval = interval;
        this.limit = limit;
        this.interpolation = interpolation;
    }

    public BaseTsQuery(TsPoint tsPoint, long startTs, long endTs) {
        this(tsPoint, startTs, endTs, 1, 1, Interpolation.RAW);
    }
    @Override
    public Date[] getDateArray() {
        if (interpolation == Interpolation.SNAP && startTs > 0 && endTs >0 && interval > 0 && endTs > startTs ) {
            int count = Math.round ((endTs - startTs)/interval/1000);
            int length = count;// <= limit ? count : limit;
            Date[] result = new Date[length];
            for (int i=0;i<length;i++) {
                long ts = startTs + (long)i*interval*1000;
                result[i] = new Date(ts);
            }

            return result;
        }
        return  null;
    }

}
