package sgc.portal.cw.cw1502.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.cw1502.model.CW15020002P;
import sgc.portal.cw.cw1502.persistence.CW15020002PMapper;

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

@Service
public class CW15020002PService {
    private final CW15020002PMapper mapper;

    public CW15020002PService(CW15020002PMapper mapper) {
        this.mapper = mapper;
    }

    @Transactional
    public Map<String, String> save(CW15020002P cW15020002P) throws ParseException {
    	
    	Map<String,String> result = new HashMap<>();
    	
        Date format1 = new SimpleDateFormat("yyyyMMdd").parse(cW15020002P.getStartYyyymmdd());
        Date format2 = new SimpleDateFormat("yyyyMMdd").parse(cW15020002P.getEndYyyymmdd());
        
        long diffSec = (format2.getTime() - format1.getTime()) / 1000; //초 차이
        long diffDays = diffSec / (24*60*60); //일자수 차이

    	Calendar cal1 = Calendar.getInstance();
    	Calendar cal2 = Calendar.getInstance();
    	
    	cal1.setTime(format1);
    	cal2.setTime(format2);

        cal1.add(Calendar.DATE, 1);
        cal2.add(Calendar.DATE, -1);

        String format3 = new SimpleDateFormat("yyyyMMdd").format(cal1.getTime());
        String format4 = new SimpleDateFormat("yyyyMMdd").format(cal2.getTime());
        
    	if(diffDays > 1) {
    		
            cW15020002P.setSearchYmd1(format3); // 시작일자 +1일
            cW15020002P.setSearchYmd2(format4); // 종료일자 -1일

        	cW15020002P.setCheck1(disColCount1(cW15020002P));//시작일자 디스플레이 컬럼수
        	cW15020002P.setCheck2(disColCount2(cW15020002P));//종료일자 디스플레이 컬럼수
            
        	if(mapper.getCheckReservation(cW15020002P) == 0) {
        		if(getCheckReservationDis(cW15020002P, cW15020002P.getCheck1()) == 0) {//시작일자 중복체크
            		if(getCheckReservationDis2(cW15020002P) == 0) {//종료일자 중복체크
                		if(mapper.getCheckReservationDis3(cW15020002P) == 0) {//중간일자 중복체크
		    				insert(cW15020002P);
							updateDis(cW15020002P, cW15020002P.getCheck1());//시작일자
							updateDis2(cW15020002P);//종료일자
							updateDis3(cW15020002P);//중간일자
                		} else {
        					throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
                		} 
            		} else {
    					throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
            		} 
        		} else {
					throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
        		} 
        	} else {
				throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
        	}
        	
    	} else if(diffDays > 0) {
        	cW15020002P.setCheck1(disColCount1(cW15020002P));//시작일자 디스플레이 컬럼수
        	cW15020002P.setCheck2(disColCount2(cW15020002P));//종료일자 디스플레이 컬럼수
        	if(mapper.getCheckReservation(cW15020002P) == 0) {
        		if(getCheckReservationDis(cW15020002P, cW15020002P.getCheck1()) == 0) {//시작일자 중복체크
            		if(getCheckReservationDis2(cW15020002P) == 0) {//종료일자 중복체크
	    				insert(cW15020002P);
						updateDis(cW15020002P, cW15020002P.getCheck1());//시작일자
						updateDis2(cW15020002P);//종료일자
            		} else {
    					throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
            		} 
        		} else {
					throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
        		} 
        	} else {
				throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
        	}
    		
    	} else if(diffDays == 0) {
        	cW15020002P.setCheck(disColCount(cW15020002P));	//시작일자 디스플레이 컬럼수
        	if(mapper.getCheckReservation(cW15020002P) == 0) {
        		if(getCheckReservationDis(cW15020002P, cW15020002P.getCheck()) == 0) {
    				insert(cW15020002P);
					updateDis(cW15020002P, cW15020002P.getCheck());//시작일자
        		} else {
					throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
        		} 
        	} else {
				throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
        	}
    	}
        return result;
    }

    private void insert(CW15020002P cW15020002P) {
        mapper.insert(cW15020002P);
    }
    
