package org.innovateuk.ifs.dashboard.viewmodel;

import org.innovateuk.ifs.applicant.resource.dashboard.DashboardPreviousRowResource;
import org.innovateuk.ifs.application.resource.ApplicationState;
import org.innovateuk.ifs.project.resource.ProjectState;

import java.time.LocalDate;

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.innovateuk.ifs.application.resource.ApplicationState.*;

/**
 * View model for each application row in the 'Previous' section of the applicant dashboard.
 */
public class PreviousDashboardRowViewModel extends AbstractApplicantDashboardRowViewModel {

    private final ApplicationState applicationState;
    private final LocalDate startDate;
    private final ProjectState projectState;
    private final Long projectId;

    public PreviousDashboardRowViewModel(String title,
                                         long applicationId,
                                         Long projectId,
                                         String competitionTitle,
                                         ApplicationState applicationState,
                                         ProjectState projectState,
                                         LocalDate startDate) {
        super(title, applicationId, competitionTitle);
        this.applicationState = applicationState;
        this.projectState = projectState;
        this.projectId = projectId;
        this.startDate = startDate;
    }

    public PreviousDashboardRowViewModel(DashboardPreviousRowResource resource){
        super(resource.getTitle(), resource.getApplicationId(), resource.getCompetitionTitle());
        this.applicationState = resource.getApplicationState();
        this.projectState = resource.getProjectState();
        this.projectId = resource.getProjectId();
        this.startDate = resource.getStartDate();
    }

    public ApplicationState getApplicationState() {
        return applicationState;
    }

    public LocalDate getStartDate() {
        return startDate;
    }

    private boolean hasProject() {
        return projectState != null;
    }

    /* View logic */
    public boolean isRejected() {
        return REJECTED.equals(applicationState);
    }

    public boolean isApproved() {
        return APPROVED.equals(applicationState) && !hasProject();
    }

    public boolean isCreatedOrOpen() {
        return OPENED.equals(applicationState)
                ||  CREATED.equals(applicationState);
    }

    public boolean isInformedIneligible() {
        return INELIGIBLE_INFORMED.equals(applicationState);
    }

    public boolean isWithdrawn() {
        return hasProject() && projectState.isWithdrawn();
    }

    public boolean isLive() {
        return hasProject() && (projectState.isLive() || projectState.isCompletedOffline());
    }

    @Override
    public String getLinkUrl() {
        return hasProject() ?
                String.format("/project-setup/project/%s", projectId) :
                String.format("/application/%s/summary", getApplicationNumber());
    }

    @Override
    public String getTitle() {
        return !isNullOrEmpty(title) ? title : "Untitled application";
    }

}

@@ -2876,111 +2876,8 @@
 urn 
-hasProject() ?%0A                String.format(%22/project-setup/project/%25s%22, projectId) :%0A                
 Stri

Update url to always point to application in previous section