package com.dcsoft.file.service;

import com.dcsoft.file.utils.HttpUtil;
import com.dcsoft.file.utils.ImageToBase64Utils;
import com.uniubi.Register.FaceBox;
import com.uniubi.Register.UfaceRegister;
import com.uniubi.Register.UfaceRegisterJNI;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户管理 服务层处理
 *
 * @author xueyi
 */
@Service
public class FaceServiceImpl implements IFaceService {

    //@Value("${uni.modelPath}")
    public  String modelPath;

    //@Value("${uni.userName}")
    public  String userName;

    //@Value("${uni.passWord}")
    public  String passWord;

    //@Value("${uni.license}")
    public  String license;

    /**
     * 项目启动时，初始化参数到缓存
     */
    @PostConstruct
    public void init()
    {
        /*System.out.println("SDK version : " + UfaceRegisterJNI.GetSDKVersion());
        UfaceRegister.SetLogMode(2);
        UfaceRegister.SetModelPath(modelPath);
        UfaceRegister.Auth(userName,passWord, license);*/
    }

    //检测图片
    public  int judgeImageUrl(String url){
        UfaceRegister u = new UfaceRegister();
        System.out.println("UfaceRegister : " + u );
        byte[] image1 = Base64.decodeBase64(ImageToBase64Utils.getImgUrlToBase64(url));
        FaceBox box1 = new FaceBox();
        return u.JudgeImage(image1, box1);
    }

    //检测图片
    public int judgeImageBase64(String base64){
        UfaceRegister u = new UfaceRegister();
        byte[] image1 = Base64.decodeBase64(base64);
        FaceBox box1 = new FaceBox();
        return u.JudgeImage(image1, box1);
    }

   //提取特征值
    public String getFeature(String base64){
        UfaceRegister u = new UfaceRegister();
        byte[] image1 = Base64.decodeBase64(base64);
        FaceBox box1 = new FaceBox();
        return u.GetFeature(image1, 110,box1);
    }

    //图片比对
    public float compareTwoPic(byte[] image1,byte[] image2){
        float score=0;
        UfaceRegister u = new UfaceRegister();
        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);
        }

        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);
        }
        score = u.CompareFeat(feature1, feature2);
        System.out.println("score : " + score);
        return score;
    }


    //特征值与图片比对
    public float compareTwoPic(String feature1,String feature2){
        float score=0;
        UfaceRegister u = new UfaceRegister();
        score = u.CompareFeat(feature1, feature2);
        System.out.println("score : " + score);
        return score;
    }

/*
    //图片压缩
    public String getSimpleImage(String admitGuids){
        String url=uniUrl+"/algo/wrapper/getSimpleImage";
        Map<String, Object> headMap=new HashMap<String, Object>();
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("admitGuids",admitGuids);
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }
*/
}
