/*
MIT License

Copyright (c) <2025> <Paul Uszak>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to 
the following conditions:

The above copyright notice and this permission notice shall be included 
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.reallyreallyrandom.isitrandom;

import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;

import org.apache.commons.compress.compressors.lzma.LZMACompressorOutputStream;

import com.opencsv.CSVWriter;

public class Calibration {

    String FILENAME = "xxxxxxxxxx.csv";
    int NO_TRIALS = 2500;
    Double[] sizesArray = {
            2.50000000e+04, 3.06051936e+04, 3.74671149e+04, 4.58675321e+04,
            5.61513880e+04, 6.87409639e+04, 8.41532202e+04, 1.03021024e+05,
            1.26119135e+05, 1.54396021e+05, 1.89012805e+05, 2.31390939e+05,
            2.83270579e+05, 3.46782035e+05, 4.24533253e+05, 5.19716895e+05,
            6.36241446e+05, 7.78891704e+05, 9.53525254e+05, 1.16731300e+06,
            1.42903361e+06, 1.74943401e+06, 2.14167066e+06, 2.62184980e+06,
            3.20968883e+06, 3.92932591e+06, 4.81031120e+06, 5.88882022e+06,
            7.20913930e+06, 8.82548415e+06, 1.08042260e+07, 1.32266171e+07,
            1.61921271e+07, 1.98225274e+07, 2.42668915e+07, 2.97077164e+07,
            3.63684164e+07, 4.45224970e+07, 5.45047855e+07, 6.67251804e+07,
            8.16854824e+07, 1.00000000e+08
    };
    List<Double> sizesList = Arrays.asList(sizesArray);
    SecureRandom secureRandom = new SecureRandom();
    RandomGenerator rng = RandomGeneratorFactory.of("L64X256MixRandom").create(getCryptoSeed()); // 320 bits of state.;

    private void calibrate() throws IOException {
        var outputfile = new FileWriter(FILENAME);
        var csvWriter = new CSVWriter(outputfile);
        String[] header = { "Size", "testStatistic" };
        csvWriter.writeNext(header);
        var then = System.currentTimeMillis();

        for (var size : sizesList) {
            for (var trial = 1; trial <= NO_TRIALS; trial++) {
                var samples = getSamples(size.intValue());

                //
                //
                // =========================================
                var testStatistic = compress(samples);
                // =========================================
                //
                //

                System.out.printf("%d trial(s), %s   %d %n", trial, samples.length, testStatistic);
                String[] data = { String.valueOf(samples.length), String.valueOf(testStatistic) };
                csvWriter.writeNext(data, false);
            }

        }
        System.out.println("Took " + (System.currentTimeMillis() - then) / 1000 + " seconds.");
        csvWriter.close();
        outputfile.close();
    }

    private byte[] getCryptoSeed() {
        byte[] seed = new byte[40]; // = 320 bits.
        secureRandom.nextBytes(seed);
        return seed;
    }

    private byte[] getSamples(int size) {
        var samples = new byte[size];
        rng.nextBytes(samples);
        return samples;
    }

    private static int compress(byte[] samples) throws IOException {
        int testStatistic;
        var bufferSize = (int) (samples.length * 1.3); // Allow 30% headroom.
        var bos = new ByteArrayOutputStream(bufferSize);
        try (var lzmaOut = new LZMACompressorOutputStream(bos)) {
            lzmaOut.write(samples);
            lzmaOut.flush();
            lzmaOut.close();

            testStatistic = bos.toByteArray().length;
        }
        return testStatistic;
    }

    public static void main(String[] args) throws IOException {
        new Calibration().calibrate();
    }
}