package com.capgemini.reports.definition.r28report;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.reports.definition.r28report.ExecutionR28.ReportDataR28;
import com.capgemini.reports.execution.mapconstructor.AbstractStaticTemplateMapConstructor;
import com.capgemini.reports.execution.mapconstructor.IMapConstructor;
import com.capgemini.reports.printing.PrintParameters;
import com.teamcenter.soa.client.model.strong.Item;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.capgemini.framework.teamcenter.object.SnecmaObjectProperties.PV4_TYPE_EFFECTIVITY;
import static com.capgemini.framework.teamcenter.object.SnecmaObjectProperties.PV4_TYPE_MODIFICATION;

/**
 * Extracts the relevant data obtained from TC into the expected List of Maps for Jasper.
 */
@Slf4j
public class R28Printer extends AbstractStaticTemplateMapConstructor<ExecutionR28, ExecutionR28.ReportDataR28> implements IMapConstructor<ExecutionR28, ExecutionR28.ReportDataR28> {

    private static final long serialVersionUID = 4281716591716247163L;
    private static final Logger LOGGER = LoggerFactory.getLogger(TcSession.class);

    /**
     * Puts the data gathered by an {@link ExecutionR28} into some {@link PrintParameters} for printing.
     *
     * @param reportData the data-bearing {@link ReportDataR28}
     * @param mainReport the {@link PrintParameters}
     */
    private void print(final ReportDataR28 reportData, final PrintParameters mainReport, final TcSession session) {
        List<Item> parts = reportData.getParts();
        if (parts.isEmpty()) {
            mainReport.addReportData(setupEmptyData());
        }
        else {

            List<Map<R28Keys, ?>> reportDataMap = new ArrayList<>();
            for (Item part : parts) {
                    Map<R28Keys, String> map = new EnumMap<>(R28Keys.class);
                    map.put(R28Keys.ITEM_ID, part.get_item_id());
                    map.put(R28Keys.ITEM_NAME, part.get_object_name());
                    // if (part.get_IMAN_master_form()[0].)
                if (part.get_IMAN_master_form().length != 0){
                    session.loadProperties(part.get_IMAN_master_form()[0], PV4_TYPE_EFFECTIVITY);
                    // LOGGER.trace("Property is ", part.get_IMAN_master_form()[0].getPropertyObject(PV4_TYPE_EFFECTIVITY).getDisplayableValue());
                    if (part.get_IMAN_master_form()[0].getPropertyObject(PV4_TYPE_EFFECTIVITY).getDisplayableValue() == "") {
                        LOGGER.trace("Object ID " + part.get_item_id() + " does not have any effectivity type");
                        map.put(R28Keys.ITEM_EFFECTIVITY, "");
                    } else {
                        map.put(R28Keys.ITEM_EFFECTIVITY, part.get_IMAN_master_form()[0].getPropertyObject(PV4_TYPE_EFFECTIVITY).getDisplayableValue());
                    }
                } else {
                    LOGGER.trace("Object ID " + part.get_item_id() + " does not have any form");
                    map.put(R28Keys.ITEM_EFFECTIVITY, "");
                }
                    reportDataMap.add(map);
            }
            mainReport.addReportDataFromEnum(reportDataMap);
        }
    }

    private static List<Map<String, ?>> setupEmptyData() {
        List<Map<String, ?>> list = new ArrayList<>();
        list.add(new HashMap<String, Object>());
        return list;
    }

    @Override
    public void constructDataMaps(TcSession session, PrintParameters printParameters, ReportDataR28 reportData) {
        print(reportData, printParameters, session);
    }
}
