package org.mifosplatform.organisation.staff.serialization;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.mifosplatform.infrastructure.core.data.ApiParameterError;
import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder;
import org.mifosplatform.infrastructure.core.exception.InvalidJsonException;
import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException;
import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

@Component
public final class StaffCommandFromApiJsonDeserializer {

    /**
     * The parameters supported for this command.
     */
    private final Set<String> supportedParameters = new HashSet<String>(Arrays.asList("firstname", "lastname", "officeId", "isLoanOfficer"));

    private final FromJsonHelper fromApiJsonHelper;

    @Autowired
    public StaffCommandFromApiJsonDeserializer(final FromJsonHelper fromApiJsonHelper) {
        this.fromApiJsonHelper = fromApiJsonHelper;
    }

    public void validateForCreate(final String json) {
        if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }

        final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
        fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters);

        final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
        final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("staff");

        final JsonElement element = fromApiJsonHelper.parse(json);

        final String firstname = fromApiJsonHelper.extractStringNamed("firstname", element);
        baseDataValidator.reset().parameter("firstname").value(firstname).notBlank().notExceedingLengthOf(50);

        final String lastname = fromApiJsonHelper.extractStringNamed("lastname", element);
        baseDataValidator.reset().parameter("lastname").value(lastname).notBlank().notExceedingLengthOf(50);

        final Long officeId = fromApiJsonHelper.extractLongNamed("officeId", element);
        baseDataValidator.reset().parameter("officeId").value(officeId).notNull().integerGreaterThanZero();

        if (fromApiJsonHelper.parameterExists("isLoanOfficer", element)) {
            final Boolean loanOfficerFlag = fromApiJsonHelper.extractBooleanNamed("isLoanOfficer", element);
            baseDataValidator.reset().parameter("isLoanOfficer").value(loanOfficerFlag).notNull();
        }

        throwExceptionIfValidationWarningsExist(dataValidationErrors);
    }

    public void validateForUpdate(final String json) {
        if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }

        final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
        fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters);

        final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
        final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("staff");

        final JsonElement element = fromApiJsonHelper.parse(json);
        if (fromApiJsonHelper.parameterExists("firstname", element)) {
            final String firstname = fromApiJsonHelper.extractStringNamed("firstname", element);
            baseDataValidator.reset().parameter("firstname").value(firstname).notBlank().notExceedingLengthOf(50);
        }

        if (fromApiJsonHelper.parameterExists("lastname", element)) {
            final String lastname = fromApiJsonHelper.extractStringNamed("lastname", element);
            baseDataValidator.reset().parameter("lastname").value(lastname).notBlank().notExceedingLengthOf(50);
        }

        if (fromApiJsonHelper.parameterExists("officeId", element)) {
            final Long officeId = fromApiJsonHelper.extractLongNamed("officeId", element);
            baseDataValidator.reset().parameter("officeId").value(officeId).notNull().integerGreaterThanZero();
        }

        if (fromApiJsonHelper.parameterExists("isLoanOfficer", element)) {
            final Boolean loanOfficerFlag = fromApiJsonHelper.extractBooleanNamed("isLoanOfficer", element);
            baseDataValidator.reset().parameter("isLoanOfficer").value(loanOfficerFlag).notNull();
        }

        throwExceptionIfValidationWarningsExist(dataValidationErrors);
    }

    private void throwExceptionIfValidationWarningsExist(final List<ApiParameterError> dataValidationErrors) {
        if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                "Validation errors exist.", dataValidationErrors); }
    }
}
@@ -1940,24 +1940,228 @@
 rse(json);%0A%0A
+        final Long officeId = fromApiJsonHelper.extractLongNamed(%22officeId%22, element);%0A        baseDataValidator.reset().parameter(%22officeId%22).value(officeId).notNull().integerGreaterThanZero();%0A        %0A
         fina
@@ -2233,32 +2233,32 @@
 ame%22, element);%0A
-
         baseData
@@ -2558,204 +2558,8 @@
 );%0A%0A
-        final Long officeId = fromApiJsonHelper.extractLongNamed(%22officeId%22, element);%0A        baseDataValidator.reset().parameter(%22officeId%22).value(officeId).notNull().integerGreaterThanZero();%0A%0A
     
@@ -3447,33 +3447,41 @@
 ource(%22staff%22);%0A
+        
 %0A
-
         final Js
@@ -3531,16 +3531,308 @@
 (json);%0A
+        if (fromApiJsonHelper.parameterExists(%22officeId%22, element)) %7B%0A            final Long officeId = fromApiJsonHelper.extractLongNamed(%22officeId%22, element);%0A            baseDataValidator.reset().parameter(%22officeId%22).value(officeId).notNull().integerGreaterThanZero();%0A        %7D%0A        %0A
         
@@ -4274,32 +4274,32 @@
 ame%22, element);%0A
+
             base
@@ -4410,292 +4410,8 @@
  %7D%0A%0A
-        if (fromApiJsonHelper.parameterExists(%22officeId%22, element)) %7B%0A            final Long officeId = fromApiJsonHelper.extractLongNamed(%22officeId%22, element);%0A            baseDataValidator.reset().parameter(%22officeId%22).value(officeId).notNull().integerGreaterThanZero();%0A        %7D%0A%0A
     

tidy up validation order of staff.