/* enoECMChangeReportUX
 *
 ** Copyright (c) 1993-2020 Dassault Systemes. All Rights Reserved.
 ** This program contains proprietary and trade secret information of
 ** Dassault Systemes.
 ** Copyright notice is precautionary only and does not evidence any actual
 ** or intended publication of such program
 *
 */

import com.dassault_systemes.enovia.changeorder.factory.ChangeOrderFactory;
import com.dassault_systemes.enovia.changeorder.interfaces.IChangeOrderServices;
import com.dassault_systemes.enovia.changerequest.factory.ChangeRequestFactory;
import com.dassault_systemes.enovia.changerequest.interfaces.IChangeRequest;
import com.dassault_systemes.enovia.changerequest.interfaces.IChangeRequestServices;
import com.matrixone.apps.domain.DomainObject;
import com.matrixone.apps.domain.util.*;
import com.matrixone.apps.framework.ui.UINavigatorUtil;
import com.matrixone.apps.framework.ui.UIUtil;
import matrix.db.BusinessObjectWithSelect;
import matrix.db.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

public class enoECMChangeReportUX_mxJPO extends enoECMChangeReportUXBase_mxJPO {

    /**
     * Constructor.
     * @param context the eMatrix <code>Context</code> object.
     * @param args holds no arguments.
     * @throws Exception if the operation fails.
     * @since ECM R421.HF4
     */

    private static final Logger LOGGER = Logger.getLogger(enoECMChangeReportUXBase_mxJPO.class.getName());

    public enoECMChangeReportUX_mxJPO (Context context, String[] args) throws Exception {
        super(context, args);
    }