    private void updateDis(CW15020002P cW15020002P, int checkCnt) {
    	
    	int cnt			= checkCnt;
    	String col 		= "";
    	String colname 	= "";
    	String reinfo 	= mapper.getReservationDis(cW15020002P);
    	
    	colname = "T" + cW15020002P.getStartHhmm().substring(0, 2);
    	col 	= colname + " = '" + reinfo + "'";
    	
    	for( int i = 1; i < cnt; i++ ){
        	//컬럼명 변경
        	if(colname.length()<4){
          		colname = "," + colname;
          	}
      		colname = String.valueOf(Integer.parseInt(colname.substring(2, 4))+1);
      		if( colname.length() < 2 ){
      			colname = ",T0" + colname;
      		}else{
      			colname = ",T" + colname;
      		}
        	
        	col = col + colname + " = '" + reinfo + "'";
    	}
    	cW15020002P.setColname(col);
    	
        mapper.updateDis(cW15020002P);
    }
    
    private void updateDis2(CW15020002P cW15020002P) {
    	
    	int cnt			= cW15020002P.getCheck2();
    	String col 		= "";
    	String colname 	= "";
    	String reinfo 	= mapper.getReservationDis(cW15020002P);
    	
    	colname = "T00";
    	col 	= colname + " = '" + reinfo + "'";
    	
    	for( int i = 1; i < cnt; i++ ){
        	//컬럼명 변경
        	if(colname.length()<4){
          		colname = "," + colname;
          	}
      		colname = String.valueOf(Integer.parseInt(colname.substring(2, 4))+1);
      		if( colname.length() < 2 ){
      			colname = ",T0" + colname;
      		}else{
      			colname = ",T" + colname;
      		}
        	
        	col = col + colname + " = '" + reinfo + "'";
    	}
    	cW15020002P.setColname(col);
    	
        mapper.updateDis2(cW15020002P);
    }
    
    private void updateDis3(CW15020002P cW15020002P) {
    	
    	String reinfo 	= mapper.getReservationDis(cW15020002P);
    	
    	cW15020002P.setColname(reinfo);
    	
        mapper.updateDis3(cW15020002P);
    }
    
    private int disColCount(CW15020002P cW15020002P) {
    	
    	int 	cnt		= 0;
    	String 	start_t = "";
        String 	end_t	= "";
        String 	cnt_t	= "";
        
        int i_start_t	= 0;
        int i_end_t		= 0;
        int h			= 0;
        int m			= 0;
        
        start_t = cW15020002P.getStartHhmm().replace(":", "");
    	end_t 	= cW15020002P.getEndHhmm().replace(":", "");
    	
    	i_start_t	= Integer.parseInt(start_t);
    	i_end_t		= Integer.parseInt(end_t);
    	
    	cnt		= i_end_t - i_start_t;
    	cnt_t	= String.valueOf(cnt);

    	
    	if(cnt_t.length()>3){
    		h = Integer.parseInt(cnt_t.substring(0, 2));
    		m = Integer.parseInt(cnt_t.substring(2));
    	}else if(cnt_t.length() > 2){
    		h = Integer.parseInt(cnt_t.substring(0, 1));
    		m = Integer.parseInt(cnt_t.substring(1));
    	}else if(cnt_t.length() > 1){
    		h = 1;
    		m = 0;
    	} else {
    		h = 0;
    		m = 0;
    	}

    	if(m == 0){
    		cnt = h;
    	}else{
    		cnt = h + 1;
    	}
    	
    	return cnt;
    }
    
    private int disColCount1(CW15020002P cW15020002P) {
    	
    	int 	cnt		= 0;
    	String 	start_t = "";
        String 	end_t	= "";
        String 	cnt_t	= "";
        
        int i_start_t	= 0;
        int i_end_t		= 0;
        int h			= 0;
        int m			= 0;
        
        start_t = cW15020002P.getStartHhmm().replace(":", "");
    	end_t 	= "2359";
    	
    	i_start_t	= Integer.parseInt(start_t);
    	i_end_t		= Integer.parseInt(end_t);
    	
    	cnt		= i_end_t - i_start_t;
    	cnt_t	= String.valueOf(cnt);
    	
    	if(cnt_t.length()>3){
    		h = Integer.parseInt(cnt_t.substring(0, 2));
    		m = Integer.parseInt(cnt_t.substring(2));
    	}else if(cnt_t.length() > 2){
    		h = Integer.parseInt(cnt_t.substring(0, 1));
    		m = Integer.parseInt(cnt_t.substring(1));
    	}else if(cnt_t.length() > 1){
    		h = 1;
    		m = 0;
    	} else {
    		h = 0;
    		m = 0;
    	}

    	if(m == 0){
    		cnt = h;
    	}else{
    		cnt = h + 1;
    	}
    	
    	return cnt;
    }
    
