package com.airlock.iam.transactionapproval.domain.model.cronto;

import com.airlock.iam.transactionapproval.domain.model.flow.TransactionApprovalMessageAttributes;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public final class CrontoTransactionApprovalParameterFactory {
   private CrontoTransactionApprovalParameterFactory() {
   }

   public static CrontoTransactionApprovalParameters createCrontoTransactionApprovalParameters(TransactionApprovalMessageAttributes messageAttributes) {
      Map<String, String> map = messageAttributes.attributeAsRawKeyValues();
      return CrontoTransactionApprovalParameters.builder()
         .title(Optional.ofNullable(map.get("title")))
         .amount(Optional.ofNullable(map.get("amount")))
         .currency(Optional.ofNullable(map.get("currency")))
         .amountLabel(Optional.ofNullable(map.get("amountLabel")))
         .details(getDetails(map))
         .build();
   }

   private static Map<String, String> getDetails(Map<String, String> map) {
      return map.entrySet()
         .stream()
         .filter(e -> !e.getKey().equals("title"))
         .filter(e -> !e.getKey().equals("amount"))
         .filter(e -> !e.getKey().equals("currency"))
         .filter(e -> !e.getKey().equals("amountLabel"))
         .filter(e -> !e.getKey().equals("tokenLabel"))
         .collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
   }
}
