package com.yuchen.saas.park.service.mapper.domain.config.details;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nullable;
import javax.validation.constraints.Positive;
import java.time.LocalDateTime;
import java.util.Set;

@Data
public class DoorAccessControlConfigDetailsCondition {
    @Nullable
    private Set<Long> userIds;

    @Nullable
    private Set<@Positive Long> doorAccessControlIds;

    /**
     * -1: 过期
     * > 0: 剩余*天
     */
    private Integer effectiveTime;

    private Set<Integer> cardStates;
    private Set<Integer> facialStates;

    // 内部
    @JsonIgnore
    private LocalDateTime startTime;
    @JsonIgnore
    private LocalDateTime endTime;

    @JsonIgnore
    private Boolean cardToIssued;
    @JsonIgnore
    private Boolean cardToDeleted;
    @JsonIgnore
    private Boolean cardBeenIssued;
    @JsonIgnore
    private Boolean cardUnConfig;

    @JsonIgnore
    private Boolean facialToIssued;
    @JsonIgnore
    private Boolean facialToDeleted;
    @JsonIgnore
    private Boolean facialBeenIssued;
    @JsonIgnore
    private Boolean facialUnConfig;

    public DoorAccessControlConfigDetailsCondition parse() {
        this.parseEffectiveTime();
        this.parseCardCondition();
        this.parseFacialCondition();
        return this;
    }

    private void parseEffectiveTime() {
        if (this.effectiveTime == null) {
            return;
        }

        if (this.effectiveTime == -1) {
            this.endTime = LocalDateTime.now();
        }

        if (this.effectiveTime > 0) {
            this.endTime = LocalDateTime.now().plusDays(this.effectiveTime);
        }
    }

    private void parseCardCondition() {
        if (CollectionUtils.isEmpty(this.cardStates)) {
            return;
        }

        if (cardStates.contains(1)) {
            this.cardToIssued = true;
        }
        if (cardStates.contains(2)) {
            this.cardToDeleted = true;
        }
        if (cardStates.contains(3)) {
            this.cardToDeleted = true;
        }
        if (cardStates.contains(4)) {
            this.cardUnConfig = true;
        }
    }

    private void parseFacialCondition() {
        if (CollectionUtils.isEmpty(this.facialStates)) {
            return;
        }

        if (facialStates.contains(1)) {
            this.facialToIssued = true;
        }
        if (facialStates.contains(2)) {
            this.facialToDeleted = true;
        }
        if (facialStates.contains(3)) {
            this.facialBeenIssued = true;
        }
        if (facialStates.contains(4)) {
            this.facialUnConfig = true;
        }
    }
}
