package org.innovateuk.ifs.project.grantofferletter.populator;

import org.innovateuk.ifs.address.resource.AddressResource;
import org.innovateuk.ifs.competition.resource.CompetitionResource;
import org.innovateuk.ifs.competition.service.CompetitionRestService;
import org.innovateuk.ifs.finance.resource.ProjectFinanceResource;
import org.innovateuk.ifs.organisation.resource.OrganisationResource;
import org.innovateuk.ifs.project.ProjectService;
import org.innovateuk.ifs.project.finance.service.ProjectFinanceNotesRestService;
import org.innovateuk.ifs.project.finance.service.ProjectFinanceRestService;
import org.innovateuk.ifs.project.grantofferletter.viewmodel.GrantOfferLetterTemplateViewModel;
import org.innovateuk.ifs.project.resource.ProjectResource;
import org.innovateuk.ifs.project.resource.ProjectUserResource;
import org.innovateuk.ifs.project.service.ProjectRestService;
import org.innovateuk.ifs.threads.resource.NoteResource;
import org.innovateuk.ifs.user.resource.UserResource;
import org.innovateuk.ifs.user.service.OrganisationRestService;
import org.innovateuk.ifs.user.service.UserRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class GrantOfferLetterTemplatePopulator {

    @Autowired
    private ProjectRestService projectRestService;

    @Autowired
    private CompetitionRestService competitionRestService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private OrganisationRestService organisationRestService;

    @Autowired
    private UserRestService userRestService;

    @Autowired
    private ProjectFinanceRestService projectFinanceRestService;

    @Autowired
    private ProjectFinanceNotesRestService projectFinanceNotesRestService;

    public GrantOfferLetterTemplateViewModel populate(long projectId) {

        ProjectResource projectResource = projectRestService.getProjectById(projectId).getSuccess();
        String projectName = projectResource.getName();
        long applicationId = projectResource.getApplication();
        CompetitionResource competitionResource = competitionRestService.getCompetitionById(projectResource.getCompetition()).getSuccess();
        String competitionName = competitionResource.getName();
        ProjectUserResource projectUserResource = projectService.getProjectManager(projectId).get();
        OrganisationResource leadOrg = organisationRestService.getOrganisationById(projectUserResource.getOrganisation()).getSuccess();
        String leadOrgName = leadOrg.getName();
        UserResource user = userRestService.retrieveUserById(projectUserResource.getUser()).getSuccess();
        String projectManagerFirstName = user.getFirstName();
        String projectManagerLastName = user.getLastName();
        List<ProjectFinanceResource> allProjectFinances = projectFinanceRestService.getProjectFinances(projectResource.getId()).getSuccess();
        List<NoteResource> allProjectNotes = new ArrayList<>();
        allProjectFinances.forEach(projectFinance ->
                                           projectFinanceNotesRestService.findAll(projectFinance.getId())
                                                   .ifSuccessful(allProjectNotes::addAll)
        );

        return new GrantOfferLetterTemplateViewModel(applicationId,
                                                     projectManagerFirstName,
                                                     projectManagerLastName,
                                                     getAddressLines(projectResource),
                                                     competitionName,
                                                     projectName,
                                                     leadOrgName,
                                                     allProjectNotes,
                                                     competitionResource.getTermsAndConditions().getTemplate());
    }

    private List<String> getAddressLines(ProjectResource project) {
        List<String> addressLines = new ArrayList<>();
        if (project.getAddress() != null) {
            AddressResource address = project.getAddress();
            addressLines.add(address.getAddressLine1() != null ? address.getAddressLine1() : "");
            addressLines.add(address.getAddressLine2() != null ? address.getAddressLine2() : "");
            addressLines.add((address.getAddressLine3() != null ? address.getAddressLine3() : ""));
            addressLines.add(address.getTown() != null ? address.getTown() : "");
            addressLines.add(address.getPostcode() != null ? address.getPostcode() : "");
        }
        return addressLines;
    }

}

@@ -1348,180 +1348,532 @@
     
-@Autowired%0A    private ProjectRestService projectRestService;%0A%0A    @Autowired%0A    private CompetitionRestService competitionRestService;%0A%0A    @Autowired%0A    private
+private final ProjectRestService projectRestService;%0A%0A    private final CompetitionRestService competitionRestService;%0A%0A    private final ProjectService projectService;%0A%0A    private final OrganisationRestService organisationRestService;%0A%0A    private final UserRestService userRestService;%0A%0A    private final ProjectFinanceRestService projectFinanceRestService;%0A%0A    private final ProjectFinanceNotesRestService projectFinanceNotesRestService;%0A%0A    @Autowired%0A    public GrantOfferLetterTemplatePopulator(%0A           
  Project
 Serv
@@ -1860,32 +1860,36 @@
          Project
+Rest
 Service projectS
@@ -1891,184 +1891,664 @@
 ject
+Rest
 Service
-;%0A%0A    @Autowired%0A    private OrganisationRestService organisationRestService;%0A%0A    @Autowired%0A    private UserRestService userRestService;%0A%0A    @Autowired%0A    private P
+,%0A            CompetitionRestService competitionRestService,%0A            ProjectService projectService,%0A            OrganisationRestService organisationRestService,%0A            UserRestService userRestService,%0A            ProjectFinanceRestService projectFinanceRestService,%0A            ProjectFinanceNotesRestService projectFinanceNotesRestService%0A    ) %7B%0A        this.projectRestService = projectRestService;%0A        this.competitionRestService = competitionRestService;%0A        this.projectService = projectService;%0A        this.organisationRestService = organisationRestService;%0A        this.userRestService = userRestService;%0A        this.p
 roje
@@ -2559,32 +2559,34 @@
 nanceRestService
+ =
  projectFinanceR
@@ -2601,37 +2601,22 @@
 ce;%0A
-%0A
     
-@Autowired%0A    private P
+    this.p
 roje
@@ -2632,32 +2632,34 @@
 NotesRestService
+ =
  projectFinanceN
@@ -2670,24 +2670,30 @@
 RestService;
+%0A    %7D
 %0A%0A    public

Tidy up