/*
 * 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.sikd5.sikd5bean.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import sikd.sikd5.sikd5bean.query.Periode;

/**
 *
 * @author agus
 */
public class SIKDUtil {
	public static final String SERVER = "Wildfly";

//    public static final String SERVER = "Glassfish";

	public static String uploadPathnya(String uploadpath, short tahun, String kdikd) {
		return uploadpath + File.separator + tahun + File.separator + kdikd + File.separator + "sofcopy" + File.separator + "zip"
				+ File.separator;
	}

	public static String uploadPathCsv(String uploadpath, short tahun, String kdikd) {
		return uploadpath + File.separator + tahun + File.separator + kdikd + File.separator + "sofcopy" + File.separator + "csvzip"
				+ File.separator;
	}

	public static String uploadPathUnzipnya(String uploadpath, short tahun, String kdikd) {
		return uploadpath + File.separator + tahun + File.separator + kdikd + File.separator + "sofcopy" + File.separator + "unzip"
				+ File.separator;
	}

	public static String uploadPathCsvUnzip(String uploadpath, short tahun, String kdikd) {
		return uploadpath + File.separator + tahun + File.separator + kdikd + File.separator + "sofcopy" + File.separator + "csvunzip"
				+ File.separator;
	}

	public static String uploadPathHcnya(String uploadpath, short tahun, String kdikd) {
		return uploadpath + File.separator + tahun + File.separator + kdikd + File.separator + "hardcopy" + File.separator;
	}

	public static void cekFolder(String foldernya) {
		File f = new File(foldernya);
		if (!f.isDirectory())
			f.mkdirs();
		f = null;
	}

	public static String dateToString1(Date tgl) {
		if (tgl != null) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_HHmmssSSS");
				return sdf.format(tgl);
			} catch (Exception ex) {
				return "";
			}
		} else
			return "";
	}

	public static List<Periode> getBulans() {
		List<Periode> result = new ArrayList<Periode>();
		result.add(new Periode((short) 1, "Januari"));
		result.add(new Periode((short) 2, "Februari"));
		result.add(new Periode((short) 3, "Maret"));
		result.add(new Periode((short) 4, "April"));
		result.add(new Periode((short) 5, "Mei"));
		result.add(new Periode((short) 6, "Juni"));
		result.add(new Periode((short) 7, "Juli"));
		result.add(new Periode((short) 8, "Agustus"));
		result.add(new Periode((short) 9, "September"));
		result.add(new Periode((short) 10, "Oktober"));
		result.add(new Periode((short) 11, "November"));
		result.add(new Periode((short) 12, "Desember"));
		return result;
	}

	public static <T> List<List<T>> getPages(Collection<T> c, Integer pageSize) {
		if (c == null)
			return Collections.emptyList();
		List<T> list = new ArrayList<T>(c);
		if (pageSize == null || pageSize <= 0 || pageSize > list.size())
			pageSize = list.size();
		int numPages = (int) Math.ceil((double) list.size() / (double) pageSize);
		List<List<T>> pages = new ArrayList<List<T>>(numPages);
		for (int pageNum = 0; pageNum < numPages;)
			pages.add(list.subList(pageNum * pageSize, Math.min(++pageNum * pageSize, list.size())));
		return pages;
	}

	public static Long selisihTanggal(Date tgawal, Date tgakhir) {
		Long DAY = 24L * 60L * 60L * 1000L;
		Long hasil = 0L;

		if (tgawal == null && tgakhir == null) {
			hasil = 0L;
		} else {
			hasil = (tgawal.getTime() - tgakhir.getTime()) / DAY;
		}
		return hasil;
	}

	public static int versiStringToInt(String versiString) {
		Integer persi = 0;
		Integer int1 = 0;
		Integer int2 = 0;
		Integer int3 = 0;
		if (versiString != null && versiString.trim() != "") {
			String[] parts = versiString.split("\\.");
			if (parts.length > 0) {
				int1 = Integer.valueOf(parts[0]);
			}
			if (parts.length > 1) {
				int2 = Integer.valueOf(parts[1]);
			}
			if (parts.length > 2) {
				int3 = Integer.valueOf(parts[2]);
			}
		}
		persi = (int1 * 1000000) + (int2 * 1000) + int3;
		return persi;

	}
}
