package com.njzscloud.common.security.support;


import com.njzscloud.common.core.tuple.Tuple2;
import com.njzscloud.common.mvc.validator.Constrained;
import com.njzscloud.common.mvc.validator.ValidRule;
import com.njzscloud.common.security.contant.AuthWay;
import com.njzscloud.common.security.contant.ClientCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

import java.security.Principal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
@RequiredArgsConstructor
public abstract class LoginForm implements Constrained, Principal {
    /**
     * 登录方式
     */
    private final AuthWay authWay;

    private ClientCode clientCode;

    public Tuple2<Boolean, String> validate() {
        ValidRule[] rules = rules();
        List<ValidRule> ruleList = Arrays.stream(rules).collect(Collectors.toList());
        ruleList.add(ValidRule.of(() -> clientCode != null, "未指定要使用的客户端"));
        String message = ruleList.stream()
            .filter(it -> !it.predicate.get())
            .map(it -> it.message)
            .collect(Collectors.joining("\n"));
        return Tuple2.of(message.isEmpty(), message);
    }
}
