package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.DataPerusahaanYangBekerjaSamaDao;
import com.jasamedika.medifirst2000.dao.RekananDao;
import com.jasamedika.medifirst2000.entities.AdvokasiHukumMedicolegal;
import com.jasamedika.medifirst2000.entities.DataPerusahaanYangBekerjaSama;
import com.jasamedika.medifirst2000.entities.Rekanan;
import com.jasamedika.medifirst2000.service.DataPerusahaanYangBekerjaSamaService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.DataPerusahaanYangBekerjaSamaVO;
import com.jasamedika.medifirst2000.vo.MonitoringStatusPksVO;
import com.jasamedika.medifirst2000.vo.RekananVO;

@Service("DataPerusahaanYangBekerjaSamaService")
public class DataPerusahaanYangBekerjaSamaServiceImpl extends BaseVoServiceImpl implements DataPerusahaanYangBekerjaSamaService {
	
	@Autowired
	private DataPerusahaanYangBekerjaSamaDao dataPerusahaanYangBekerjaSamaDao;
	
	@Autowired
	private RekananDao rekananDao;
	
	@Autowired
	private BaseConverterImpl<RekananVO, Rekanan> rekananConverter;
	
	@Autowired
	private BaseConverterImpl<DataPerusahaanYangBekerjaSamaVO, DataPerusahaanYangBekerjaSama> dataPerusahaanYangBekerjaSamaConverter;
	
	@Override
	public Map<String, Object> addDataPerusahaanYangBekerjaSama(DataPerusahaanYangBekerjaSamaVO vo) {
		DataPerusahaanYangBekerjaSama dataPerusahaanYangBekerjaSama = new DataPerusahaanYangBekerjaSama();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			DataPerusahaanYangBekerjaSama model = new DataPerusahaanYangBekerjaSama();
			model = dataPerusahaanYangBekerjaSamaDao.getByIdRekanan(vo.getRekanan().getId());
			dataPerusahaanYangBekerjaSama = dataPerusahaanYangBekerjaSamaConverter.transferVOToModel(vo, new DataPerusahaanYangBekerjaSama());
			if(CommonUtil.isNotNullOrEmpty(model)){
				dataPerusahaanYangBekerjaSama.setNoRec(model.getNoRec());
			}
		}
		
