/*
 * 
 */
package com.fp.bpmlib.query.inbox;

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

import org.kie.api.task.model.Task;

import com.fp.bpmlib.db.BPMProperties;
import com.fp.bpmlib.task.client.HumanTaskClient;
import com.fp.bpmlib.task.client.TaskUtil;
import com.fp.common.properties.PropertiesHandler;
import com.fp.dto.data.TransactionDTO;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;

/**
 * Class TransactionTasks encargada de consultar las Tareas por transacción.
 * 
 * @author gfiallos
 */
public class TransactionTasks extends QueryRule {

    /** serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /**
     * Process.
     * 
     * @param pQueryRequest the query request
     * @return query request
     * @throws Exception la exception
     */
    @Override
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        PropertiesHandler ph = BPMProperties.getInstance();
        HumanTaskClient htc = new HumanTaskClient();
        try {
            List<Object[]> t = htc.getUnfinishedTasksByUser(pQueryRequest.getUser());
            List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
            TransactionDTO trnP = new TransactionDTO(pQueryRequest.getTransactionModule(), pQueryRequest.getTransactionCode(),
                    pQueryRequest.getTransactionVersion());
            for (Object[] obj : t) {
                Task task = (Task) obj[0];
                TaskUtil tu = new TaskUtil(task, htc);
                TransactionDTO trn1 = tu.getTransaction();
                if (trn1.compareTo(trnP) != 0) {
                    continue;
                }
                Map<String, Object> rec = new HashMap<String, Object>();
                rec.put("tid", tu.getTid());
                rec.put("i",
                        ph.getStringValue(BPMProperties.INBOX_ICON_PATH) + tu.getStatus() + ph.getStringValue(BPMProperties.INBOX_ICON_EXTENSION));
                rec.put("c", "w" + task.getTaskData().getWorkItemId());
                rec.put("d", tu.getSubject());
                Map<String, Object> m = new HashMap<String, Object>();
                rec.put("desc", m);
                m.put("c", "t" + tu.getTid());
                m.put("d", null);
                data.add(rec);
            }
            pQueryRequest.getResponse().put("tasks", data);
            return pQueryRequest;
        } finally {
            htc.close();
        }
    }

    @SuppressWarnings("unchecked")
    public static void addTask(Map<String, Object> mtran, TaskUtil taskUtil, String language) throws Exception {
        PropertiesHandler ph = BPMProperties.getInstance();
        Map<String, Object> rec = new HashMap<String, Object>();
        rec.put("tid", taskUtil.getTid());
        rec.put("i", ph.getStringValue(BPMProperties.INBOX_ICON_PATH) + taskUtil.getStatus() + ph.getStringValue(BPMProperties.INBOX_ICON_EXTENSION));
        rec.put("c", "w" + taskUtil.getWorkItemId());
        rec.put("d", taskUtil.getSubject());
        rec.put("logsolicitude",taskUtil.getRequest().get("logsolicitude"));
        List<Map<String, Object>> ltasks = null;
        if (mtran.get("tasks") == null) {
            ltasks = new ArrayList<>();
            mtran.put("tasks", ltasks);
        } else {
            ltasks = (List<Map<String, Object>>) mtran.get("tasks");
        }
        TaskDetail.addTaskDetail(rec, taskUtil, language);
        ltasks.add(rec);
    }
    
    
    
}