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.List;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.DokumenDao;
import com.jasamedika.medifirst2000.dao.DokumenMasukDao;
import com.jasamedika.medifirst2000.dao.DokumenTemplateDao;
import com.jasamedika.medifirst2000.dao.JenisDokumenDao;
import com.jasamedika.medifirst2000.dao.KelompokTransaksiDao;
import com.jasamedika.medifirst2000.dao.RuanganDao;
import com.jasamedika.medifirst2000.dao.RunningNumberDao;
import com.jasamedika.medifirst2000.dao.StrukKirimDao;
import com.jasamedika.medifirst2000.dao.StrukPelayananDao;
import com.jasamedika.medifirst2000.dao.StrukVerifikasiDao;
import com.jasamedika.medifirst2000.dao.SuratDao;
import com.jasamedika.medifirst2000.dao.custom.SuratMasukDaoCustom;
import com.jasamedika.medifirst2000.entities.Dokumen;
import com.jasamedika.medifirst2000.entities.DokumenMasuk;
import com.jasamedika.medifirst2000.entities.DokumenTemplate;
import com.jasamedika.medifirst2000.entities.JenisArsip;
import com.jasamedika.medifirst2000.entities.JenisDokumen;
import com.jasamedika.medifirst2000.entities.JenisSurat;
import com.jasamedika.medifirst2000.entities.KategoryDokumen;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.entities.MappingKirimDokumenPasien;
import com.jasamedika.medifirst2000.entities.MetodeKirim;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.RunningNumber;
import com.jasamedika.medifirst2000.entities.SatuanWaktu;
import com.jasamedika.medifirst2000.entities.SifatSurat;
import com.jasamedika.medifirst2000.entities.StatusBerkas;
import com.jasamedika.medifirst2000.entities.StrukKirim;
import com.jasamedika.medifirst2000.entities.StrukPelayanan;
import com.jasamedika.medifirst2000.entities.Surat;
import com.jasamedika.medifirst2000.entities.TipePengirimSurat;
import com.jasamedika.medifirst2000.service.LoginUserService;
import com.jasamedika.medifirst2000.service.ProdukHukumService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.StringUtil;
import com.jasamedika.medifirst2000.vo.DokumenMasukVO;
import com.jasamedika.medifirst2000.vo.DokumenTemplateVO;
import com.jasamedika.medifirst2000.vo.DokumenVO;
import com.jasamedika.medifirst2000.vo.JenisArsipVO;
import com.jasamedika.medifirst2000.vo.JenisDokumenVO;
import com.jasamedika.medifirst2000.vo.JenisSuratVO;
import com.jasamedika.medifirst2000.vo.KategoryDokumenVO;
import com.jasamedika.medifirst2000.vo.LoginUserVO;
import com.jasamedika.medifirst2000.vo.MappingKirimDokumenPasienVO;
import com.jasamedika.medifirst2000.vo.MetodeKirimVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.SatuanWaktuVO;
import com.jasamedika.medifirst2000.vo.SifatSuratVO;
import com.jasamedika.medifirst2000.vo.StatusBerkasVO;
import com.jasamedika.medifirst2000.vo.StrukKirimVO;
import com.jasamedika.medifirst2000.vo.StrukPelayananVO;
import com.jasamedika.medifirst2000.vo.SuratVO;
import com.jasamedika.medifirst2000.vo.TipePengirimSuratVO;

