package cn.axzo.msg.center.service.pending.card.domain;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author yanglin
 */
@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class CardButtonStates {

    private final List<CardButtonState> states;

    public static CardButtonStates create(List<CardButtonState> buttonStates) {
        return new CardButtonStates(new ArrayList<>(
                buttonStates == null ? Collections.emptyList(): buttonStates));
    }

    public void setActionPerformed(String buttonCode) {
        states.removeIf(s -> s.getButtonCode().equals(buttonCode));
        CardButtonState state = new CardButtonState();
        state.setButtonCode(buttonCode);
        state.setIsActionPerformed(true);
        states.add(state);
    }

    public boolean isButtonActionPerformed(String buttonCode) {
        return states.stream()
                .anyMatch(state -> {
                    boolean isTheButton = state.getButtonCode().equals(buttonCode);
                    boolean actionPerformed = state.determineIsActionPerformed();
                    return isTheButton && actionPerformed;
                });
    }

}