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

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.yuchen.saas.park.api.entity.door.config.DoorAccessControlConfigIssueTask;
import com.yuchen.saas.park.service.mapper.domain.config.details.DoorAccessControlConfigDetailsRO;
import lombok.Data;

import java.util.List;
import java.util.Set;

import static com.yuchen.saas.park.api.commons.enums.DoorAccessControlConfigType.CARD;
import static com.yuchen.saas.park.api.commons.enums.DoorAccessControlConfigType.FACIAL;
import static java.util.stream.Collectors.*;

@Data
public class DoorAccessControlConfigDetailsSO extends DoorAccessControlConfigDetailsRO {
    @JsonIgnore
    private List<DoorAccessControlConfigIssueTask> tasks;

    @JsonProperty
    public Set<Integer> getCardState() {
        return this.getState(tasks.stream().filter(o -> o.getType() == CARD).collect(toList()));
    }

    @JsonProperty
    public Set<Integer> getFacialState() {
        return this.getState(tasks.stream().filter(o -> o.getType() == FACIAL).collect(toList()));
    }

    private Set<Integer> getState(List<DoorAccessControlConfigIssueTask> list) {
        return list.isEmpty() ? Set.of(4) : list.stream().map(this::getState).collect(toSet());
    }

    private int getState(DoorAccessControlConfigIssueTask o) {
        if (!o.isProcessed()) {
            return 1;
        }

        if (o.isDisabled() && !o.isSuccessful()) {
            return 2;
        }

        return 3;// TODO:下发但失败?
    }
}
