package com.mvc.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.mvc.recognition.freeai.FaSampleResult;
import com.mvc.recognition.noconfig.NoConfigOutModel;
import com.mvc.service.aiiden.UnitType;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;

public class TExtendIdentModel {
    public TExtendIdentModel() {
    }

    public TExtendIdentModel(NoConfigOutModel outModel, TAireadModel aireadModel) {
        this.aireadId = aireadModel.getId();
        this.deviceSn = aireadModel.getDeviceSn();
        this.stationCode = aireadModel.getStationCode();
        this.identTime = new Date();
        this.identValue = BigDecimal.valueOf(outModel.getAiValue());
        this.resultCode = outModel.getServerAiValueIsOk();
        this.digitNum = outModel.getNumNumbers();
        UnitType unitType = UnitType.getElement(outModel.getNumberUnit());
        this.unitType = unitType == null ? -1 : unitType.getKey();
        this.rotateAngle = outModel.getServerRotateAngle();
        this.imagePath = outModel.getOutImagePath();
        this.debugInfo = StringUtil.debugInfoToString(outModel.getDebugInfo());
        this.numberRegion = String.join(",", Arrays.stream(outModel.getServerNumberRegion()).map(Object::toString).toArray(String[]::new));
        this.maxFlow = new BigDecimal(outModel.getMax_flow_per_hour());
        this.imageQuality = outModel.getImageQuality();
        this.meterType = outModel.getMeterType();
        this.numRunCnn = outModel.getNum_run_CNN();
    }

    /**
     * FreeAi结果转换
     * @param sampleResult
     * @param aireadModel
     */
    public TExtendIdentModel(FaSampleResult sampleResult, TAireadModel aireadModel) {
        this.identType = 5;
        this.aireadId = aireadModel.getId();
        this.deviceSn = aireadModel.getDeviceSn();
        this.stationCode = aireadModel.getStationCode();
        this.identTime = sampleResult.getRecogTime();
        if (StringUtils.isNotEmpty(sampleResult.getRecogResult())) {
            this.identValue = new BigDecimal(sampleResult.getRecogResult());
        }
        this.imagePath = sampleResult.getRunLogUrl();
        if(sampleResult.getAiResult_array()!=null){
            this.debugInfo = StringUtil.debugInfoToString(sampleResult.getAiResult_array());
            this.resultCode = sampleResult.getAiResult_array()[0][11];
        }
    }

    private Integer id;

    private Integer aireadId;

    private String deviceSn;

    private String stationCode;

    private Integer identType;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8", locale = "zh")
    private Date identTime;

    private BigDecimal identValue;

    private Integer resultCode;

    private Integer digitNum;

    private Integer unitType;

    private Integer rotateAngle;

    private String imagePath;

    private String debugInfo;

    private String numberRegion;

    private Integer imageQuality;

    private BigDecimal maxFlow;

    private Integer meterType;

    private Integer numRunCnn;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAireadId() {
        return aireadId;
    }

    public void setAireadId(Integer aireadId) {
        this.aireadId = aireadId;
    }

    public String getDeviceSn() {
        return deviceSn;
    }

    public void setDeviceSn(String deviceSn) {
        this.deviceSn = deviceSn == null ? null : deviceSn.trim();
    }

    public String getStationCode() {
        return stationCode;
    }

    public void setStationCode(String stationCode) {
        this.stationCode = stationCode == null ? null : stationCode.trim();
    }

    public Integer getIdentType() {
        return identType;
    }

    public void setIdentType(Integer identType) {
        this.identType = identType;
    }

    public Date getIdentTime() {
        return identTime;
    }

    public void setIdentTime(Date identTime) {
        this.identTime = identTime;
    }

    public BigDecimal getIdentValue() {
        return identValue;
    }

    public void setIdentValue(BigDecimal identValue) {
        this.identValue = identValue;
    }

    public Integer getResultCode() {
        return resultCode;
    }

    public void setResultCode(Integer resultCode) {
        this.resultCode = resultCode;
    }

    public Integer getDigitNum() {
        return digitNum;
    }

    public void setDigitNum(Integer digitNum) {
        this.digitNum = digitNum;
    }

    public Integer getUnitType() {
        return unitType;
    }

    public void setUnitType(Integer unitType) {
        this.unitType = unitType;
    }

    public Integer getRotateAngle() {
        return rotateAngle;
    }

    public void setRotateAngle(Integer rotateAngle) {
        this.rotateAngle = rotateAngle;
    }

    public String getImagePath() {
        return imagePath;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath == null ? null : imagePath.trim();
    }

    public String getDebugInfo() {
        return debugInfo;
    }

    public void setDebugInfo(String debugInfo) {
        this.debugInfo = debugInfo == null ? null : debugInfo.trim();
    }

    public String getNumberRegion() {
        return numberRegion;
    }

    public void setNumberRegion(String numberRegion) {
        this.numberRegion = numberRegion == null ? null : numberRegion.trim();
    }

    public Integer getImageQuality() {
        return imageQuality;
    }

    public void setImageQuality(Integer imageQuality) {
        this.imageQuality = imageQuality;
    }

    public BigDecimal getMaxFlow() {
        return maxFlow;
    }

    public void setMaxFlow(BigDecimal maxFlow) {
        this.maxFlow = maxFlow;
    }

    public Integer getMeterType() {
        return meterType;
    }

    public void setMeterType(Integer meterType) {
        this.meterType = meterType;
    }

    public Integer getNumRunCnn() {
        return numRunCnn;
    }

    public void setNumRunCnn(Integer numRunCnn) {
        this.numRunCnn = numRunCnn;
    }
}