package org.innovateuk.ifs.registration.form;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.innovateuk.ifs.commons.validation.constraints.FieldRequiredIf;
import org.innovateuk.ifs.organisation.resource.OrganisationSearchResult;
import org.innovateuk.ifs.organisation.resource.OrganisationTypeEnum;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Object to store the data that is used for the companies house form, while creating a new application.
 */

@FieldRequiredIf(required = "organisationSearchName", argument = "organisationSearching", predicate = true, message = "{validation.standard.organisationsearchname.required}")
@FieldRequiredIf(required = "organisationName", argument = "manualEntry", predicate = false, message = "{validation.standard.organisationname.required}")
public class OrganisationCreationForm implements Serializable {
    private boolean triedToSave = false;

    @NotNull(message = "{validation.standard.organisationtype.required}")
    private Long organisationTypeId;
    private String organisationSearchName;
    private String searchOrganisationId;
    private boolean organisationSearching;
    private boolean manualEntry = false;
    private transient List<OrganisationSearchResult> organisationSearchResults;
    private String organisationName;

    public OrganisationCreationForm() {
        this.organisationSearchResults = new ArrayList<>();
    }

    public OrganisationCreationForm(List<OrganisationSearchResult> companiesHouseList) {
        this.organisationSearchResults = companiesHouseList;
    }

    public boolean isManualEntry() {
        return manualEntry;
    }

    public void setManualEntry(boolean manualEntry) {
        this.manualEntry = manualEntry;
    }

    public Long getOrganisationTypeId() {
        return organisationTypeId;
    }

    public void setOrganisationTypeId(Long organisationTypeId) {
        this.organisationTypeId = organisationTypeId;
    }

    @JsonIgnore
    public boolean isResearch(){
        if(organisationTypeId != null) {
            return OrganisationTypeEnum.getFromId(organisationTypeId).equals(OrganisationTypeEnum.RESEARCH);
        }
        else {
            return false;
        }
    }

    public String getOrganisationName() {
        return organisationName;
    }

    public void setOrganisationName(String organisationName) {
        this.organisationName = organisationName;
    }

    public String getOrganisationSearchName() {
        return organisationSearchName;
    }

    public void setOrganisationSearchName(String organisationSearchName) {
        this.organisationSearchName = organisationSearchName;
    }

    public boolean isOrganisationSearching() {
        return organisationSearching;
    }

    public void setOrganisationSearching(boolean organisationSearching) {
        this.organisationSearching = organisationSearching;
    }

    @JsonIgnore
    public List<OrganisationSearchResult> getOrganisationSearchResults() {
        return organisationSearchResults;
    }

    public void setOrganisationSearchResults(List<OrganisationSearchResult> organisationSearchResults) {
        this.organisationSearchResults = organisationSearchResults;
    }

    public String getSearchOrganisationId() {
        return searchOrganisationId;
    }

    public void setSearchOrganisationId(String searchOrganisationId) {
        this.searchOrganisationId = searchOrganisationId;
    }

    public boolean isTriedToSave() {
        return triedToSave;
    }

    public void setTriedToSave(boolean triedToSave) {
        this.triedToSave = triedToSave;
    }

    @JsonIgnore
    public OrganisationTypeEnum getOrganisationTypeEnum() {
        return OrganisationTypeEnum.getFromId(organisationTypeId);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        OrganisationCreationForm that = (OrganisationCreationForm) o;

        return new EqualsBuilder()
                .append(triedToSave, that.triedToSave)
                .append(organisationSearching, that.organisationSearching)
                .append(manualEntry, that.manualEntry)
                .append(organisationTypeId, that.organisationTypeId)
                .append(organisationSearchName, that.organisationSearchName)
                .append(searchOrganisationId, that.searchOrganisationId)
                .append(organisationSearchResults, that.organisationSearchResults)
                .append(organisationName, that.organisationName)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(triedToSave)
                .append(organisationTypeId)
                .append(organisationSearchName)
                .append(searchOrganisationId)
                .append(organisationSearching)
                .append(manualEntry)
                .append(organisationSearchResults)
                .append(organisationName)
                .toHashCode();
    }
}

@@ -219,38 +219,25 @@
 org.
-innovateuk.ifs.commons
+hibernate
 .validat
 ion.
@@ -224,35 +224,34 @@
 ibernate.validat
-ion
+or
 .constraints.Fie
@@ -247,31 +247,24 @@
 traints.
-FieldRequiredIf
+NotBlank
 ;%0Aimport
@@ -644,338 +644,8 @@
 %0A */
-%0A%0A@FieldRequiredIf(required = %22organisationSearchName%22, argument = %22organisationSearching%22, predicate = true, message = %22%7Bvalidation.standard.organisationsearchname.required%7D%22)%0A@FieldRequiredIf(required = %22organisationName%22, argument = %22manualEntry%22, predicate = false, message = %22%7Bvalidation.standard.organisationname.required%7D%22)
 %0Apub
@@ -853,24 +853,198 @@
 ationTypeId;
+%0A%0A    @NotBlank(message = %22%7Bvalidation.standard.organisationsearchname.required%7D%22)%0A    // on empty value don't check pattern since then there already is a validation message.
 %0A    private
@@ -1196,24 +1196,24 @@
 ry = false;%0A
-
     private 
@@ -1276,24 +1276,99 @@
 rchResults;%0A
+    @NotBlank(message = %22%7Bvalidation.standard.organisationname.required%7D%22)%0A
     private 

Revert "FieldRequiredIf added for organisationSearchName and organisationName in place of NotBlank."