package com.yuchen.saas.park.service.service.domain;

import com.yuchen.saas.park.api.dto.door.DoorAccessControlCardConditionDTO;
import lombok.Data;
import lombok.val;

import javax.annotation.Nullable;
import java.util.Objects;
import java.util.Set;

import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.*;

@Data
public class DoorAccessControlCardConditionSO {
    @Nullable
    private Long departmentId;
    /**
     * 递归:包含下级
     */
    private boolean recursive;

    @Nullable
    private String username;
    @Nullable
    private Integer userGender;

    @Nullable
    private Boolean fingerprint;
    @Nullable
    private Boolean facial;

    @Nullable
    private Integer minCardCount;
    @Nullable
    private Integer maxCardCount;

    // TODO
    public static DoorAccessControlCardConditionSO from(DoorAccessControlCardConditionDTO o) {
        val set = ofNullable(o.getBiometrics()).map(x -> x.stream().filter(Objects::nonNull).collect(toSet())).orElse(Set.of());

        val so = new DoorAccessControlCardConditionSO();
        so.setDepartmentId(o.getDepartmentId());
        so.setRecursive(o.isRecursive());
        so.setUsername(o.getUsername());
        so.setUserGender(o.getUserGender());
        so.setFingerprint(parseFingerprint(set));
        so.setFacial(parseFacial(set));
        so.setMinCardCount(o.getMinCardCount());
        so.setMaxCardCount(o.getMaxCardCount());

        return so;
    }

    @Nullable
    private static Boolean parseFingerprint(Set<Integer> set) {
        if ((set.contains(3) && set.contains(4)) || (!set.contains(3) && !set.contains(4))) {
            return null;
        }

        return !set.contains(3);
    }

    @Nullable
    private static Boolean parseFacial(Set<Integer> set) {
        if ((set.contains(1) && set.contains(2)) || (!set.contains(1) && !set.contains(2))) {
            return null;
        }

        return !set.contains(1);
    }
}
