package com.jasamedika.medifirst2000.service.impl;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.ProdukDao;
import com.jasamedika.medifirst2000.dao.RekananDao;
import com.jasamedika.medifirst2000.dao.RekananSkKontrakDao;
import com.jasamedika.medifirst2000.dao.RekananSkKontrakDetailDao;
import com.jasamedika.medifirst2000.dao.custom.RekananDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.RekananSkKontrakDaoCustom;
import com.jasamedika.medifirst2000.entities.DetailJenisProduk;
import com.jasamedika.medifirst2000.entities.Jabatan;
import com.jasamedika.medifirst2000.entities.JenisRekanan;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.Rekanan;
import com.jasamedika.medifirst2000.entities.RekananSkKontrak;
import com.jasamedika.medifirst2000.entities.RekananSkKontrakDetail;
import com.jasamedika.medifirst2000.entities.SatuanStandar;
import com.jasamedika.medifirst2000.service.KontrakService;
import com.jasamedika.medifirst2000.service.RekananService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.StringUtil;
import com.jasamedika.medifirst2000.vo.JabatanVO;
import com.jasamedika.medifirst2000.vo.JenisRekananVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.RekananSkKontrakDetailVO;
import com.jasamedika.medifirst2000.vo.RekananSkKontrakVO;
import com.jasamedika.medifirst2000.vo.RekananVO;
import com.jasamedika.medifirst2000.vo.SatuanStandarVO;


/**
 * 
 * @author adik
 */
@Service("kontrakService")
public class KontrakServiceImpl implements KontrakService{
	
	@Autowired
	private BaseConverterImpl<RekananSkKontrakVO,RekananSkKontrak> rekananSkKontrakConvereter;
	
	@Autowired
	private RekananDaoCustom rekananDaoCustom;
	
	@Autowired
	private ProdukDao produkDao;
	
	@Autowired
	private RekananSkKontrakDaoCustom rekananSkKontrakDaoCustom;
	
	@Autowired
	private RekananSkKontrakDetailDao rekananSkKontrakDetailDao;

	@Autowired
	private BaseConverterImpl<RekananVO,Rekanan> rekananConvereter;
	
	@Autowired
	private BaseConverterImpl<SatuanStandarVO,SatuanStandar> satuanStandarConvereter;
	
	@Autowired
	private BaseConverterImpl<RekananSkKontrakVO,RekananSkKontrak> rekananSkKontrakConverter;
	
	@Autowired
	private BaseConverterImpl<PegawaiVO,Pegawai> pegawaiConverter;
	
	@Autowired
	private BaseConverterImpl<RekananSkKontrakDetailVO,RekananSkKontrakDetail> rekananSkKontrakDetailConverter;
	
	@Autowired
	private BaseConverterImpl<JabatanVO,Jabatan> jabatanConverter;

	@Value("${reportDirectory}")
	String reportDirectory;
	
	@Autowired
	private RekananSkKontrakDao rekananSkKontrakDao;
	

	@Override
	public Map<String, Object> saveKontrak(RekananSkKontrakVO vo) {
		RekananSkKontrak rekananSkKontrak=rekananSkKontrakConvereter.transferVOToModel(vo, new RekananSkKontrak());
		if(CommonUtil.isNotNullOrEmpty(vo.getRekanan())){
			rekananSkKontrak.setRekanan(rekananConvereter.transferVOToModel(vo.getRekanan(), new Rekanan()));
		}
		String targetFile =null;
		if (CommonUtil.isNotNullOrEmpty(vo.getUrlDokumen())) {
			try {
				byte[] decodedBytes = Base64.decodeBase64(vo.getUrlDokumen());
				String fileName = UUID.randomUUID().toString().replaceAll("-", "") + "."+vo.getFormatFile();
				targetFile = reportDirectory + fileName;
				writeByteArraysToFile(targetFile, decodedBytes);
				rekananSkKontrak.setUrlDokumen(fileName);
			} catch (IOException e) {
				System.out.println(e.toString());
			}

		}
		rekananSkKontrakDao.save(rekananSkKontrak);
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("data",rekananSkKontrak.getNoRec());
		return result;
	}

