package de.fau.amos.virtualledger.server.api;

import de.fau.amos.virtualledger.dtos.SavingsAccount;
import de.fau.amos.virtualledger.server.auth.Secured;
import de.fau.amos.virtualledger.server.savings.SavingsController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import java.lang.invoke.MethodHandles;
import java.util.*;

/**
 * Endpoints for savings
 */
@Path("/savings")
public class SavingsApiEndpoint {

    private final static Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
    private SavingsController savingsController;


    @Inject
    public SavingsApiEndpoint(SavingsController savingsController) {
        this.savingsController = savingsController;
    }
    protected SavingsApiEndpoint()  {}

    /**
     * Gets all available saving accounts to the authenticated user
     * @param securityContext
     * @return
     */
    @GET
    @Secured
    @Produces(MediaType.APPLICATION_JSON)
    public Response getSavingAccountsEndpoint(@Context SecurityContext securityContext) {
        if(securityContext.getUserPrincipal().getName() == null || securityContext.getUserPrincipal().getName().isEmpty())
        {
            return Response.status(Response.Status.FORBIDDEN).entity("Authentication failed! Your email wasn't found.").build();
        }
        final String email = securityContext.getUserPrincipal().getName();
        logger.info("getSavingAccountsEndpoint of " + email + " was requested");

        return this.getSavingAccounts(email);
    }


    /**
     * Adds a saving accounts to the authenticated user
     * @param securityContext
     * @param savingsAccount
     * @return status 201 if successful
     */
    @POST
    @Secured
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response addSavingAccountEndpoint(@Context SecurityContext securityContext, SavingsAccount savingsAccount) {
        if(securityContext.getUserPrincipal().getName() == null || securityContext.getUserPrincipal().getName().isEmpty())
        {
            return Response.status(Response.Status.FORBIDDEN).entity("Authentication failed! Your email wasn't found.").build();
        }
        final String email = securityContext.getUserPrincipal().getName();
        logger.info("getSavingAccountsEndpoint of " + email + " was requested");

        return this.addSavingAccount(email, savingsAccount);
    }

    /**
     * Does the logic for adding a saving account to a specific user.
     * Handles exceptions and returns corresponding response codes.
     * @param email
     * @param savingsAccount
     * @return status 201 if successful
     */
    private Response addSavingAccount(String email, SavingsAccount savingsAccount) {
        // TODO implement
        return Response.status(Response.Status.CREATED).build();
    }

    /**
     * Does the logic for getting all saving accounts to a specific user.
     * Handles exceptions and returns corresponding response codes.
     * @param email
     * @return
     */
    private Response getSavingAccounts(String email) {
        // TODO implement a SavingsController and call it, do exception handling
        List<SavingsAccount> savingsAccountList = new ArrayList<>();
        for(int i = 0; i < 5; ++i) {
            SavingsAccount savingsAccount = new SavingsAccount("id1", "savingsaccount1", 500.00, 123.12, new Date());
            savingsAccountList.add(savingsAccount);
        }
        return Response.ok(savingsAccountList).build();
    }

}

@@ -211,24 +211,90 @@
 Controller;%0A
+import de.fau.amos.virtualledger.server.savings.SavingsException;%0A
 import org.s
@@ -832,17 +832,16 @@
 oller;%0A%0A
-%0A
     @Inj
@@ -3004,33 +3004,216 @@
 ) %7B%0A
-        // TODO implement
+%0A        try %7B%0A            savingsController.addSavingAccount(email, savingsAccount);%0A        %7D catch (SavingsException ex) %7B%0A            return Response.status(Response.Status.BAD_REQUEST).build();%0A        %7D
 %0A   
@@ -3266,26 +3266,24 @@
 EATED).build
-()
 ;%0A    %7D%0A%0A   
@@ -3531,374 +3531,102 @@
 ) %7B%0A
-        // TODO implement a SavingsController and call it, do exception handling%0A        List%3CSavingsAccount%3E savingsAccountList = new ArrayList%3C%3E();%0A        for(int i = 0; i %3C 5; ++i) %7B%0A            SavingsAccount savingsAccount = new SavingsAccount(%22id1%22, %22savingsaccount1%22, 500.00, 123.12, new Date());%0A            s
+%0A        List%3CSavingsAccount%3E savingsAccountList = savingsController.getS
 aving
-s
 Account
-List.add(savingsAccount);%0A        %7D
+s(email);
 %0A   

use the savingsController in SavingsApiEndpoint