package com;

import com.uniubi.Register.FaceBox;
import com.uniubi.Register.UfaceRegister;
import com.uniubi.Register.UfaceRegisterJNI;

import javax.imageio.stream.FileImageOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Objects;

class MyThread extends Thread {
    private final String name;
    private final File[] files;

    public MyThread(String name, File[] files) {
        this.name = name;
        this.files = files;
    }

    public void run() {
        System.out.println("SDK version : " + UfaceRegisterJNI.GetSDKVersion());
        UfaceRegister.SetLogMode(2);
//        UfaceRegister.SetModelPath("");
        int res = UfaceRegister.Auth("dongbang", "NvJZ9pHhVbtX", "verify.lic");
        System.out.println("Auth status : " + res);
        UfaceRegister u = new UfaceRegister();
        int need_handle_num = files.length;
        for (int i = 0; i < need_handle_num; i++) {
            try {
                byte[] image1 = Files.readAllBytes(new File(files[i].toString()).toPath());
                FaceBox box1 = new FaceBox();
//                res = u.JudgeImage(image1, box1);
                float score = u.DynamicQuality(image1, box1);
                System.out.println(name + ",i = " + i + ",all" + need_handle_num + ",fileName = " + files[i] + ",code " + box1.code + " score " + score);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        u.release();
    }
}

public class Main {
    public static void test_GetSimpleImage() {
        System.out.println("SDK version : " + UfaceRegisterJNI.GetSDKVersion());
        UfaceRegister.SetLogMode(2);
        //UfaceRegister.SetModelPath("");
        int res = UfaceRegister.Auth("decheng", "kbSfmCwalEFU", "verify.lic");
        System.out.println("Auth status : " + res);
        UfaceRegister u = new UfaceRegister();
        String path = "";
        File file = new File(path);
        File[] tempList = file.listFiles();

        try {
            for (int i = 0; i < Objects.requireNonNull(tempList).length; i++) {
                System.out.println(tempList[i] + "  " + i);
                byte[] image = Files.readAllBytes(new File(tempList[i].toString()).toPath());
                FaceBox box = new FaceBox();
                res = u.JudgeImage(image, box);
                if (res == 0) {
                    String feature_src = u.GetFeature(image, 110, box);
                    System.out.println("before  box xmin:" + box.xmin + ",ymin:" + box.ymin + ",xmax:" + box.xmax + ",ymax:" + box.ymax);
                    byte[] small_image = u.GetSimpleImage(image, box, 1, 300);
                    System.out.println("after  box xmin:" + box.xmin + ",ymin:" + box.ymin + ",xmax:" + box.xmax + ",ymax:" + box.ymax);
                    if (small_image != null) {
                        FileImageOutputStream os = new FileImageOutputStream(new File("test.jpg"));
                        System.out.println("small_image.length : " + small_image.length);
                        os.write(small_image, 0, small_image.length);
                        os.flush();
                        os.close();
                    }
                    String feature = u.GetFeature(small_image, 110, box);
                    float score = u.CompareFeat(feature_src, feature);
                    System.out.println("score : " + score);
//                    System.out.println("feature : " + feature);
                } else {
                    System.out.println("res == " + res);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        //test_thread
//        test_thread();
        //test_GetSimpleImage
//        test_GetSimpleImage();
        //test_compare_two_pic
        test_compare_two_pic();
    }

    public static void test_compare_two_pic() {
        System.out.println("SDK version : " + UfaceRegisterJNI.GetSDKVersion());
        UfaceRegister.SetLogMode(2);
        UfaceRegister.SetModelPath("/usr/uface_sdk/models");
        int res = UfaceRegister.Auth("decheng","kbSfmCwalEFU", "verify.lic");
        System.out.println("Auth status : " + res);
        UfaceRegister u = new UfaceRegister();

        try {
            byte[] image1 = Files.readAllBytes(new File("/usr/face_img/A003.jpg").toPath());
            FaceBox box1 = new FaceBox();
            int res1 = u.JudgeImage(image1, box1);
            String feature1 = "";
            if (res1 == 0) {
                feature1 = u.GetFeature(image1, 110, box1);
            } else {
                System.out.println("res1 == " + res1);
            }

            byte[] image2 = Files.readAllBytes(new File("/usr/face_img/A004.jpg").toPath());
            FaceBox box2 = new FaceBox();
            int res2 = u.JudgeImage(image2, box2);
            String feature2 = "";
            if (res2 == 0) {
                feature2 = u.GetFeature(image2, 110, box2);
            } else {
                System.out.println("res2 == " + res2);
            }

            float score = u.CompareFeat(feature1, feature2);
            System.out.println("score : " + score);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test_thread() {
        int thread_num = 1;

        String path = "";
        File file = new File(path);
        File[] tempList = file.listFiles();
        assert tempList != null;
        int per_thread_handle_nums = tempList.length / thread_num;
        System.out.println(" per_thread_handle_nums: " + per_thread_handle_nums);
        for (int i = 0; i < thread_num; i++) {
            if (i != thread_num - 1) {
                File[] test = new File[per_thread_handle_nums];
                System.arraycopy(tempList, i * per_thread_handle_nums, test, 0, per_thread_handle_nums);
                MyThread mt = new MyThread("thread" + i, test);
                mt.start();
            } else {
                File[] test = new File[per_thread_handle_nums + tempList.length % thread_num];
                System.arraycopy(tempList, i * per_thread_handle_nums, test, 0, per_thread_handle_nums + tempList.length % thread_num);
                MyThread mt = new MyThread("thread" + i, test);
                mt.start();
            }
        }
    }

}

