/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sikd.ikonsol.ctrl;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import sikd.ikonsol.ejb.session.AkunEliminasiEjbHome;
import sikd.ikonsol.ejb.session.JenisBasEjbHome;
import sikd.ikonsol.ejb.session.PeriodeEjbHome;
import sikd.ikonsol.ejb.session.RefPemdaEjbHome;
import sikd.ikonsol.ejb.session.WorksheetViewEjbHome;
import sikd.ikonsol.model.JenisBas;
import sikd.ikonsol.model.Periode;
import sikd.ikonsol.model.ReferensiPemda;
import sikd.ikonsol.model.WorksheetView;

/**
 *
 * @author Imam Bhoim
 */
@ViewScoped
@Named(value = "worksheetViewBeanCtrl")
public class WorksheetViewBeanCtrl implements Serializable {

    WorksheetView wv;

    private List<WorksheetView> list;

    private List<JenisBas> listjbas;

    private List<ReferensiPemda> listprovinsi;

    private List<Periode> listperiode;

    private Boolean customExporter;

    @EJB
    WorksheetViewEjbHome worksheetViewEjbHome;

    @EJB
    RefPemdaEjbHome pemdaEjbHome;

    @EJB
    JenisBasEjbHome jenisBasEjbHome;

    @EJB
    PeriodeEjbHome periodeEjbHome;

    @EJB
    AkunEliminasiEjbHome akeliminasiEjbHome;

    private int idbas = 0;
    private int tahun = 0;
    private String kdPemda = null;
    private String kdPeriode = null;

    private List<ReferensiPemda> listPemda;

    public WorksheetViewBeanCtrl() {
        customExporter = false;
    }

    public Boolean getCustomExporter() {
        return customExporter;
    }

    public void setCustomExporter(Boolean customExporter) {
        this.customExporter = customExporter;
    }

    @PostConstruct
    public void init() {
        try {
            list = worksheetViewEjbHome.findAllByTahunBasProvinsi(tahun, idbas, kdPemda, kdPeriode);

            // listDetail = tbEjbHome.findAllByTahunKodeDetail(tahun, kdPemda);
        } catch (Exception e) {
            e.printStackTrace();
        }
        listjbas = jenisBasEjbHome.findAllAktif();
        listprovinsi = pemdaEjbHome.findAllProvinsi("__.00");
        listperiode = periodeEjbHome.findAllAktif();
        customExporter = false;
    }

    public void reload() {

        System.out.println(" Tahun : " + tahun);
        System.out.println(" BAS : " + idbas);
        System.out.println(" kdpemda : " + kdPemda);
        list = worksheetViewEjbHome.findAllByTahunBasProvinsi(tahun, idbas, kdPemda, kdPeriode);

    }

    public boolean debit(String db) {
        boolean val = false;
        try {
            if (db.trim().equalsIgnoreCase("D")) {
                val = true;
            } else {
                val = false;
            }
        } catch (Exception e) {
            //  e.printStackTrace();
            val = false;
        }

        return val;
    }

    public boolean kredit(String kd) {
        boolean val = false;
        try {
            if (kd.trim().equalsIgnoreCase("K")) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            //  e.printStackTrace();
            val = false;
        }
        return val;
    }

    public boolean laporanLRADebit(String db, int jnslap) {
        boolean val = false;
        try {
            if (db.trim().equalsIgnoreCase("D") && jnslap == 1) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            // e.printStackTrace();
            val = false;
        }
        return val;
    }

    public boolean laporanLRAKredit(String dk, int jnslap) {
        boolean val = false;
        try {
            if (dk.trim().equalsIgnoreCase("K") && jnslap == 1) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            // e.printStackTrace();
            val = false;
        }
        return val;
    }

    public boolean laporanLODebit(String db, int jnslap) {
        boolean val = false;
        try {
            if (db.trim().equalsIgnoreCase("D") && jnslap == 3) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            // e.printStackTrace();
            val = false;
        }
        return val;
    }

    public boolean laporanLOKredit(String dk, int jnslap) {
        boolean val = false;
        try {
            if (dk.trim().equalsIgnoreCase("K") && jnslap == 3) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            // e.printStackTrace();
            val = false;
        }
        return val;
    }

    public boolean laporanNeracaDebit(String db, int jnslap) {
        boolean val = false;
        try {
            if (db.trim().equalsIgnoreCase("D") && jnslap == 2) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            //  e.printStackTrace();
            val = false;
        }
        return val;
    }

    public boolean laporanNeracaKredit(String dk, int jnslap) {
        boolean val = false;
        try {
            if (dk.trim().equalsIgnoreCase("K") && jnslap == 2) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            //  e.printStackTrace();
            val = false;
        }
        return val;
    }

    public ArrayList<Integer> listTahun() {

        ArrayList<Integer> numbers = new ArrayList<Integer>();
        for (int years = 2015; years <= Calendar.getInstance().get(Calendar.YEAR); years++) {
            numbers.add(years);
        }
        return (numbers);
    }

    public String refNama4(int kode1, int kode2, int kode3, String kode4, int bast) {
        String name = null;
        try {
            name = akeliminasiEjbHome.selectRefAkun4(String.valueOf(kode1), String.valueOf(kode2), String.valueOf(kode3), String.valueOf(kode4),
                    bast).getNamaakun();
        } catch (Exception c) {
            // c.printStackTrace();
            name = "-";
        }
        return name;
    }

