package com.yuchen.saas.park.service.service.door.communication.domain.user;

import com.yuchen.saas.park.service.service.door.communication.domain.CommandParameter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Positive;
import java.util.Map;
import java.util.Set;

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

@Data
@AllArgsConstructor(staticName = "of")
@NoArgsConstructor
public class AddUserParameter implements CommandParameter {
    @Positive
    private long userId;
    @NotBlank
    private String username;
    @NotEmpty
    private Set<@Positive Long> timeConfigIds;

    @Override
    public Map<@NotBlank String, Object> encoding() {
        return Map.of(
          "employeeNo", userId,
          "name", username,
          "userType", 1,
          "gender", 1,
          "rightPlan", timeConfigIds.stream().map(o -> PlanOptions.of("1", String.valueOf(o))).collect(toSet()),
          "userVerifyMode", 1,
          "expire", Options.of().encoding()
        );
    }

    @Data(staticConstructor = "of")
    private static class PlanOptions {
        @NotBlank
        private final String doorNo;
        @NotBlank
        private final String planTemplateNo;
    }

    @Data(staticConstructor = "of")
    private static class Options implements CommandParameter {
        @Override
        public Map<@NotBlank String, Object> encoding() {
            return Map.of(
              "enable", 0,
              "beginTime", "2022-04-15T11:53:00Z",
              "endTime", "2040-04-15T11:53:00Z",
              "timeType","local"
            );
        }
    }
}
