package com.amee.platform.resource.profileitem.v_3_6;

import com.amee.base.domain.Since;
import com.amee.base.resource.RequestWrapper;
import com.amee.base.resource.ResourceBeanFinder;
import com.amee.base.transaction.AMEETransaction;
import com.amee.calculation.service.CalculationService;
import com.amee.domain.APIVersion;
import com.amee.domain.ProfileItemService;
import com.amee.domain.item.profile.ProfileItem;
import com.amee.domain.profile.Profile;
import com.amee.domain.sheet.Choice;
import com.amee.domain.sheet.Choices;
import com.amee.platform.resource.ResourceService;
import com.amee.platform.resource.profileitem.ProfileItemResource;
import com.amee.platform.science.ReturnValues;
import com.amee.service.auth.ResourceAuthorizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Scope("prototype")
@Since("3.6.0")
public class ProfileItemBuilder_3_6_0 implements ProfileItemResource.Builder {

    @Autowired
    private ProfileItemService profileItemService;

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private ResourceAuthorizationService resourceAuthorizationService;

    @Autowired
    private ResourceBeanFinder resourceBeanFinder;

    @Autowired
    private CalculationService calculationService;

    private ProfileItemResource.Renderer renderer;

    @Override
    @AMEETransaction
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Object handle(RequestWrapper requestWrapper) {

        // Get resource entities for this request
        Profile profile = resourceService.getProfile(requestWrapper);
        ProfileItem profileItem = resourceService.getProfileItem(requestWrapper, profile);

        // Authorised for profile item?
        resourceAuthorizationService.ensureAuthorizedForBuild(
            requestWrapper.getAttributes().get("activeUserUid"), profileItem);

        // Handle the profile item
        handle(requestWrapper, profileItem);
        ProfileItemResource.Renderer renderer = getRenderer(requestWrapper);
        renderer.ok();
        return renderer.getObject();
    }

    @Override
    public void handle(RequestWrapper requestWrapper, ProfileItem profileItem) {

        // Get renderer
        ProfileItemResource.Renderer renderer = getRenderer(requestWrapper);
        renderer.start();

        // Get rendering options from matrix parameters
        boolean full = requestWrapper.getMatrixParameters().containsKey("full");
        boolean audit = requestWrapper.getMatrixParameters().containsKey("audit");
        boolean amounts = requestWrapper.getMatrixParameters().containsKey("amounts");
        boolean name = requestWrapper.getMatrixParameters().containsKey("name");
        boolean dates = requestWrapper.getMatrixParameters().containsKey("dates");
        boolean category = requestWrapper.getMatrixParameters().containsKey("category");

        // New Profile Item and basic
        renderer.newProfileItem(profileItem);
        renderer.addBasic();

        // Optionals
        if (audit || full) {
            renderer.addAudit();
        }
        if (name || full) {
            renderer.addName();
        }
        if (dates || full) {
            TimeZone currentUserTimeZone = resourceService.getCurrentUser(requestWrapper).getTimeZone();
            renderer.addDates(currentUserTimeZone);
        }
        if (category || full) {
            renderer.addCategory();
        }
        if (amounts || full) {
            renderer.addReturnValues(profileItem.getAmounts());
        }
    }

    @Override
    public ProfileItemResource.Renderer getRenderer(RequestWrapper requestWrapper) {
        if (renderer == null) {
            renderer =
                (ProfileItemResource.Renderer) resourceBeanFinder.getRenderer(ProfileItemResource.Renderer.class, requestWrapper);
        }
        return renderer;
    }
}

@@ -48,16 +48,336 @@
 v_3_6;%0A%0A
+import java.util.TimeZone;%0A%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.context.annotation.Scope;%0Aimport org.springframework.stereotype.Service;%0Aimport org.springframework.transaction.annotation.Propagation;%0Aimport org.springframework.transaction.annotation.Transactional;%0A%0A
 import c
@@ -399,24 +399,24 @@
 main.Since;%0A
-
 import com.a
@@ -609,43 +609,8 @@
 ce;%0A
-import com.amee.domain.APIVersion;%0A
 impo
@@ -741,83 +741,8 @@
 le;%0A
-import com.amee.domain.sheet.Choice;%0Aimport com.amee.domain.sheet.Choices;%0A
 impo
@@ -859,55 +859,8 @@
 ce;%0A
-import com.amee.platform.science.ReturnValues;%0A
 impo
@@ -917,320 +917,8 @@
 ice;
-%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.context.annotation.Scope;%0Aimport org.springframework.stereotype.Service;%0Aimport org.springframework.transaction.annotation.Propagation;%0Aimport org.springframework.transaction.annotation.Transactional;%0A%0Aimport java.util.*;
 %0A%0A@S
@@ -3009,16 +3009,99 @@
 egory%22);
+%0A        boolean notes = requestWrapper.getMatrixParameters().containsKey(%22notes%22);
 %0A%0A      
@@ -3746,24 +3746,90 @@
 ;%0A        %7D%0A
+        if(notes %7C%7C full)%7B%0A        %09renderer.addNote();%0A        %7D%0A
     %7D%0A%0A    @

add support for new note field in ProfileItem responses