export class Sampler {

    constructor(id, data, classes, depositRatio, maxSamples, voffsets) {
        this.data = data;
        this.samples = [];
        this.voffsets = [];
        this.classes = classes;
        this.id = id;
        this.depositRatio = depositRatio;
        this.svgclass = 'sampler-' + this.id;
        this.currentSampleId = undefined;
        this.currentYield = undefined;
        this.maxSamples = maxSamples;
    }

    // actual sampling implemented as importance sampling in app.js
    drawSample(d) {

        let y = (1 - this.depositRatio) * d.values[0] + this.depositRatio * d.values[1];

        // compare yield to thresholds defined in classes
        for (let j = 0; j < this.classes.length; ++j) {
            // if (y >= this.classes[j].min && y < this.classes[j].max) {
            if (y > this.classes[j].min && y <= this.classes[j].max) {
                this.currentSampleId = this.classes[j].id;
                this.currentYield = y;
                this.samples.push(this.currentSampleId);
                return;
            }
        }
    }

    updateOffsets(iconBlockSize) {

        for (let cidx = 0; cidx < this.classes.length; ++cidx) {
            this.voffsets[cidx] = 1;  // calculate number of lines occupied by class
        }
        return this.voffsets;
    }

    get calculatedYield() {
        let factor = Math.pow(10, 0);
        let val = Math.round(this.currentYield * 100 * factor) / factor;
        let sign = '';
        if (val > 0) {
            sign = '+';
        } else if (sign < 0) {
            sign = '-';
        }

        let result = (sign + val + '%').replace('.', ',');
        return result;
    }

    getStockRatioRepr() {
        let val = Math.round((1 - this.depositRatio) * 100000) / 1000;
        return val + '%';
    }

    get stockRatioRepr() {
        let val = Math.round((1 - this.depositRatio) * 100000) / 1000;
        return val + '%';
    }

    getDepositRatioRepr() {
        let val = Math.round(this.depositRatio * 100000) / 1000;
        return val + '%';
    }

    get depositRatioRepr() {
        let val = Math.round(this.depositRatio * 100000) / 1000;
        return val + '%';
    }

    setPeriod(val) {
        this.period = val;
    }

    isSafeToSample(amount) {
        for (s in this.samplesPerClass()) {
            s.length < this;
        }
    }

    samplesPerClass() {
        let samplesIsolated = [];
        let samples = [];
        for (let s of this.samples) {  // deep copy of samples
            samples.push(s);
        }

        for (let cls of this.classes) {
            let arr = samples.filter(function(c) {
                return c === cls.id;
            }, cls);

            samplesIsolated.push(arr);
        }
        // console.log('samplesIsolated', this.id, samplesIsolated);
        return samplesIsolated;
    }

    reset() {
        this.samples = [];
        this.currentSampleId = undefined;
        this.currentYield = undefined;
    }
}