    private int disColCount2(CW15020002P cW15020002P) {
    	
    	int 	cnt		= 0;
    	String 	start_t = "";
        String 	end_t	= "";
        String 	cnt_t	= "";
        
        int i_start_t	= 0;
        int i_end_t		= 0;
        int h			= 0;
        int m			= 0;
        
        start_t = "0000";
    	end_t 	= cW15020002P.getEndHhmm().replace(":", "");
    	
    	i_start_t	= Integer.parseInt(start_t);
    	i_end_t		= Integer.parseInt(end_t);
    	
    	cnt		= i_end_t - i_start_t;
    	cnt_t	= String.valueOf(cnt);
    	
    	if(cnt_t.length()>3){
    		h = Integer.parseInt(cnt_t.substring(0, 2));
    		m = Integer.parseInt(cnt_t.substring(2));
    	}else if(cnt_t.length() > 2){
    		h = Integer.parseInt(cnt_t.substring(0, 1));
    		m = Integer.parseInt(cnt_t.substring(1));
    	}else if(cnt_t.length() > 1){
    		h = 1;
    		m = 0;
    	} else {
    		h = 0;
    		m = 0;
    	}

    	if(m == 0){
    		cnt = h;
    	}else{
    		cnt = h + 1;
    	}
    	
    	return cnt;
    }

	//시작일자 체크
    private int getCheckReservationDis(CW15020002P cW15020002P, int checkcnt) {
    	int cnt			= checkcnt;
    	int result		= 0;
    	String col 		= "";
    	String colname 	= "";
    	
    	colname = "T" + cW15020002P.getStartHhmm().substring(0, 2);
    	col 	= colname;
    	
    	for( int i = 1; i < cnt; i++ ){
    		
    		if(colname.length()<4){
          		colname = "," + colname;
          	}
      		colname = String.valueOf(Integer.parseInt(colname.substring(2, 4))+1);
      		if( colname.length() < 2 ){
      			colname = ",T0" + colname;
      		}else{
      			colname = ",T" + colname;
      		}
      		
          	col = col + colname;
    	}

    	cW15020002P.setSearchYmd(cW15020002P.getStartYyyymmdd());
    	cW15020002P.setColname(col);
    	EasyHashMap<String, String> Map = new EasyHashMap<String, String>();
    	Map = mapper.getCheckReservationDis(cW15020002P);
    	
    	for (Map.Entry<String, String> entry : Map.entrySet()) {
            if (!"N".equals(entry.getValue())) {
            	result = 1;
            	break;
            }
    	}
    	return result;
    }
    
    //종료일자 체크
    private int getCheckReservationDis2(CW15020002P cW15020002P) {
    	
    	int cnt			= cW15020002P.getCheck2();
    	int result		= 0;
    	String col 		= "";
    	String colname 	= "";
    	
    	colname = "T00";
    	col 	= colname;
    	
    	for( int i = 1; i < cnt; i++ ){
    		
    		if(colname.length()<4){
          		colname = "," + colname;
          	}
      		colname = String.valueOf(Integer.parseInt(colname.substring(2, 4))+1);
      		if( colname.length() < 2 ){
      			colname = ",T0" + colname;
      		}else{
      			colname = ",T" + colname;
      		}
      		
          	col = col + colname;
    	}
    	cW15020002P.setSearchYmd(cW15020002P.getEndYyyymmdd());
    	cW15020002P.setColname(col);
    	EasyHashMap<String, String> Map = new EasyHashMap<String, String>();
    	Map = mapper.getCheckReservationDis(cW15020002P);
    	
    	for (Map.Entry<String, String> entry : Map.entrySet()) {
            if (!"N".equals(entry.getValue())) {
            	result = 1;
            	break;
            }
    	}
    	return result;
    }
}
