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

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

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

import com.fp.base.persistence.util.helper.GeneralDescriptions;
import com.fp.bpmlib.ProcessUtil;
import com.fp.bpmlib.task.client.HumanTaskClient;
import com.fp.bpmlib.task.client.TaskUtil;
import com.fp.dto.Response;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;
import com.fp.persistence.commondb.helper.FormatDates;
import com.fp.simple.dto.TaskInfo;

// TODO: Auto-generated Javadoc
/**
 * Class TaskPending encargada de.
 * 
 * @author gfiallos
 */
public class TaskPending extends QueryRule {

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

    /** El valor de data. */
    private List<Map<String, Object>> data;

    /** El valor de htc. */
    private HumanTaskClient htc;

    // private Process process;

    /** El valor de request. */
    private QueryRequest request;

    // private static final Logger LOG = Logger.getLogger(TaskPending.class);

    /**
     * Manage task.
     * 
     * @param pWork the work
     * @return list
     * @throws Exception la exception
     */
    protected List<Map<String, Object>> manageTask(Long pWork) throws Exception {
        Task t = this.htc.getTaskByWorkItemId(pWork);
        TaskData td = t.getTaskData();
        TaskUtil tu = new TaskUtil(t.getId());
        TaskInfo ti = tu.getTaskInfo();
        ti.setCreated(new Timestamp(td.getCreatedOn().getTime()));
        this.data = new ArrayList<Map<String, Object>>();
        String user = ti.getUserId();
        try {
            user = td.getActualOwner().getId();
        } catch (Exception e) {
            user = ti.getUserId();
        }
        this.data.add(ProcessUtil.getInstance().prepareRecord("userAsign", user, this.request));
        this.data.add(ProcessUtil.getInstance().prepareRecord("name", GeneralDescriptions.getOfficername(user), this.request));
        this.data.add(ProcessUtil.getInstance().prepareRecord("groupAsign", ti.getGroupId(), this.request));
        this.data.add(ProcessUtil.getInstance().prepareRecord("taskStatus",
                GeneralDescriptions.getLabel(td.getStatus().name(), this.request.getLanguage(), this.request.getChannelCode()), this.request));
        this.data.add(ProcessUtil.getInstance().prepareRecord("workItem", td.getWorkItemId(), this.request));
        this.data.add(ProcessUtil.getInstance().prepareRecord("taskId", t.getId(), this.request));
        this.data.add(ProcessUtil.getInstance().prepareRecord("create", new Timestamp(td.getCreatedOn().getTime()), this.request));
        this.data.add(ProcessUtil.getInstance().prepareRecord("realTime",
                ti.getRealTimeString(FormatDates.getInstance().getDataBaseTimestamp().getTime()), this.request));
        this.data.add(ProcessUtil.getInstance().prepareRecord("expectedTime", ti.getExpectedTimeString(), this.request));
        this.data.add(ProcessUtil.getInstance().prepareRecord(
                "transaction",
                ti.getModule() + "-" + ti.getTransaction() + "-" + ti.getVersion() + " "
                        + GeneralDescriptions.getTransactiondesc(ti.getModule(), ti.getTransaction(), ti.getVersion()), this.request));
        List<Comment> lc = tu.getComments();
        List<Map<String, Object>> comments = new ArrayList<Map<String, Object>>();
        for (Comment comment : lc) {
            Map<String, Object> record = new HashMap<String, Object>();
            record.put("user", comment.getAddedBy().getId());
            record.put("name", GeneralDescriptions.getUsername(comment.getAddedBy().getId()));
            record.put("date", new Timestamp(comment.getAddedAt().getTime()));
            record.put("comment", comment.getText());
            comments.add(record);
        }
        return comments;
    }

    /**
     * Process.
     * 
     * @param pQueryRequest the query request
     * @return query request
     * @throws Exception la exception
     */
    @Override
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        this.htc = new HumanTaskClient();
        try {
            this.request = pQueryRequest;
            // Long pid = pQueryRequest.getLong("PID");
            // Long tid = pQueryRequest.getLong("TID");
            Long workId = pQueryRequest.getLong("WORKID");

            Response response = this.request.getResponse();
            response.put("comments", this.manageTask(workId));
            response.put("data", this.data);
        } finally {
            this.htc.close();
        }
        return pQueryRequest;
    }

}
