package sgc.portal.cw.cw0804.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sgc.portal.base.exception.BusinessException;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.cw.cw0804.model.CW08040005P;
import sgc.portal.cw.cw0804.persistence.CW08040005PMapper;
import sgc.portal.com.sym.mail.service.MailService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.List;

@Service
public class CW08040005PService {
    private final CW08040005PMapper mapper;
    private final MailService mailService;

    public CW08040005PService(CW08040005PMapper mapper, MailService mailService) {
        this.mapper = mapper;
        this.mailService = mailService;
    }

	@Transactional
    public Map<String, String> save(CW08040005P cW08040005P) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	int mail_yn = 0;
    	
    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.KOREA);
    	Date sDate 	= null;
    	try {sDate 	= dateFormat.parse(cW08040005P.getStartYyyymmdd());} catch (ParseException e) {e.printStackTrace();}
    	Date eDate 	= null;
    	try {eDate 	= dateFormat.parse(cW08040005P.getEndYyyymmdd());} catch (ParseException e) {e.printStackTrace();}
    	int period 	= (int)((eDate.getTime() - sDate.getTime()) / (24 * 60 * 60 * 1000));
    	
    	String s_temp 	= "";
    	Calendar cal_t 	= Calendar.getInstance();
    	int day 		= 0; //요일 
    	
		s_temp 	= dateFormat.format(sDate);
		cal_t.setTime(sDate);
		day 	= cal_t.get(cal_t.DAY_OF_WEEK);
		cW08040005P.setStartDate(s_temp);
		
		if(cW08040005P.getKeyYn().equals("Y") || (day == 2 && period > 3) || (day != 2 && period > 5)) {
			
			mapper.forUpdateDis(cW08040005P);
			if(mapper.getCheckReservation(cW08040005P) == 0) {
				if(mapper.getCheckReservationDis(cW08040005P) == 0) {
					cW08040005P.setStatus("20");
					insert(cW08040005P);
					updateDis(cW08040005P,"Y");
					insertHis(cW08040005P);
					mail_yn=1;
				} else {
					throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
				}
			} else {
				throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
			}
			
			if(mail_yn == 1) {
				
				List<EasyHashMap<String, Object>> email = mapper.selectUserEmailList(cW08040005P);

				StringBuffer sContent = new StringBuffer();
				String cmpnCd = cW08040005P.getSubscCmpnCd();
				String srecvAddress = "";
				String sSender = cW08040005P.getSubscSabun();	
				String sSubject = "승인요청한 회의실 예약이 있습니다.";
				String ssubSubject = "회의실 승인요청";
        		
        		sContent.append("[회의실]승인요청한 회의실 예약이 있습니다.<br><br>");
        		sContent.append("회   의    명  : " + cW08040005P.getMeetingCon() +  "<br>");
        		sContent.append("예   약    자  : " + cW08040005P.getSubscName() +    "<br>");
        		sContent.append("부        서  : " +  cW08040005P.getDeptName() +  "<br>");
        		sContent.append("기        간  : " +  cW08040005P.getStartYyyymmdd() + " ~ " + cW08040005P.getEndYyyymmdd() +     "<br>");
				sContent.append("열쇠사용 여부  : "+ cW08040005P.getKeyYn() +  "<br>");
            	
				for(int i = 0; i < email.size(); i++){
					srecvAddress = (String) email.get(i).get("email");
					mailService.mailSend(cmpnCd, sSubject, ssubSubject, sContent, sSender, srecvAddress);
				}
			}
		} else {
			mapper.forUpdateDis(cW08040005P);
			if(mapper.getCheckReservation(cW08040005P) == 0) {
				if(mapper.getCheckReservationDis(cW08040005P) == 0) {
					cW08040005P.setStatus("10");
					insert(cW08040005P);
					updateDis(cW08040005P,"N");
					insertHis(cW08040005P);
				} else {
					throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
				}
			} else {
				throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
			}
		}
        return result;
    }

    private void insert(CW08040005P cW08040005P) {
        mapper.insert(cW08040005P);
    }
    
    private void updateDis(CW08040005P cW08040005P,String yn) {
    	
    	if(yn.equals("Y")) {
    		cW08040005P.setColname(mapper.getReservationDisY(cW08040005P));
    	} else {
    		cW08040005P.setColname(mapper.getReservationDisN(cW08040005P));
    	}
    	
        mapper.updateDis(cW08040005P);
    }
    
    private void insertHis(CW08040005P cW08040005P) {
        mapper.insertHis(cW08040005P);
    }
}
