package cn.axzo.msg.center.api.custombutton;

import cn.axzo.msg.center.service.enums.RouterButtonSourceEnum;
import cn.axzo.msg.center.service.pending.response.MessageButton;
import cn.axzo.msg.center.service.pending.response.MessageButtonProvider;
import cn.axzo.msg.center.service.pending.response.PendingMessageResponse;
import cn.axzo.msg.center.service.pending.response.TodoInfoProvider;
import cn.axzo.msg.center.service.pending.response.v3.model.ParsedModelV3;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.function.Function;

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

/**
 * @author yanglin
 */
@RequiredArgsConstructor
public class ProposedButtonFilter {

    public static void adjustProposedButtons(
            PendingMessageResponse response, boolean forPromoter) {
        filterProposedButtons(response, response, forPromoter);
        ParsedModelV3 modelV3 = response.getModelV3();
        if (modelV3 != null)
            filterProposedButtons(response, modelV3, forPromoter);
    }

    public static <T extends MessageButton> void filterProposedButtons(
            TodoInfoProvider todoInfo, MessageButtonProvider<T> buttonProvider, boolean forPromoter) {
        List<T> buttons = buttonProvider.buttons();
        if (CollectionUtils.isEmpty(buttons)) return;
        ProposedButtons proposedButtons = todoInfo.getProposedButtons();
        Function<String, Boolean> isConfiguredProposedBtnFun = btnKey -> {
            if (proposedButtons == null || CollectionUtils.isEmpty(proposedButtons.getConfiguredButtonKeys()))
                return false;
            return proposedButtons.getConfiguredButtonKeys().contains(btnKey);
        };
        Function<String, ProposedButton> findProposedBtnFun = btnKey -> {
            if (proposedButtons == null || CollectionUtils.isEmpty(proposedButtons.getProposedButtons()))
                return null;
            return proposedButtons.getProposedButtons().stream()
                    .filter(i -> btnKey.equals(i.getButtonKey()))
                    .findFirst()
                    .orElse(null);
        };
        Function<T, Boolean> syncBtnFun = btn -> {
            // 没有配置可见的自定义的按钮
            ProposedButton proposedBtn = findProposedBtnFun.apply(btn.key());
            if (proposedBtn == null || CollectionUtils.isEmpty(proposedBtn.getConfigs()))
                return false;
            return proposedBtn.getConfigs().stream()
                    .anyMatch(c -> forPromoter
                            ? c.isVisible(todoInfo.getBizFinalState())
                            : c.isVisible(todoInfo.getState()));
        };
        List<T> filteredButtons = buttons.stream()
                .filter(routerBtn -> {
                    ProposedButton proposedBtn = findProposedBtnFun.apply(routerBtn.key());
                    boolean isNormalCustomBtn = proposedBtn == null
                            && !isConfiguredProposedBtnFun.apply(routerBtn.key())
                            && routerBtn.getSource() == RouterButtonSourceEnum.CUSTOM;
                    return isNormalCustomBtn || syncBtnFun.apply(routerBtn);
                })
                .collect(toList());
        buttonProvider.buttons(filteredButtons);
    }

}