package com.fp.bpm.helper;

import com.fp.dto.query.QueryRequest;
import com.fp.dto.save.SaveRequest;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.commondb.helper.FormatDates;
import com.fp.persistence.pgeneral.safe.TsafeProfileHorary;
import com.fp.persistence.pgeneral.safe.TsafeProfileHoraryKey;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Clase que se encarga de verificar si una transaccion se debe ejecutar en una hora determinada
 * @author scastillo
 */
public class TransactionTime {

    private static SimpleDateFormat simpleDateFormatCreateTime = new SimpleDateFormat("HHmm");

    /**
     * Metodo que se encarga de verificar si la transaccion a procesar se encuentra dentro de un horario permitido
     * @param saveRequest Objeto SaveRequest
     * @param queryRequest Objeto QueryRequest
     * @return
     * @throws Exception
     */
    public static boolean verifyTransactionTime(SaveRequest saveRequest, QueryRequest queryRequest) throws Exception {
        boolean transactionEnabled = true;
        if (queryRequest != null && queryRequest.get("cancelVerification") != null && ((Boolean) queryRequest.get("cancelVerification")).booleanValue()) {
            return transactionEnabled;
        }
        String profile = null;
        String module = null;
        Integer transactioncode = null;
        Integer transactionversion = null;
        if (saveRequest != null) {
            profile = saveRequest.getProfile();
            module = saveRequest.getTransactionModule();
            transactioncode = saveRequest.getTransactionCode();
            transactionversion = saveRequest.getTransactionVersion();
        } else if (queryRequest != null) {
            profile = queryRequest.getProfile();
            module = queryRequest.getTransactionModule();
            transactioncode = queryRequest.getTransactionCode();
            transactionversion = queryRequest.getTransactionVersion();
            if (profile == null || module == null || transactioncode == null || transactionversion == null) {
                return transactionEnabled;
            }
        }
        Timestamp date = FormatDates.getInstance().getDataBaseTimestamp();
        Calendar calc = Calendar.getInstance();
        calc.setTime(date);
        Integer day = calc.get(Calendar.DAY_OF_WEEK);
        Integer hour = new Integer(TransactionTime.simpleDateFormatCreateTime.format(date));
        TsafeProfileHoraryKey tsafeProfileHoraryKey = new TsafeProfileHoraryKey(profile,
                module, transactioncode, transactionversion, day);
        TsafeProfileHorary tsafeProfileHorary = TsafeProfileHorary.find(PersistenceHelper.getEntityManager(),
                tsafeProfileHoraryKey);
        if (tsafeProfileHorary != null) {
            Integer initTime = new Integer(tsafeProfileHorary.getInittime().replaceAll(":", ""));
            Integer finishTime = new Integer(tsafeProfileHorary.getFinishtime().replaceAll(":", ""));
            if (hour < initTime || hour > finishTime) {
                transactionEnabled = false;
            }
            if (transactionEnabled && tsafeProfileHorary.getValidateamount() != null
                    && tsafeProfileHorary.getValidateamount().compareTo("Y") == 0) {
                if (saveRequest != null) {
                    saveRequest.put("validateamount", "Y");
                } else if (queryRequest != null) {
                    queryRequest.put("validateamount", "Y");
                }
            }
        }
        return transactionEnabled;
    }
}
