package com.capgemini.reports.r19;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.object.types.SnecmaObjectTypes;
import com.capgemini.framework.teamcenter.policies.PolicyLocation;
import com.capgemini.framework.teamcenter.resolution.filtering.ITcTypeFilter;
import com.capgemini.framework.teamcenter.resolution.filtering.TcTypeInclusionFilter;
import com.capgemini.reports.common.ServletMessageHelper;
import com.capgemini.reports.common.obtaindata.AbstractObtainDataFromModelObject;
import com.capgemini.reports.exceptions.InvalidUserInputException;
import com.capgemini.reports.exceptions.UserInputErrorEnum;
import com.teamcenter.soa.client.model.ModelObject;
import com.teamcenter.soa.client.model.strong.Item;
import com.teamcenter.soa.client.model.strong.ItemRevision;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import javax.servlet.annotation.WebServlet;

import static com.capgemini.framework.teamcenter.TeamcenterObjectProperties.ITEM_ID;
import static com.capgemini.framework.teamcenter.TeamcenterObjectProperties.ITEM_REVISION_ID;

/**
 * This class receives demands form ajax client to initialize values id and revision for PartPV.
 */
@Slf4j
@WebServlet("/report_r19/obtainDataR19")
public class ObtainDataServletR19 extends AbstractObtainDataFromModelObject {

    private static final long serialVersionUID = -4391142243290210007L;

    private static final String REMOVE_REVISION = " Revision";
    private static final String WARNING_MESSAGE =
            "Ce rapport listera les cas d&#39;utilisation du master et non de la r&eacute;vision s&eacute;lectionn&eacute;e";
    private static final String SUCCES_MESSAGE = "Donn&eacute;es obtenues avec succ&egrave;s pour le rapport R19";

    private static final ITcTypeFilter AUTHORIZED_DOCUMENTS = new TcTypeInclusionFilter(SnecmaObjectTypes.DOC_NORM,
            SnecmaObjectTypes.DOC_NORM_STANDARD, SnecmaObjectTypes.DOC_DRAWING_REVISION, SnecmaObjectTypes.DOC_TECHNIC_REVISION,
            SnecmaObjectTypes.DOC_NORM_REVISION, SnecmaObjectTypes.DOC_NORM_STANDARD_REVISION);

    @Override
    protected JSONObject obtainDataFromModelObject(final ModelObject modelObject, final TcSession session) throws InvalidUserInputException {
        log.info("Server received a request to obtain data for R19 report.");

        JSONObject json;
        String revisionId = "";
        String uid;
        String valueJsonWarning = "";
        // findAll type
        String objectType = modelObject.getTypeObject().getName();

        if (AUTHORIZED_DOCUMENTS.isIncluded(objectType)) {
            // start json
            json = ServletMessageHelper.successMessage(SUCCES_MESSAGE);
            // if doc drawing rev or doc technic revision revision
            String itemId = modelObject.getPropertyObject(ITEM_ID).getStringValue();

            // if object type is a revision
            if (isDocDrawingOrTechnicRevision(objectType)) {
                revisionId = modelObject.getPropertyObject(ITEM_REVISION_ID).getStringValue();
            }

            // if object type is a DocNorm Revision or a DocNrmStandard Revision -> findAll uid of Master
            if (isDocNormRevOrDocNormStandardRev(objectType)) {
                ItemRevision tempRev = (ItemRevision) modelObject;
                session.loadProperties(tempRev, "items_tag");
                Item item = tempRev.get_items_tag();
                uid = item.getUid();
                objectType = objectType.replace(REMOVE_REVISION, "");
                valueJsonWarning = WARNING_MESSAGE;
            } else {
                uid = getUid();
            }

            // send datas
            json.put("ObjectId", itemId);
            json.put("ObjectUid", uid);
            json.put("ObjectRev", revisionId);
            json.put("ObjectType", objectType);
            json.put("warning", valueJsonWarning);
        } else {
            throw new InvalidUserInputException(UserInputErrorEnum.TYPE_DOCUMENT_SELECTED_NOT_AUTHORIZED);
        }

        return json;
    }


    @Override
    protected PolicyLocation obtainLocation() {
        return PolicyLocation.make("R19_policy_light", "/xml/policies/r19report/R19_policy_light.xml");
    }

    private boolean isDocNormRevOrDocNormStandardRev(final String objectType) {
        return objectType.equals(SnecmaObjectTypes.DOC_NORM_REVISION) || objectType.equals(SnecmaObjectTypes.DOC_NORM_STANDARD_REVISION);
    }

    private boolean isDocDrawingOrTechnicRevision(final String objectType) {
        return objectType.equals(SnecmaObjectTypes.DOC_DRAWING_REVISION) || objectType.equals(SnecmaObjectTypes.DOC_TECHNIC_REVISION);
    }

}