    public double nilaiEliminasi(int kode1, int kode2, int kode3, String kode4, int bast,
            String kdperiode1, String kdprov1, int tahun1) {

        double name = 0;
        try {
            name = worksheetViewEjbHome.selectEliminasi(String.valueOf(kode1), String.valueOf(kode2), String.valueOf(kode3),
                    String.valueOf(kode4), bast, kdperiode1, kdprov1,
                    tahun1).getNilaieliminasi();
            System.out.println("A " + name);
            name = name;
        } catch (Exception ex) {
            //   ex.printStackTrace();
            name = name;
        }

        return name;

    }

    public String refDK(int kode1, int kode2, int kode3, String kode4, int bast,
            String kdperiode1, String kdprov1, int tahun1) {
        String name = null;
        try {
            name = worksheetViewEjbHome.selectDK(String.valueOf(kode1), String.valueOf(kode2), String.valueOf(kode3),
                    String.valueOf(kode4), bast, kdperiode1, kdprov1,
                    tahun1).getDk();
        } catch (Exception c) {
            //   c.printStackTrace();
            name = "-";
        }
        return name;
    }

    public double nilaiEliminasiManual(int kode1, int kode2, int kode3, String kode4, int bast,
            String kdperiode1, String kdprov1, int tahun1) {

        double name = 0;
        try {
            name = worksheetViewEjbHome.selectEliminasiManual(String.valueOf(kode1), String.valueOf(kode2), String.valueOf(kode3),
                    String.valueOf(kode4), bast, kdperiode1, kdprov1,
                    tahun1).getNilai();
            System.out.println("A " + name);
            name = name;
        } catch (Exception ex) {
            //ex.printStackTrace();
            name = name;
        }

        return name;

    }

    public String refDKManual(int kode1, int kode2, int kode3, String kode4, int bast,
            String kdperiode1, String kdprov1, int tahun1) {
        String name = null;
        try {
            name = worksheetViewEjbHome.selectDKManual(String.valueOf(kode1), String.valueOf(kode2), String.valueOf(kode3),
                    String.valueOf(kode4), bast, kdperiode1, kdprov1,
                    tahun1).getDk();
        } catch (Exception c) {
            //c.printStackTrace();
            name = "-";
        }
        return name;
    }

    public String namaprov(String a) {
        String c = null;

        try {
            if (a != null) {
                ReferensiPemda b = pemdaEjbHome.findByKodeProv(a);
                c = b.getNama_pemda();

            } else {
                c = "-";

            }
        } catch (Exception ex) {
            c = "-";
        }
        return c;
    }

    public String tahunHeader(int a) {
        String b = null;
        if (a == 0) {
            b = "-";
        } else {
            b = String.valueOf(a);
        }
        return b;
    }

    public void postProcessXLS(Object document) {

        XSSFWorkbook wb = (XSSFWorkbook) document;
        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow header = sheet.getRow(0);

        Iterator<Row> rowIterator = sheet.iterator();

        if (rowIterator.hasNext()) {
            rowIterator.next();
        }

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                if (cell.getColumnIndex() > 1) {
                    if (cell.getRowIndex() > 4) {
                        if (!cell.getStringCellValue().isEmpty()) {
                            cell.setCellValue(Double.valueOf(cell.getStringCellValue().replace(",", "")));
                        }
                    }
                }
            }
        }
    }

    public WorksheetView getWv() {
        return wv;
    }

    public void setWv(WorksheetView wv) {
        this.wv = wv;
    }

    public List<WorksheetView> getList() {
        return list;
    }

    public void setList(List<WorksheetView> list) {
        this.list = list;
    }

    public WorksheetViewEjbHome getWorksheetViewEjbHome() {
        return worksheetViewEjbHome;
    }

    public void setWorksheetViewEjbHome(WorksheetViewEjbHome worksheetViewEjbHome) {
        this.worksheetViewEjbHome = worksheetViewEjbHome;
    }

    public RefPemdaEjbHome getPemdaEjbHome() {
        return pemdaEjbHome;
    }

    public void setPemdaEjbHome(RefPemdaEjbHome pemdaEjbHome) {
        this.pemdaEjbHome = pemdaEjbHome;
    }

    public int getIdbas() {
        return idbas;
    }

    public void setIdbas(int idbas) {
        this.idbas = idbas;
    }

    public int getTahun() {
        return tahun;
    }

    public void setTahun(int tahun) {
        this.tahun = tahun;
    }

    public String getKdPemda() {
        return kdPemda;
    }

    public void setKdPemda(String kdPemda) {
        this.kdPemda = kdPemda;
    }

    public List<ReferensiPemda> getListPemda() {
        return listPemda;
    }

    public void setListPemda(List<ReferensiPemda> listPemda) {
        this.listPemda = listPemda;
    }

    public List<JenisBas> getListjbas() {
        return listjbas;
    }

    public void setListjbas(List<JenisBas> listjbas) {
        this.listjbas = listjbas;
    }

    public List<ReferensiPemda> getListprovinsi() {
        return listprovinsi;
    }

    public void setListprovinsi(List<ReferensiPemda> listprovinsi) {
        this.listprovinsi = listprovinsi;
    }

    public List<Periode> getListperiode() {
        return listperiode;
    }

    public void setListperiode(List<Periode> listperiode) {
        this.listperiode = listperiode;
    }

    public String getKdPeriode() {
        return kdPeriode;
    }

    public void setKdPeriode(String kdPeriode) {
        this.kdPeriode = kdPeriode;
    }

}
