package cn.com.oa.model.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

@Data
public class BarrierCameraAlarmDTO {

    @JsonProperty("AlarmInfoPlate")
    private AlarmInfoPlate alarmInfoPlate;

    @Data
    public static class AlarmInfoPlate {
        private int channel;
        private String deviceName;
        private String ipaddr;
        private int openflag;

        @JsonProperty("enterprie_name")
        private String enterprie_name;

        @JsonProperty("uniform_credit_code")
        private String uniform_credit_code;

        private String serialno;

        @JsonProperty("result")
        private Result result;
    }
    @Data
    public static class Result {

        @JsonProperty("PlateResult")
        private PlateResult plateResult;
    }

    @Data
    public static class PlateResult {
        private int bright;
        private int carBright;
        private int carColor;
        private int colorType;
        private int colorValue;
        private int confidence;
        private int direction;
        private String license;

        @JsonProperty("plate_encryption_state")
        private int plate_encryption_state;

        @JsonProperty("Whitelist")
        private int whitelist;

        @JsonProperty("location")
        private Location location;

        @JsonProperty("timeStamp")
        private TimeStamp timeStamp;

        private int timeUsed;
        private int triggerType;
        private int type;
        private double speed;

        @JsonProperty("radarSpeed")
        private RadarSpeed radarSpeed;

        private long vehicleId;
        private boolean realplate;
        private int retryflag;
    }

    @Data
    public static class Location {

        @JsonProperty("RECT")
        private Rect rect;
    }

    @Data
    public static class Rect {
        private int left;
        private int top;
        private int right;
        private int bottom;
    }

    @Data
    public static class TimeStamp {

        @JsonProperty("Timeval")
        private Timeval timeval;
    }

    @Data
    public static class Timeval {
        private long sec;
        private int usec;
    }

    @Data
    public static class RadarSpeed {

        @JsonProperty("Speed")
        private Speed speed;
    }

    @Data
    private static class Speed {

        @JsonProperty("PerHour")
        private double perHour;

        @JsonProperty("Direction")
        private int direction;
    }

    // AlarmInfoPlate 层字段
    public int getChannel() {
        return alarmInfoPlate != null ? alarmInfoPlate.channel : 0;
    }

    public String getDeviceName() {
        return alarmInfoPlate != null ? alarmInfoPlate.deviceName : null;
    }

    public String getIpaddr() {
        return alarmInfoPlate != null ? alarmInfoPlate.ipaddr : null;
    }

    public int getOpenflag() {
        return alarmInfoPlate != null ? alarmInfoPlate.openflag : 0;
    }

    public String getEnterprie_name() {
        return alarmInfoPlate != null ? alarmInfoPlate.enterprie_name : null;
    }

    public String getUniform_credit_code() {
        return alarmInfoPlate != null ? alarmInfoPlate.uniform_credit_code : null;
    }

    public String getSerialno() {
        return alarmInfoPlate != null ? alarmInfoPlate.serialno : null;
    }

    // PlateResult 层字段
    public String getLicense() {
        return alarmInfoPlate != null
                && alarmInfoPlate.result != null
                && alarmInfoPlate.result.plateResult != null
                ? alarmInfoPlate.result.plateResult.license
                : null;
    }

    public int getConfidence() {
        return alarmInfoPlate != null
                && alarmInfoPlate.result != null
                && alarmInfoPlate.result.plateResult != null
                ? alarmInfoPlate.result.plateResult.confidence
                : 0;
    }

    public boolean getRealplate() {
        return alarmInfoPlate != null
                && alarmInfoPlate.result != null
                && alarmInfoPlate.result.plateResult != null
                ? alarmInfoPlate.result.plateResult.realplate
                : false;
    }

    public int getColorType() {
        return alarmInfoPlate != null
                && alarmInfoPlate.result != null
                && alarmInfoPlate.result.plateResult != null
                ? alarmInfoPlate.result.plateResult.colorType
                : 99;
    }

    public long getVehicleId() {
        return alarmInfoPlate != null
                && alarmInfoPlate.result != null
                && alarmInfoPlate.result.plateResult != null
                ? alarmInfoPlate.result.plateResult.vehicleId
                : 0;
    }

    public int getWhitelist() {
        return alarmInfoPlate != null
                && alarmInfoPlate.result != null
                && alarmInfoPlate.result.plateResult != null
                ? alarmInfoPlate.result.plateResult.whitelist
                : 0;
    }

    public LocalDateTime getTimeStamp() {
        if (alarmInfoPlate != null
                && alarmInfoPlate.result != null
                && alarmInfoPlate.result.plateResult != null
                && alarmInfoPlate.result.plateResult.timeStamp != null
                && alarmInfoPlate.result.plateResult.timeStamp.timeval != null) {

            long sec = alarmInfoPlate.result.plateResult.timeStamp.timeval.sec;
            int usec = alarmInfoPlate.result.plateResult.timeStamp.timeval.usec;

            // 创建 Instant（支持纳秒）→ LocalDateTime（指定时区）
            Instant instant = Instant.ofEpochSecond(sec, usec * 1000L); // usec 是微秒，需转换为纳秒
            return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        }
        return null;
    }

}
