package com.sptg.gateway.util;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.util.Base64DecodeMultipartFileUtils;
import com.sptg.common.core.util.aliface.FaceDetectUtil;
import com.sptg.estate.api.entity.ApplyUser;
import com.sptg.estate.api.entity.Member;
import com.sptg.estate.api.entity.UserFeedback;
import com.sptg.permissions.api.entity.ApiDockingConf;
import com.sptg.permissions.api.feigns.ApiDockingConfFeign;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * title: DataCheck
 * package: com.sptg.gateway.util
 * description: 数据校验工具类
 *
 *
 *
 */
public class DataCheck {
    /**
     * 功能描述:检测org_id的权限
     * @param
     * @return 是否返回
     *
     *
     */
   public static boolean checkOrgId(Map<String, Object> hashMap, String org_id, OrgFeign orgFeign) {
//        if(StringUtils.isBlank(org_id)){
//            hashMap.put("result_msg", "org_id不能为空");
//            return true;
//        }else{
//            Organization organization = orgFeign.getOrgById(Long.valueOf(org_id));
//            if(organization==null){
//                hashMap.put("result_msg", "org_id没有对应的机构，无权限");
//                return true;
//            }
//        }
        return false;
    }

    /**
     * 功能描述:检测令牌
     * @param
     * @return 是否返回
     *
     *
     */
    public static boolean checkToken(Map<String, Object> hashMap,String org_id, String token, ApiDockingConfFeign apiDockingConfFeign) {
        if(StringUtils.isBlank(token)){
            hashMap.put("result_msg", "令牌token不能为空");
            return true;
        }else{
            ApiDockingConf apiDockingConf = apiDockingConfFeign.selectEntityById(Long.valueOf(org_id));
            if(apiDockingConf==null){
                hashMap.put("result_msg", "无对接权限，请联系管理员");
                return true;
            }
            if(!token.equals(apiDockingConf.getToken())){
                hashMap.put("result_msg", "无效令牌");
                return true;
            }
            if(apiDockingConf.getStatus()==0){
                hashMap.put("result_msg", "接口权限已关闭，请联系管理员");
                return true;
            }
        }
        return false;
    }

    //上传人脸
    public static ResultBean uploadFace(String face_image_base64, ApplyUser applyUser, FastFileStorageClient fastFileStorageClient, ConfFeign confFeign) throws IOException {
        if(StringUtils.isNotBlank(face_image_base64)){
            MultipartFile multipartFile  = Base64DecodeMultipartFileUtils.base64ToMultipart(Base64DecodeMultipartFileUtils.imageFormatToPng(face_image_base64));
            //图片上传
            StorePath storePath = fastFileStorageClient.uploadFile(
                    multipartFile.getInputStream(),multipartFile.getSize(),
                    FilenameUtils.getExtension(multipartFile.getOriginalFilename()),null);

            String faceUrl = confFeign.getFileServerHost() + storePath.getFullPath();
            FaceDetectUtil faceDetectUtil = new FaceDetectUtil();
            ResultBean resultBean = faceDetectUtil.detectImage(faceUrl);
            if (resultBean != null){
                return resultBean;
            }
            applyUser.setImageUri(storePath.getFullPath());
            //图片的md5值
            String md5Hex = DigestUtils.md5Hex(multipartFile.getInputStream());
            applyUser.setFaceCode(md5Hex);

        }
        return null;
    }

    //上传人脸返回url和faceCode
    public static Map<String, String> uploadFace(String face_image_base64, FastFileStorageClient fastFileStorageClient) throws IOException {
        if(StringUtils.isNotBlank(face_image_base64)){
            MultipartFile multipartFile  = Base64DecodeMultipartFileUtils.base64ToMultipart(Base64DecodeMultipartFileUtils.imageFormatToPng(face_image_base64));
            //图片上传
            StorePath storePath = fastFileStorageClient.uploadFile(
                    multipartFile.getInputStream(),multipartFile.getSize(),
                    FilenameUtils.getExtension(multipartFile.getOriginalFilename()),null);
            //applyUser.setImageUri(storePath.getFullPath());
            //图片的md5值
            String md5Hex = DigestUtils.md5Hex(multipartFile.getInputStream());
            //applyUser.setFaceCode(md5Hex);
            Map<String, String> map = new HashMap<>();
            map.put("imageUrl", storePath.getFullPath());
            map.put("faceCode", md5Hex);
            return map;
        }
        return null;
    }

    //上传头像
    public static void uploadHeadImage(String face_image_base64, Member member, FastFileStorageClient fastFileStorageClient) throws IOException {
        if(StringUtils.isNotBlank(face_image_base64)){
            MultipartFile multipartFile  = Base64DecodeMultipartFileUtils.base64ToMultipart(Base64DecodeMultipartFileUtils.imageFormatToPng(face_image_base64));
            //图片上传
            StorePath storePath = fastFileStorageClient.uploadFile(
                    multipartFile.getInputStream(),multipartFile.getSize(),
                    FilenameUtils.getExtension(multipartFile.getOriginalFilename()),null);
            member.setHeadImage(storePath.getFullPath());
        }
    }

    //上传反馈图片
    public static void uploadImage(String face_image_base64, UserFeedback userFeedback, FastFileStorageClient fastFileStorageClient) throws IOException {
        if(StringUtils.isNotBlank(face_image_base64)){
            MultipartFile multipartFile  = Base64DecodeMultipartFileUtils.base64ToMultipart(Base64DecodeMultipartFileUtils.imageFormatToPng(face_image_base64));
            //图片上传
            StorePath storePath = fastFileStorageClient.uploadFile(
                    multipartFile.getInputStream(),multipartFile.getSize(),
                    FilenameUtils.getExtension(multipartFile.getOriginalFilename()),null);
            String imageUrl = userFeedback.getImageUrl();
            if(imageUrl == null){
                userFeedback.setImageUrl(storePath.getFullPath());
            }else{
                String newImageUrl = imageUrl + "," + storePath.getFullPath();
                userFeedback.setImageUrl(newImageUrl);
            }
        }
    }
}
