package com.fp.person.rules.save;

import java.util.List;

import javax.persistence.EntityManager;

import com.fp.dto.helper.FileHelper;
import com.fp.dto.rules.TransactionRule;
import com.fp.dto.save.SaveRequest;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.commondb.helper.FormatDates;
import com.fp.persistence.pgeneral.image.TgeneFiles;
import com.fp.persistence.pgeneral.image.TgeneFilesDetail;
import com.fp.persistence.pgeneral.image.TgeneFilesDetailKey;

/**
 * Clase que se encarga de guardar los datos de la situaciòn financiera de
 * personas y subir un archive a la DB
 *
 * @author BPTWPA
 */
public class PersonBalanceFile extends TransactionRule {

    /**
     * Mètodo que recibe datos para registrar la informacion en la DB
     *
     * @param pSaveRequest Recibe un objeto SaveRequest
     * @return pSaveRequest Retorna un objeto SaveRequest
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @Override
    public SaveRequest normalProcess(SaveRequest pSaveRequest) throws Exception {
        String balancefile = (String) pSaveRequest.get("balancefile");
        List<FileHelper> fileItem = (List<FileHelper>) pSaveRequest.get("file");
        if (fileItem == null || balancefile == null || fileItem.isEmpty()) {
            return pSaveRequest;
        }
        if (balancefile != null && balancefile.compareTo("excelfile") == 0) {
            Integer filecode = PersonImage.getFileCode();
            TgeneFiles tgeneFiles = TgeneFiles.find(PersistenceHelper.getEntityManager(), filecode);
            if (tgeneFiles == null) {
                tgeneFiles = new TgeneFiles(filecode);
                pSaveRequest.putSaveBean("TGENEFILES", tgeneFiles, 1);
            }
            TgeneFilesDetailKey tgeneFilesDetailKey = new TgeneFilesDetailKey(filecode, FormatDates.getDefaultExpiryTimestamp());
            TgeneFilesDetail tgeneFilesDetail = TgeneFilesDetail.find(PersistenceHelper.getEntityManager(), tgeneFilesDetailKey);
            if (tgeneFilesDetail == null) {
                tgeneFilesDetail = new TgeneFilesDetail();
                tgeneFilesDetail.setPk(tgeneFilesDetailKey);
                tgeneFilesDetail.setImage(fileItem.get(0).getImage());
                tgeneFilesDetail.setCapturedate(FormatDates.getInstance().getDataBaseDate());
                tgeneFilesDetail.setImagesize(Integer.parseInt(String.valueOf(fileItem.get(0).getSize())));
                tgeneFilesDetail.setIngressuser(pSaveRequest.getUser());
                tgeneFilesDetail.setExtension(fileItem.get(0).getExtension());
                tgeneFilesDetail.setContenttype(fileItem.get(0).getContentType());
                tgeneFilesDetail.setName(fileItem.get(0).getName());
            } else {
                tgeneFilesDetail.setImage(fileItem.get(0).getImage());
                tgeneFilesDetail.setCapturedate(FormatDates.getInstance().getDataBaseDate());
                tgeneFilesDetail.setImagesize(Integer.parseInt(String.valueOf(fileItem.get(0).getSize())));
                tgeneFilesDetail.setDatefrom(FormatDates.getInstance().getDataBaseTimestamp());
                tgeneFilesDetail.setModifyuser(pSaveRequest.getUser());
                tgeneFilesDetail.setContenttype(fileItem.get(0).getContentType());
                tgeneFilesDetail.setName(fileItem.get(0).getName());
                EntityManager em = PersistenceHelper.getEntityManager();
                em.detach(tgeneFilesDetail);
            }
            pSaveRequest.putSaveBean("TGENEFILESDETAIL", tgeneFilesDetail, 2);
            pSaveRequest.getResponse().put("codes", "[" + filecode + "]");
        }
        return pSaveRequest;
    }

    @Override
    public SaveRequest reverseProcess(SaveRequest pSaveRequest) throws Exception {
        return pSaveRequest;
    }
}