@Service("ProdukHukumService")
public class ProdukHukumServiceImpl extends BaseVoServiceImpl implements ProdukHukumService {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ProdukHukumServiceImpl.class);
	
	
	
	@Autowired
	private DokumenTemplateDao dokumenTemplateDao;
	@Autowired
	private DokumenDao daoDokumen;

	@Autowired
	private KelompokTransaksiDao kelompokTransaksiDao;

	@Autowired
	private RuanganDao ruanganDao;

	
	 

	@Autowired
	private BaseConverterImpl<DokumenVO, Dokumen> dokumenConverter;

	 
	@Autowired
	private LoginUserService loginUserService;
	
	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;

	@Autowired
	private BaseConverterImpl<LoginUserVO, LoginUser> loginUserConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Autowired
	private BaseConverterImpl<DokumenTemplateVO, DokumenTemplate> dokumenTemplateConverter;
	@Autowired
	private BaseConverterImpl<JenisDokumenVO, JenisDokumen> jenisDokumenConverter;
	@Autowired
	private BaseConverterImpl<KategoryDokumenVO, KategoryDokumen> kategoryDokumenConverter;
	
	@Value("${reportDirectory}")
	String reportDirectory;
	
	
	@Autowired
	JenisDokumenDao jenisDokumenDao;
	
	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
	@Transactional
	public String uploadDocumentTemplateSurat(DokumenTemplateVO dokumenTemplateVO) {
		DokumenTemplate dokumenTemplate= dokumenTemplateConverter.transferVOToModel(dokumenTemplateVO, new DokumenTemplate());
		byte[] decodedBytes = Base64.decodeBase64(dokumenTemplateVO.getBodyFile());
		String extension =  FilenameUtils.getExtension(dokumenTemplate.getFileName());
		String fileName=UUID.randomUUID().toString().replaceAll("-", "")+"."+extension;
		String targetFile = reportDirectory+File.separator+ "template";
		try {
			//Buat Folder apabila belum ada
			File files = new File(targetFile);
	        if (!files.exists()) {
	            if (files.mkdirs()) {
	                System.out.println("Multiple directories are created!");
	            } else {
	                System.out.println("Failed to create multiple directories!");
	            }
	            
	        }
	        //Simpan kedalam file fisik
			writeByteArraysToFile(targetFile+File.separator+fileName, decodedBytes);
			dokumenTemplate.setFileLocation(targetFile+File.separator+fileName);
			dokumenTemplate.setFileName(dokumenTemplateVO.getFileName());
			//Kategori
			if(CommonUtil.isNotNullOrEmpty(dokumenTemplateVO.getKategoryDokumen())){
				dokumenTemplate.setKategoryDokumen(kategoryDokumenConverter.transferVOToModel(dokumenTemplateVO.getKategoryDokumen(), new KategoryDokumen()));
			} 
			System.out.println(dokumenTemplateVO.getJenisDokumen().getId());
			if(CommonUtil.isNotNullOrEmpty(dokumenTemplateVO.getJenisDokumen())){
				dokumenTemplate.setJenisDokumen(jenisDokumenConverter.transferVOToModel(dokumenTemplateVO.getJenisDokumen(), new JenisDokumen()));
				 
			}
			dokumenTemplateDao.save(dokumenTemplate);
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		if(CommonUtil.isNotNullOrEmpty(dokumenTemplate.getId())){
			return dokumenTemplate.getId().toString();
		}else{
			return null;
		}		
	}

	@Override
	public DokumenTemplate findById(Integer id) {
		return dokumenTemplateDao.findByIdDokumen(id);
	}

	 

	@Override
	public List<DokumenTemplate> getAllDokumenTemplate(Integer page, Integer limit, String sort, String dir,
			String dateStart, String dateEnd, Integer namaSurat) {
		 List<DokumenTemplate> result= dokumenTemplateDao.getAllDokumen();
		return result;
	}
	 
	
	public Integer getDayFromString(String jangkaWaktu) {
	     Integer day = 0;
	     switch (jangkaWaktu.toLowerCase()) {
	         case "hari":
	             day = 1;
	             break;
	         case "minggu":
	             day = 1;
	             break;
	         case "bulan":
	             day = 30;
	             break;
	         case "tahun":
	              day = 365;
	             break;
	         default:
	             throw new IllegalArgumentException("Invalid day of the week: " + jangkaWaktu);
	     }
	     return day;
	}
}
