package sgc.portal.cw.cw1502.service;

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

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

import sgc.portal.base.util.EasyHashMap;
import sgc.portal.cw.cw1502.dto.CW15020004PSearch;
import sgc.portal.cw.cw1502.model.CW15020004P;
import sgc.portal.cw.cw1502.persistence.CW15020004PMapper;

@Service
public class CW15020004PService {
    private final CW15020004PMapper mapper;

    public CW15020004PService(CW15020004PMapper mapper) {
        this.mapper = mapper;
    }
    
    public EasyHashMap<String, Object> getSelectRemInfo(CW15020004PSearch search) {
        return mapper.getSelectRemInfo(search);
    }

	@Transactional
    public Map<String, String> save(CW15020004P cW15020004P) throws ParseException {
    	
    	Map<String,String> result = new HashMap<>();

        Date format1 = new SimpleDateFormat("yyyyMMdd").parse(cW15020004P.getStartYyyymmdd());
        Date format2 = new SimpleDateFormat("yyyyMMdd").parse(cW15020004P.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());
        
    	updateRes(cW15020004P);	//예약테이블 종료 업데이트

    	if(diffDays > 1) {

            cW15020004P.setSearchYmd1(format3); // 시작일자 +1일
            cW15020004P.setSearchYmd2(format4); // 종료일자 -1일
            
        	cW15020004P.setCheck1(disColCount1(cW15020004P));//시작일자 디스플레이 컬럼수
        	cW15020004P.setCheck2(disColCount2(cW15020004P));//종료일자 디스플레이 컬럼수

        	updateDis(cW15020004P, cW15020004P.getCheck1());//시작일자
    		updateDis2(cW15020004P);						//종료일자
    		updateDis3(cW15020004P);						//중간일자
    		
    	} else if(diffDays > 0) {

        	cW15020004P.setCheck1(disColCount1(cW15020004P));//시작일자 디스플레이 컬럼수
        	cW15020004P.setCheck2(disColCount2(cW15020004P));//종료일자 디스플레이 컬럼수
        	
        	updateDis(cW15020004P, cW15020004P.getCheck1());//시작일자
    		updateDis2(cW15020004P);						//종료일자
        	
    	} else if(diffDays == 0) {
    		
    		cW15020004P.setCheck(disColCount(cW15020004P));	//디스플레이 컬럼수
    		
    		updateDis(cW15020004P, cW15020004P.getCheck());//디스플레이테이블
    		
    	}
        return result;
    }
    
    private void updateRes(CW15020004P cW15020004P) {
        mapper.updateRes(cW15020004P);
    }
    
    private void updateDis(CW15020004P cW15020004P, int checkCnt) {
    	
    	int cnt			= checkCnt;
    	String col 		= "";
    	String colname 	= "";
    	
    	colname = "T" + cW15020004P.getStartHhmm().substring(0, 2);
    	col 	= colname + " = 'N'";
    	
    	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 + " = 'N'";
    	}
    	cW15020004P.setColname(col);
    	
        mapper.updateDis(cW15020004P);
    }
    
    private void updateDis2(CW15020004P cW15020004P) {

    	int cnt			= cW15020004P.getCheck2();
    	String col 		= "";
    	String colname 	= "";
    	
    	colname = "T00";
    	col 	= colname + " = 'N'";
    	
    	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 + " = 'N'";
    	}
    	cW15020004P.setColname(col);
    	
        mapper.updateDis2(cW15020004P);
    }
    
    private void updateDis3(CW15020004P cW15020004P) {
        mapper.updateDis3(cW15020004P);
    }
    
    private int disColCount(CW15020004P cW15020004P) {

    	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 = cW15020004P.getStartHhmm().replace(":", "");
    	end_t 	= cW15020004P.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(CW15020004P cW15020004P) {

    	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 = cW15020004P.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(CW15020004P cW15020004P) {

    	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 	= cW15020004P.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;
    }
}
