package sikd.sikd5.transaksi.upload;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import sikd.sikd5.sikd5bean.util.PropertiesLoader;
import sikd.sikd5.sikd5bean.ws.transaksi.Headerapbd_WS;
import sikd.sikd5.sikd5bean.ws.transaksi.Headerdth_WS;
import sikd.sikd5.sikd5bean.ws.transaksi.Headerjurnal_WS;
import sikd.sikd5.sikd5bean.ws.transaksi.Headerlog_WS;
import sikd.sikd5.sikd5ejb.session.TransaksiSessionBeanRemote;

/**
 *
 * @author soraturaes
 */
@ManagedBean(name = "uploadDthZipMBean")
@ViewScoped
public class UploadDthZipMBean implements Serializable {

    private List<String> stats;
    boolean handleFileUpload;
    boolean getObjekFromXM;
    boolean insertData;
    boolean uploadDisable;
    UploadedFile uploadedFile;
    String fileName;
    String uploadPath;
    String keterangan = "";
    String trueFileName = "";

    @EJB
    TransaksiSessionBeanRemote transaksiSession;

    @PostConstruct
    public void init() {
        try {
            stats = new ArrayList();
            uploadPath = ((Properties) PropertiesLoader.loadProperties("sikd.properties")).getProperty("uploadPath");
            uploadDisable = false;
        } catch (Exception ex) {
            Logger.getLogger(UploadDthZipMBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @SuppressWarnings("ConvertToTryWithResources")
    public void handleFileUpload(FileUploadEvent e) {
        System.out.println("handleFileUpload....................");
        fileName = "";
        handleFileUpload = false;
        getObjekFromXM = false;
        insertData = false;
        stats.clear();
        stats.add("Mulai Upload data  ");
//        if (getPemdaFilterBean().getSelectedData() != null && getPemdaFilterBean().getSelectedData().getId() > 0) {
        uploadedFile = e.getFile();
        stats.add("Upload data berhasil");
        handleFileUpload = true;
        getObjectFromXML();
        handleFileUpload = true;
//        } else {
//            handleFileUpload = false;
//            stats.add("gagal upload File : Silahkan pilih Pemerintah Daerah");
//        }
    }

    public void handleFileUploadChange() {
        stats.clear();
    }

    public boolean isUploadDisable() {
        return uploadDisable;
    }

    public void setUploadDisable(boolean uploadDisable) {
        this.uploadDisable = uploadDisable;
    }

    public void getObjectFromXML() {
        System.out.println("getObjectFromXML.......................");
        System.out.println("handleFileUpload "+handleFileUpload);
        
        if (handleFileUpload) {
            if (uploadedFile != null) {
                try {
                    byte[] contents = uploadedFile.getContents();
                    uploadData(contents);
                    getObjekFromXM = true;
                } catch (Exception ex) {
                    stats.add("gagal parsing: " + ex.getMessage());
                    getObjekFromXM = false;
                    Logger.getLogger(UploadDthZipMBean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private void uploadData(byte[] contents) throws Exception {
//        String kodeikd = ikdFilterBean.getSelectedData().getKode();
        String[] filenya = UnzipFileUtil.simpanExtract(uploadPath, contents);
        File fxml = new File(filenya[1]);
        fileName = fxml.getName();
        if (fxml.exists()) {
            Object obj = XmlServiceUtil.parsingXML(filenya[1]);
            if (obj != null) {
                if (obj instanceof Headerlog_WS) {
                    Headerlog_WS hws = (Headerlog_WS) obj;
                    System.out.println("Kadieu");
                    transaksiSession.inputDth(hws);
                    
                    System.out.println("satker "+hws.getKodesatker());
                    stats.add("Berhasil input Dth");
                }
            }
        } else {
            stats.add("berkas xml gagal diparsing");
        }
    }

    public List<String> getStats() {
        return stats;
    }

    public void setStats(List<String> stats) {
        this.stats = stats;
    }

    public boolean isInsertData() {
        return insertData;
    }

    public void setInsertData(boolean insertData) {
        this.insertData = insertData;
    }
    
    

}
