package com.centralnabanka.service.implementation;

import com.centralnabanka.model.Bank;
import com.centralnabanka.repository.BankRepository;
import com.centralnabanka.service.MessageSenderService;
import com.centralnabanka.service.RtgsService;
import com.centralnabanka.types.Mt103;
import com.centralnabanka.types.Mt900;
import com.centralnabanka.types.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.SoapFaultException;
import sun.plugin2.message.Message;

import java.math.BigDecimal;

@Service
public class RtgsServiceImpl extends WebServiceGatewaySupport implements RtgsService {

    private BankRepository bankRepository;
    private MessageSenderService messageSender;

    @Autowired
    public RtgsServiceImpl(BankRepository bankRepository, MessageSenderService messageSender) {
        this.bankRepository = bankRepository;
        this.messageSender = messageSender;
    }

    @Override
    public Mt900 processMt103Request(Mt103 request) {
        transferMoney(request);

        messageSender.sendMessagesToDebtor(request);

        return messageSender.createMt900(request);
    }

    @Transactional
    private void transferMoney(Mt103 request) {
        String debtorSwiftCode = request.getSwiftKodDuznika();
        Bank debtor = bankRepository.findBySwiftCode(debtorSwiftCode).orElseThrow(() ->
            new SoapFaultException("Bank with SWIFT code: " + debtorSwiftCode + " not found."));

        String creditorSwiftCode = request.getSwiftKodPoverioca();
        Bank creditor = bankRepository.findBySwiftCode(creditorSwiftCode).orElseThrow(() ->
                new SoapFaultException("Bank with SWIFT code: " + creditorSwiftCode + " not found."));

        BigDecimal transferAmount = request.getPodaciOPlacanju().getIznos();

        creditor.setAccountBalance(creditor.getAccountBalance().subtract(transferAmount));
        bankRepository.save(creditor);

        debtor.setAccountBalance(debtor.getAccountBalance().add(transferAmount));
        bankRepository.save(debtor);
    }
}

@@ -321,55 +321,8 @@
 00;%0A
-import com.centralnabanka.types.ObjectFactory;%0A
 impo
@@ -626,44 +626,8 @@
 ion;
-%0Aimport sun.plugin2.message.Message;
 %0A%0Aim
@@ -1920,25 +1920,24 @@
 znos();%0A
-%0A
         
 creditor
@@ -1932,20 +1932,30 @@
     
-c
+BigDecimal newC
 reditor
-.set
 Acco
@@ -1964,17 +1964,19 @@
 tBalance
-(
+ = 
 creditor
@@ -2012,32 +2012,284 @@
 (transferAmount)
+;%0A%0A        if (newCreditorAccountBalance.signum() == -1) %7B%0A            throw new SoapFaultException(%22Bank with SWIFT code: %22 + creditorSwiftCode + %22 has negative account balance%22);%0A        %7D%0A%0A        creditor.setAccountBalance(newCreditorAccountBalance
 );%0A        bankR

Check creditor account balance