/*******************************************************************************
 *  Imixs Workflow 
 *  Copyright (C) 2001, 2011 Imixs Software Solutions GmbH,  
 *  http://www.imixs.com
 *  
 *  This program is free software; you can redistribute it and/or 
 *  modify it under the terms of the GNU General Public License 
 *  as published by the Free Software Foundation; either version 2 
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 *  General Public License for more details.
 *  
 *  You can receive a copy of the GNU General Public
 *  License at http://www.gnu.org/licenses/gpl.html
 *  
 *  Project: 
 *  	http://www.imixs.org
 *  	http://java.net/projects/imixs-workflow
 *  
 *  Contributors:  
 *  	Imixs Software Solutions GmbH - initial API and implementation
 *  	Ralph Soika - Software Developer
 *******************************************************************************/

package com.alexanderlogistics;

import java.util.Collection;
import java.util.Date;
import java.util.logging.Logger;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.imixs.archive.core.SnapshotService;
import org.imixs.workflow.FileData;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.exceptions.QueryException;

import jakarta.annotation.security.RunAs;
import jakarta.ejb.Singleton;
import jakarta.inject.Inject;

/**
 * Der OPListExportService ist im Grunde nur dafür da das overdue date zu
 * ermitteln.
 * Ausgangsrechnungen zu akutalisieren.
 * 
 * @author rsoika
 * 
 */
@Singleton
@RunAs("org.imixs.ACCESSLEVEL.MANAGERACCESS")
public class OPListExportService {

    private static Logger logger = Logger.getLogger(OPListExportService.class.getName());

    public static final int MAX_COUNT = 999;
    final String TYPE_TEXTBLOCK = "textblock";
    public static final String ERROR_CONFIG = "CONFIG_ERROR";
    public static final String OPLIST_ERROR = "OPLIST_ERROR";
    public static final int TASK_MAHNUNG_2ND = 5210;
    public static final int TASK_MAHNUNG_LAST = 5220;
    public static final String ENV_OPLIST_OVERDUE = "oplist.overdue";

    @Inject
    @ConfigProperty(name = ENV_OPLIST_OVERDUE, defaultValue = "10")
    long overdueDays;

    @Inject
    SnapshotService snapshotService;

    @Inject
    DocumentService documentService;

    /**
     * Prüft ob der Vorgang überflällig ist.
     * Default ist 21 tage.
     */
    public boolean isOverdue(Date date, int taskid) {
        Date now = new Date();
        long diffInMillies = now.getTime() - date.getTime();
        int tage = (int) (diffInMillies / 1000 / 60 / 60 / 24);
        // logger.info("...taskid=" + taskid + " date=" + date + " tage=" + tage);
        return ((taskid >= TASK_MAHNUNG_2ND && taskid <= TASK_MAHNUNG_LAST) || tage >= overdueDays);

    }

    /**
     * This method returns a text-block ItemCollection for a specified name.
     * 
     * @param name in attribute txtname
     * 
     *
     */
    public FileData loadTextBlockFileData(String name, String fileName) {
        ItemCollection textBlockItemCollection = null;

        // load text-block by name....
        String sQuery = "(type:\"" + TYPE_TEXTBLOCK + "\" AND txtname:\"" + name + "\")";
        Collection<ItemCollection> col;
        try {
            // find the textblock...
            col = documentService.find(sQuery, 1, 0);
            if (col.size() > 0) {
                textBlockItemCollection = col.iterator().next();
                // fetch the fileData...
                return snapshotService.getWorkItemFile(textBlockItemCollection.getUniqueID(), fileName);

            } else {
                logger.warning("Missing text-block : '" + name + "'");
            }
        } catch (QueryException e) {
            logger.warning("getTextBlock - invalid query: " + e.getMessage());
        }

        return null;
    }

}
