/*
 * 
 */
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 java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.kie.api.task.model.Comment;
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.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 TaskDetail encargada de.
 * 
 * @author gfiallos
 */
public class TaskDetail extends QueryRule {

    /** Constante LOG. */
    private static final Logger LOG = Logger.getLogger(TaskDetail.class);

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

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

    // private Process process;

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

    /** El valor de response. */
    private Response response;

    /**
     * Manage task.
     * 
     * @param tid the tid
     * @return list
     * @throws Exception la exception
     */
    protected List<Map<String, Object>> manageTask(Long tid) throws Exception {
        TaskUtil t = new TaskUtil(tid);
        try {
            TaskData td = t.getTaskData();

            TaskInfo ti = t.getTaskInfo();
            this.data.add(ProcessUtil.getInstance().prepareRecord("create", new Timestamp(td.getCreatedOn().getTime()), 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));
            Map<String, Object> task = ProcessUtil.getInstance().prepareRecord("taskId", tid, this.request);

            this.data.add(task);
            this.data.add(ProcessUtil.getInstance().prepareRecord("response", ti.getResponse(), 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));
            Map<Object, Object> add = ti.getAditionalData();
            for (Entry<Object, Object> addE : add.entrySet()) {
                this.data.add(ProcessUtil.getInstance().prepareRecord("" + addE.getKey(), addE.getValue(), this.request));
            }
            List<Comment> lc = t.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;
        } finally {
            t.close();
        }
    }

    /**
     * Process.
     * 
     * @param pQueryRequest the query request
     * @return query request
     * @throws Exception la exception
     */
    @Override
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {

        this.request = pQueryRequest;
        // Long pid = pQueryRequest.getLong("PID");
        Long tid = pQueryRequest.getLong("TID");
        String journalId = pQueryRequest.getString("JID");
        TaskDetail.LOG.debug("TID:" + tid);
        TaskDetail.LOG.debug("journalId:" + journalId);
        this.data = ProcessUtil.getInstance().transactionData(journalId, this.request);

        this.response = this.request.getResponse();
        this.response.put("data", this.data);
        this.response.put("comments", this.manageTask(tid));

        return pQueryRequest;
    }

}