	@Override
	public Map<String, Object> listKontrak(Integer page, Integer limit, String sort, String dir, String noKontrak,Integer rekananId, String namaKontrak) {


		int totalRow = rekananSkKontrakDaoCustom.countRekananSkKontrak(namaKontrak,rekananId,noKontrak);
		int totalPages = 0;

		int pageRequested = page;

		if (totalRow > 0) {
			totalPages = (int) Math.ceil((double) totalRow / (double) limit);
		} else {
			totalPages = 0;
		}

		if (pageRequested > totalPages)
			pageRequested = totalPages;
		int rowStart = pageRequested * limit - limit;
		if (rowStart < 0) {
			rowStart = 0;
		}
		int rowEnd = limit;
		Map<String, Object> result=new HashMap<String,Object>();
		result.put("data", rekananSkKontrakDaoCustom.listRekananSkKontrak(page,limit,sort,dir,namaKontrak,rekananId,noKontrak));
		result.put("totalPages", totalPages);
		result.put("totalRow", totalRow);
		return result;
		
	}

	@Override
	public Map<String, Object> saveKontrakDetail(RekananSkKontrakDetailVO vo) {
		RekananSkKontrakDetail rekananSkKontrakDetail=new RekananSkKontrakDetail();
		rekananSkKontrakDetail=rekananSkKontrakDetailConverter.transferVOToModel(vo, new RekananSkKontrakDetail());
		if(CommonUtil.isNotNullOrEmpty(vo.getRekananSkKontrak())){
			rekananSkKontrakDetail.setRekananSkKontrak(rekananSkKontrakConverter.transferVOToModel(vo.getRekananSkKontrak(), new RekananSkKontrak()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getPegawaiRekanan())){
			rekananSkKontrakDetail.setPegawaiRekanan(pegawaiConverter.transferVOToModel(vo.getPegawaiRekanan() ,new Pegawai()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getJabatanPegawaiRekanan())){
			rekananSkKontrakDetail.setJabatanPegawaiRekanan(jabatanConverter.transferVOToModel(vo.getJabatanPegawaiRekanan(),new Jabatan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getSatuanStandar())){
			rekananSkKontrakDetail.setSatuanStandar(satuanStandarConvereter.transferVOToModel(vo.getSatuanStandar(), new SatuanStandar()));
		}
		Produk produkResult =new Produk();
		if(CommonUtil.isNotNullOrEmpty(vo.getDetailPekerjaan())){

			Produk produk = new Produk();
			DetailJenisProduk detailJenisProduk = new DetailJenisProduk();
			detailJenisProduk.setId(123);
			detailJenisProduk.setKdProfile((short) 0);
			produk.setDetailJenisProduk(detailJenisProduk);
			produk.setNamaProduk(vo.getDetailPekerjaan());
			produk.setNilaiNormal((short) 0);
			produk.setqProduk(1);
			if (CommonUtil.isNotNullOrEmpty(vo.getSatuanStandar())) {
				produk.setSatuanStandar(satuanStandarConvereter.transferVOToModel(vo.getSatuanStandar(), new SatuanStandar()));
			}
			produk.setQtyJualTerkecil((double) 0);
			produk.setQtySatuKemasan(0);
			produk.setQtyTerkecil((double) 0);
			produk.setKdProduk(StringUtil.formatNumber(String.valueOf(produkDao.getCount() + 1), 6));
			produk.setKdProdukIntern(StringUtil.formatNumber(String.valueOf(produkDao.getCount() + 1), 6));
			produk.setVerifikasiAnggaran(false);
			produkResult=produkDao.save(produk);

		
		}
		rekananSkKontrakDetail.setProduk(produkResult);
		rekananSkKontrakDetail=rekananSkKontrakDetailDao.save(rekananSkKontrakDetail);
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("noRec", rekananSkKontrakDetail.getNoRec());
		
		return data;
	}

	public static void writeByteArraysToFile(String fileName, byte[] content) throws IOException {

		File file = new File(fileName);
		BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(file));
		writer.write(content);
		writer.flush();
		writer.close();

	}

	@Override
	public Map<String, Object> detilKontrak(String noRec) {
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("header",rekananSkKontrakDao.findRekananSkKonrak(noRec));
		data.put("detail", rekananSkKontrakDetailDao.findByParent(noRec));
		return data;
	}
	
	
	
	
}

