package org.springblade.modules.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.modules.app.config.OcrConfig;
import org.springblade.modules.app.dto.BusinessLicenseDTO;
import org.springblade.modules.app.dto.DriverDTO;
import org.springblade.modules.app.dto.DrivingDTO;
import org.springblade.modules.app.dto.IdentityCardDTO;
import org.springblade.modules.app.service.IOcrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.*;
import com.aliyun.teaopenapi.models.*;


@Service
@Slf4j
public class OcrService implements IOcrService {

    @Autowired
    private OcrConfig ocrConfig;

    private Client client;

    @Override
    public IdentityCardDTO recognizeIdcard(String url) {
        RecognizeIdcardRequest recognizeAdvancedRequest = new RecognizeIdcardRequest()
                .setUrl(url);
        try {
            JSONObject dataObject = JSONObject.parseObject(getClient().recognizeIdcard(recognizeAdvancedRequest).getBody().data);
            JSONObject resData = dataObject.getJSONObject("data").getJSONObject("face").getJSONObject("data");
            IdentityCardDTO identityCardDTO = new IdentityCardDTO();
            identityCardDTO.setAddress(resData.getString("address"));
            identityCardDTO.setName(resData.getString("name"));
            identityCardDTO.setIDNum(resData.getString("idNumber"));
            return identityCardDTO;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public BusinessLicenseDTO RecognizeBusinessLicense(String url) {
        RecognizeBusinessLicenseRequest recognizeAdvancedRequest = new RecognizeBusinessLicenseRequest()
                .setUrl(url);
        try {
            JSONObject dataObject = JSONObject.parseObject(getClient().recognizeBusinessLicense(recognizeAdvancedRequest).getBody().data);
            JSONObject resData = dataObject.getJSONObject("data").getJSONObject("face").getJSONObject("data");
            BusinessLicenseDTO businessLicenseDTO = new BusinessLicenseDTO();
            businessLicenseDTO.setEnterpriseName(resData.getString("companyName"));
            businessLicenseDTO.setEnterpriseType(resData.getString("companyType"));
            businessLicenseDTO.setAddress(resData.getString("businessAddress"));
            businessLicenseDTO.setLegalRepresentative(resData.getString("legalPerson"));
            businessLicenseDTO.setCapital(resData.getString("registeredCapital"));
            businessLicenseDTO.setEstablishDate(resData.getDate("RegistrationDate"));
            businessLicenseDTO.setValidPeriod(resData.getDate("validPeriod"));
            businessLicenseDTO.setBusiness(resData.getString("businessScope"));
            businessLicenseDTO.setRegNum(resData.getString("creditCode"));
            return businessLicenseDTO;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public DriverDTO RecognizeDrivingLicense(String url) {
        RecognizeDrivingLicenseRequest recognizeAdvancedRequest = new RecognizeDrivingLicenseRequest()
                .setUrl(url);
        try {
            JSONObject dataObject = JSONObject.parseObject(getClient().recognizeDrivingLicense(recognizeAdvancedRequest).getBody().data);
            JSONObject resData = dataObject.getJSONObject("data").getJSONObject("face").getJSONObject("data");
            DriverDTO driverDTO = new DriverDTO();
            driverDTO.setNumber(resData.getString("licenseNumber"));
            driverDTO.setVehicleType(resData.getString("approvedType"));
            driverDTO.setIssueOrganization(resData.getString("issueAuthority"));
            driverDTO.setStartTime(resData.getDate("initialIssueDate"));
            driverDTO.setEndTime(resData.getDate("validPeriod"));
            return driverDTO;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public DrivingDTO RecognizeVehicleLicense(String url) {
        RecognizeVehicleLicenseRequest recognizeAdvancedRequest = new RecognizeVehicleLicenseRequest()
                .setUrl(url);
        try {
            JSONObject dataObject = JSONObject.parseObject(getClient().recognizeVehicleLicense(recognizeAdvancedRequest).getBody().data);
            JSONObject resData = dataObject.getJSONObject("data").getJSONObject("face").getJSONObject("data");
            DrivingDTO driverDTO = new DrivingDTO();
            driverDTO.setVIN(resData.getString("vinCode"));
            return driverDTO;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Object RecognizeGeneral(String url) {
        RecognizeGeneralRequest recognizeAdvancedRequest = new RecognizeGeneralRequest()
                .setUrl(url);
        try {
            JSONObject dataObject = JSONObject.parseObject(getClient().recognizeGeneral(recognizeAdvancedRequest).getBody().data);
            JSONObject resData = dataObject.getJSONObject("data").getJSONObject("data");
            return resData;
        } catch (Exception e) {
            return null;
        }
    }

    private Client getClient() {
        if (this.client == null) {
            try {
                Config config = new Config()
                        .setAccessKeyId(ocrConfig.getKey())
                        .setEndpoint(ocrConfig.getEndpoint())
                        .setAccessKeySecret(ocrConfig.getSecret());
                this.client = new Client(config);
                return client;
            } catch (Exception e) {
                log.error("初始化ocr配置失败");
                throw new ServiceException("初始化ocr配置失败");
            }
        } else {
            return this.client;
        }

    }

}
