package com.bytesflow.opencv.media.test;

import com.bytesflow.opencv.media.jni.PQCVMediaProcessor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class OpenCVImageBlurTest {
    private static String sIMAGE_PATH = "/Users/devyk/Downloads/IMG_3067.PNG";
    private static String sSO_PATH = "/Users/devyk/Data/Project/sample/github_code/OpenCVSample/output/lib/libpiaoquan_java_opencv.dylib";

    private static String [] sVIS = {"{\"videoPath\":\"http://rescdn.yishihui.com/longvideo/transcode/video/qa/20220314/6267824rwLv7IntDRwKxLG7y6.m3u8\",\"clips\":[{\"startTimeMs\":119900,\"endTimeMs\":120900},{\"startTimeMs\":239800,\"endTimeMs\":240800},{\"startTimeMs\":359700,\"endTimeMs\":360700},{\"startTimeMs\":479600,\"endTimeMs\":480600},{\"startTimeMs\":599500,\"endTimeMs\":600500},{\"startTimeMs\":719400,\"endTimeMs\":720400},{\"startTimeMs\":839300,\"endTimeMs\":840300},{\"startTimeMs\":959200,\"endTimeMs\":960200}]}",
    "{\"videoPath\":\"http://rescdn.yishihui.com/longvideo/transcode/video/qa/20220314/6267824Z0EoT5Pr61RFNcKI07.mp4\",\"clips\":[{\"startTimeMs\":2000,\"endTimeMs\":3000},{\"startTimeMs\":4000,\"endTimeMs\":5000},{\"startTimeMs\":6000,\"endTimeMs\":7000},{\"startTimeMs\":8000,\"endTimeMs\":9000},{\"startTimeMs\":10000,\"endTimeMs\":11000},{\"startTimeMs\":12000,\"endTimeMs\":13000},{\"startTimeMs\":14000,\"endTimeMs\":15000},{\"startTimeMs\":16000,\"endTimeMs\":17000}]}"};
    private static volatile  int vis_index = 0;
    public static void main(String[] args) {
        int count = 3;
        if (args.length > 0) {
            sIMAGE_PATH = args[0];
            sSO_PATH = args[1];
            count = Integer.parseInt(args[2]);
        }
        try {
            PQCVMediaProcessor.initSDK(sSO_PATH);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        }
        ExecutorService executorService = Executors.newFixedThreadPool(count);
        for (int j = 0; j < count; j++)  {
            long startTime = System.currentTimeMillis();
            CountDownLatch countDownLatch = new CountDownLatch(count);
            vis_index = 0;
            for (int i = 0; i < count; i++) {
                executorService.execute(new VisRunable(countDownLatch));
            }
            try {
                countDownLatch.await();
                System.out.println("const time = " + (System.currentTimeMillis() - startTime));
//                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static  class VisRunable implements  Runnable {
        private CountDownLatch countDownLatch;

        public VisRunable(CountDownLatch countDownLatch) {
            this.countDownLatch = countDownLatch;
        }
        @Override
        public void run() {
            System.out.println("index=" + vis_index + " " + PQCVMediaProcessor.getVideoSimilarityLists(sVIS[vis_index]));
            vis_index++;
            countDownLatch.countDown();
        }
    }

    private static class ImageBlurRunable implements Runnable {

        private CountDownLatch countDownLatch;

        public ImageBlurRunable(CountDownLatch countDownLatch) {
            this.countDownLatch = countDownLatch;
        }

        @Override
        public void run() {
            blurTest();
            countDownLatch.countDown();
        }
    }

    private static void blurTest() {
        try {
            String path = sIMAGE_PATH;
            int ret;
            ret = PQCVMediaProcessor.blurDetectionFromImagePath(path);
            System.out.println("PQCVMediaProcessor path = " + ret);

            File file = new File(path);
            byte[] images = new byte[(int) file.length()];
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read(images);
            ret = PQCVMediaProcessor.blurDetectionFromImageBytes(images);
            fileInputStream.close();
            System.out.println("PQCVMediaProcessor bytes = " + ret);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
