package extractcommand.step08;

import extractcommand.step00.*;
import extractcommand.AtmCommand;

public class AtmSession {

    AccountService accountService;
    Logger logger;

    public AtmSession(AccountService accountService, Logger logger) {
        this.accountService = accountService;
        this.logger = logger;
    }

    public User checkOwner(final int accountNumber) throws SessionException {
        return new CheckOwner(logger, accountService, accountNumber).execute();
    }

    public Money checkBalance(final int accountNumber) throws SessionException {
        return new CheckBalance(accountNumber, logger, accountService).execute();
    }

    public Money deposit(final int accountNumber, final Money amount) throws SessionException {
        return new Deposit(accountNumber, amount, logger, accountService).execute();
    }

    public Money withdraw(final int accountNumber, final Money amount) throws SessionException {
        return new Withdraw(accountNumber, amount, logger, accountService).execute();
    }

    public void transfer(int fromAccountNumber, int toAccountNumber, Money amount) throws SessionException {
        try {
            Account fromAccount = accountService.findAccount(fromAccountNumber);
            Account toAccount = accountService.findAccount(toAccountNumber);
            verifyBalance(fromAccount, amount);
            fromAccount.withdraw(amount);
            toAccount.deposit(amount);
        }
        catch (AccountServiceException e) {
            logger.log(e);
            throw new SessionException(e);
        }
    }

    private void verifyBalance(Account account, Money amount) throws SessionException {
        Money balance = account.getBalance();
        if (balance.subtract(amount).isNegative()) {
            throw new SessionException("Insufficient funds");
        }
    }

    // * Make the Withdraw class static.
    // * Make verifyBalance() static. (Alt-Enter)
    // Later we will move any static methods to a common base
    // class of the relevant command classes.
    private class Withdraw extends AtmCommand<Money> {
        private final int accountNumber;
        private final Money amount;
        private AccountService accountService;

        public Withdraw(int accountNumber, Money amount, Logger logger, AccountService accountService) {
            super(logger);
            this.accountNumber = accountNumber;
            this.amount = amount;
            this.accountService = accountService;
        }

        protected Money doExecute() throws AccountServiceException, SessionException {
            Account account = accountService.findAccount(accountNumber);
            verifyBalance(account, amount);
            account.withdraw(amount);
            return account.getBalance();
        }
    }
}