    /**
     * Get change actions of Change Request for Summary Report
     *
     * @param context the eMatrix <code>Context</code> object.
     * @param args holds objectId.
     * @return String.
     * @throws Exception If the operation fails.
     */
    public String getChangeActionsOfChangeRequest(Context context, String[] args) throws Exception {
        StringBuffer sbChangeActions = new StringBuffer();
        try {
            String strLanguage = context.getSession().getLanguage();
            String strChangeRequestId = args[0];

            DomainObject domChange = DomainObject.newInstance(context, strChangeRequestId);

            IChangeOrderServices changeOrderFactory = ChangeOrderFactory.createChangeOrderFactory();

            IChangeRequestServices changeRequestFactory = ChangeRequestFactory.createChangeRequestFactory();
            IChangeRequest iChangeRequest = changeRequestFactory.retrieveChangeRequestFromDatabase(context, domChange);

            Set<String> caPhysicalIdSet = iChangeRequest.getIndirectImplementingChangeActionIdentifiers(context);
            List<String> caIdList = iChangeRequest.getImplementingChangeActionIdentifiers(context);

            LOGGER.info("caPhysicalIdSet == >" + caPhysicalIdSet);
            LOGGER.info("caIdList == >" + caIdList);

            List<String> lsChangeActionId = new ArrayList<>();

            if(!caIdList.isEmpty()){
                for (String caId : caIdList){
                    lsChangeActionId.add(caId);
                }
            }

            if(!caPhysicalIdSet.isEmpty()){
                for(String caPhysicalId : caPhysicalIdSet){
                    String getCaId = "temp query bus * * * where 'physicalId == " + caPhysicalId + "' select id dump | ";
                    String caIdInfo = MqlUtil.mqlCommand(context, getCaId);
                    String caId = caIdInfo.split("\\|")[3];

                    if(lsChangeActionId.isEmpty() || !lsChangeActionId.contains(caId)){
                        lsChangeActionId.add(caId);
                    }
                }
            }

            LOGGER.info("lsChangeActionId == >" + lsChangeActionId);

            List<IChangeOrderServices.ChangeIdCardENUM> changeIdCardSelectables = new ArrayList<IChangeOrderServices.ChangeIdCardENUM>();
            changeIdCardSelectables.add(IChangeOrderServices.ChangeIdCardENUM.NAMESELECTABLE);
            changeIdCardSelectables.add(IChangeOrderServices.ChangeIdCardENUM.TYPESELECTABLE);
            changeIdCardSelectables.add(IChangeOrderServices.ChangeIdCardENUM.POLICYSELECTABLE);
            changeIdCardSelectables.add(IChangeOrderServices.ChangeIdCardENUM.OWNERSELECTABLE);
            changeIdCardSelectables.add(IChangeOrderServices.ChangeIdCardENUM.CURRENTSELECTABLE);
            changeIdCardSelectables.add(IChangeOrderServices.ChangeIdCardENUM.CATITLESELECTABLE);

            Map<String, BusinessObjectWithSelect> mpChangeInfo = changeOrderFactory.getChangeIdCard(context, lsChangeActionId, changeIdCardSelectables);

            sbChangeActions.append("<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"0\" >");
            sbChangeActions.append("<tr>");
            sbChangeActions.append("<th>" + EnoviaResourceBundle.getProperty(context, "emxEnterpriseChangeMgtStringResource", context.getLocale(), "EnterpriseChangeMgt.Label.NAME") + "</th>");
            sbChangeActions.append("<th>" + EnoviaResourceBundle.getProperty(context, "emxEnterpriseChangeMgtStringResource", context.getLocale(), "EnterpriseChangeMgt.Label.Title") + "</th>");
            sbChangeActions.append("<th>" + EnoviaResourceBundle.getProperty(context, "emxEnterpriseChangeMgtStringResource", context.getLocale(), "EnterpriseChangeMgt.Label.OWNER") + "</th>");
            sbChangeActions.append("<th>" + EnoviaResourceBundle.getProperty(context, "emxEnterpriseChangeMgtStringResource", context.getLocale(), "EnterpriseChangeMgt.Common.State") + "</th>");
            sbChangeActions.append("</tr>");

            Set<String> setMpChangeKeys = mpChangeInfo.keySet();
            for(String key : setMpChangeKeys) {
                BusinessObjectWithSelect busSelectData = (BusinessObjectWithSelect) mpChangeInfo.get(key);
                String strType = busSelectData.getSelectData(IChangeOrderServices.ChangeIdCardENUM.TYPESELECTABLE.toString());
                String strName = busSelectData.getSelectData(IChangeOrderServices.ChangeIdCardENUM.NAMESELECTABLE.toString());
                String strPolicy = busSelectData.getSelectData(IChangeOrderServices.ChangeIdCardENUM.POLICYSELECTABLE.toString());
                String strCurrent = busSelectData.getSelectData(IChangeOrderServices.ChangeIdCardENUM.CURRENTSELECTABLE.toString());
                String strTitle = busSelectData.getSelectData(IChangeOrderServices.ChangeIdCardENUM.CATITLESELECTABLE.toString());
                String strOwner= busSelectData.getSelectData(IChangeOrderServices.ChangeIdCardENUM.OWNERSELECTABLE.toString());
                String strOwnerFullName = PersonUtil.getFullName(context, strOwner);

                String imgChangeActionType = UINavigatorUtil.getTypeIconProperty(context, strType);
                if (UIUtil.isNullOrEmpty(imgChangeActionType)) {
                    imgChangeActionType = "iconSmallDefault.gif";
                }

                sbChangeActions.append("<tr>");
                sbChangeActions.append("<td><img src=\"../common/images/" + imgChangeActionType + "\" border=\"0\" alt=\"*\">&nbsp;" + XSSUtil.encodeForHTML(context, strName) + "&nbsp;</td>");
                sbChangeActions.append("<td>" + XSSUtil.encodeForHTML(context, strTitle) + "&nbsp;</td>");
                sbChangeActions.append("<td>" + XSSUtil.encodeForHTML(context, strOwnerFullName) + "&nbsp;</td>");
                sbChangeActions.append("<td>" + i18nNow.getStateI18NString(strPolicy, strCurrent, strLanguage) + "&nbsp;</td>");
                sbChangeActions.append("</tr>");
            }
            if (lsChangeActionId.size() == 0) {
                sbChangeActions.append("<tr><td colspan=\"5\">" + EnoviaResourceBundle.getProperty(context, "emxEnterpriseChangeMgtStringResource", context.getLocale(), "EnterpriseChangeMgt.Change.NoChangeActionConnected") + "</td></tr>");
            }
        } catch (FrameworkException Ex) {
            throw Ex;
        }
        sbChangeActions.append("</table>");
        return sbChangeActions.toString();
    }
}