		if(CommonUtil.isNotNullOrEmpty(vo.getRekanan())){
			dataPerusahaanYangBekerjaSama.setRekanan(rekananConverter.transferVOToModel(vo.getRekanan(), new Rekanan()));
		}
		
		
		Map<String, Object> result = new HashMap<String,Object>();
		DataPerusahaanYangBekerjaSama resultModel = dataPerusahaanYangBekerjaSamaDao.save(dataPerusahaanYangBekerjaSama);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("NoRec", resultModel.getNoRec());
			result.put("NoPks", resultModel.getNoPks());
		}
		return result;
	}

	@Override
	public Map<String, Object> updateDataPerusahaanYangBekerjaSama(DataPerusahaanYangBekerjaSamaVO vo) {
		DataPerusahaanYangBekerjaSama dataPerusahaanYangBekerjaSama = new DataPerusahaanYangBekerjaSama();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			dataPerusahaanYangBekerjaSama = dataPerusahaanYangBekerjaSamaConverter.transferVOToModel(vo, new DataPerusahaanYangBekerjaSama());
		}
		
		if(CommonUtil.isNotNullOrEmpty(vo.getRekanan())){
			dataPerusahaanYangBekerjaSama.setRekanan(rekananConverter.transferVOToModel(vo.getRekanan(), new Rekanan()));
		}
		
		
		Map<String, Object> result = new HashMap<String,Object>();
		DataPerusahaanYangBekerjaSama resultModel = dataPerusahaanYangBekerjaSamaDao.save(dataPerusahaanYangBekerjaSama);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("NoRec", resultModel.getNoRec());
			result.put("NoPks", resultModel.getNoPks());
		}
		return result;
	}

	@Override
	public Map<String, Object> findAllDataPerusahaanYangBekerjaSama(Integer jenisRekananId, Integer status) {
		List<DataPerusahaanYangBekerjaSama> list = new ArrayList<DataPerusahaanYangBekerjaSama>();
		list = (List<DataPerusahaanYangBekerjaSama>) dataPerusahaanYangBekerjaSamaDao.findAll();
		
		List<MonitoringStatusPksVO> listMonitoring = new ArrayList<MonitoringStatusPksVO>();
		
		for(DataPerusahaanYangBekerjaSama data : list){
			if(data.getRekanan().getJenisRekanan().getId()==jenisRekananId){
				MonitoringStatusPksVO monitoring = new MonitoringStatusPksVO();
				monitoring.setNoPks(data.getNoPks());
				monitoring.setNamaPerusahaan(data.getRekanan().getNamaRekanan());
				monitoring.setAlamat(data.getAlamatPenagihan());
				monitoring.setContactPerson(data.getContactPerson());
				monitoring.setJumlahKaryawan(data.getJumlahKaryawan());
				monitoring.setTglAwal(data.getJangkaAwal());
				monitoring.setTglAkhir(data.getJangkaAkhir());
				
				Long sub = data.getJangkaAkhir().getTime()-new Date().getTime();
				Calendar c = Calendar.getInstance();
				c.setTime(new Date(sub));
				int interval = c.get(Calendar.MONTH)+(c.get(Calendar.YEAR)-1970)*12;
				
				if(interval>=0){
					if(interval>=3){
						if(interval == 3 && c.get(Calendar.DAY_OF_MONTH)==1){
							monitoring.setStatus("Mendekati Nonaktif");
						}
						else {
							monitoring.setStatus("Aktif");
						}
					}
					else{
						if(interval == 0 && c.get(Calendar.DAY_OF_MONTH)==1){
							monitoring.setStatus("Nonaktif");
						}
						else {
							monitoring.setStatus("Mendekati Nonaktif");
						}
					}
				}
				else
				{
					monitoring.setStatus("Nonaktif");
				}
				
				
				listMonitoring.add(monitoring);
			}
		}
		
		List<MonitoringStatusPksVO> resultListMonitoring = new ArrayList<MonitoringStatusPksVO>();
		
		String searchStatus = "";
		
		switch (status) {
		case 1:
			searchStatus = "Aktif";
			break;
		case 2:
			searchStatus = "Mendekati Nonaktif";
			break;
		case 3:
			searchStatus = "Nonaktif";
		default:
			
			break;
		}
		
		for(MonitoringStatusPksVO data : listMonitoring){
			if(data.getStatus()==searchStatus) resultListMonitoring.add(data);
		}
		
		Map<String, Object> result = new HashMap<String,Object>();
		if(CommonUtil.isNotNullOrEmpty(resultListMonitoring)){
			result.put("data", resultListMonitoring);
		}
		return result;
	}

	@Override
	public Map<String, Object> getPerusahaan() {
		List<Rekanan> list = new ArrayList<Rekanan>();
		list = rekananDao.findAllPerusahaan();
		Map<String, Object> result = new HashMap<String,Object>();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getAsuransi() {
		List<Rekanan> list = new ArrayList<Rekanan>();
		list = rekananDao.findAllAsuransi();
		Map<String, Object> result = new HashMap<String,Object>();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getPerusahaanYangBekerjaSama() {
		List<DataPerusahaanYangBekerjaSama> list = new ArrayList<DataPerusahaanYangBekerjaSama>();
		list = dataPerusahaanYangBekerjaSamaDao.getPerusahaanYangBekerjasama();
		Map<String, Object> result = new HashMap<String,Object>();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getAsuransiYangBekerjaSama() {
		List<DataPerusahaanYangBekerjaSama> list = new ArrayList<DataPerusahaanYangBekerjaSama>();
		list = dataPerusahaanYangBekerjaSamaDao.getAsuransiYangBekerjasama();
		Map<String, Object> result = new HashMap<String,Object>();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getRumahSakitYangBekerjaSama() {
		List<DataPerusahaanYangBekerjaSama> list = new ArrayList<DataPerusahaanYangBekerjaSama>();
		list = dataPerusahaanYangBekerjaSamaDao.getRumahSakitYangBekerjaSama(); //Salman
		Map<String, Object> result = new HashMap<String,Object>();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getYayasanYangBekerjaSama() {
		List<DataPerusahaanYangBekerjaSama> list = new ArrayList<DataPerusahaanYangBekerjaSama>();
		list = dataPerusahaanYangBekerjaSamaDao.getYayasanYangBekerjaSama(); //Salman
		Map<String, Object> result = new HashMap<String,Object>();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getRumahSakit() {
		List<Rekanan> list = new ArrayList<Rekanan>();
		list = rekananDao.findAllRumahSakit();
		Map<String, Object> result = new HashMap<String,Object>();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getYayasan() {
		List<Rekanan> list = new ArrayList<Rekanan>();
		list = rekananDao.findAllYayasan();
		Map<String, Object> result = new HashMap<String,Object>();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> findAll(Integer page, Integer take, String sort, String dir,
			Specification<DataPerusahaanYangBekerjaSama> spec) {
Direction direction=null;
		
		if(dir.equalsIgnoreCase("asc")){
			direction=Sort.Direction.ASC;
		}else{
			direction=Sort.Direction.DESC;
		}
		Pageable pageable = new PageRequest(page, take, direction, sort);
		Page<DataPerusahaanYangBekerjaSama> resultPage = dataPerusahaanYangBekerjaSamaDao.findAll(spec, pageable);
		List<DataPerusahaanYangBekerjaSama> list = resultPage.getContent();
		
		List<MonitoringStatusPksVO> listMonitoring = new ArrayList<MonitoringStatusPksVO>();
		
		for(DataPerusahaanYangBekerjaSama data : list){
			MonitoringStatusPksVO monitoring = new MonitoringStatusPksVO();
			monitoring.setNoPks(data.getNoPks());
			monitoring.setNamaPerusahaan(data.getRekanan().getNamaRekanan());
			monitoring.setAlamat(data.getAlamatPenagihan());
			monitoring.setContactPerson(data.getContactPerson());
			monitoring.setJumlahKaryawan(data.getJumlahKaryawan());
			monitoring.setTglAwal(data.getJangkaAwal());
			monitoring.setTglAkhir(data.getJangkaAkhir());
			listMonitoring.add(monitoring);
		}
		
		return constructMapReturn(listMonitoring, resultPage.getTotalElements(), resultPage.getTotalPages());
	}

}
