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.CW08040003P;
import sgc.portal.cw.cw0804.persistence.CW08040003PMapper;

import java.util.HashMap;
import java.util.Map;

@Service
public class CW08040003PService {
    private final CW08040003PMapper mapper;

    public CW08040003PService(CW08040003PMapper mapper) {
        this.mapper = mapper;
    }

    @Transactional
    public Map<String, String> save(CW08040003P cW08040003P) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	cW08040003P.setCheck(disColCount(cW08040003P));	//디스플레이 컬럼수

    	if(mapper.getCheckReservation(cW08040003P) == 0) {
    		if(getCheckReservationDis(cW08040003P) == 0) {
    			if(mapper.getCheckReservation(cW08040003P) == 0) {
    				insert(cW08040003P);
    				if(getCheckReservationDis(cW08040003P) == 0) {
    					updateDis(cW08040003P);
    					insertHis(cW08040003P);
    					//result.put("result", "예약 되었습니다.");
    				} else {
    					throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
    				}
    			} else {
    				throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
    			}
    		} else {
    			throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
    		}
    	} else {
    		throw new BusinessException("예약이  중복됩니다. 예약현황을 확인하세요.");
    	};
    	
        return result;
    }

    private void insert(CW08040003P cW08040003P) {
        mapper.insert(cW08040003P);
    }
    
    private void updateDis(CW08040003P cW08040003P) {
    	
    	int cnt			= cW08040003P.getCheck();
    	String col 		= "";
    	String colname 	= "";
    	String reinfo 	= mapper.getReservationDis(cW08040003P);
    	
    	colname = "T" + cW08040003P.getStartHhmm().substring(0, 3);
    	col 	= colname + " = '" + reinfo + "'";
    	
    	for( int i = 1; i < cnt; i++ ){
        	//컬럼명 변경
        	if(colname.length()<5){
          		colname = "," + colname;
          	}
          	if(colname.substring(4).equals("0")){
          		colname = colname.substring(0, 4) + "3";
          	}else{
          		colname = String.valueOf(Integer.parseInt(colname.substring(2, 4))+1)+"0";
          		if( colname.length() < 3 ){
          			colname = ",T0" + colname;
          		}else{
          			colname = ",T" + colname;
          		}
          	}
        	col = col + colname + " = '" + reinfo + "'";
    	}
    	cW08040003P.setColname(col);
    	
        mapper.updateDis(cW08040003P);
    }
    
    private void insertHis(CW08040003P cW08040003P) {
        mapper.insertHis(cW08040003P);
    }
    
    private int disColCount(CW08040003P cW08040003P) {
    	
    	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 = cW08040003P.getStartHhmm().replace(":", "");
    	end_t 	= cW08040003P.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){
    		m = Integer.parseInt(cnt_t);
    	}
    	
    	if(m == 0){
    		cnt = h * 2;
    	}else{
    		cnt = (h * 2) + 1;
    	}
    	
    	return cnt;
    }
    
    private int getCheckReservationDis(CW08040003P cW08040003P) {
    	
    	int cnt			= cW08040003P.getCheck();
    	int result		= 0;
    	String col 		= "";
    	String colname 	= "";
    	
    	colname = "T" + cW08040003P.getStartHhmm().substring(0, 3);
    	col 	= colname;
    	
    	for( int i = 1; i < cnt; i++ ){
    		
    		if(colname.length()<5){
          		colname = "," + colname;
          	}
          	if(colname.substring(4).equals("0")){
          		colname = colname.substring(0, 4) + "3";
          	}else{
          		colname = String.valueOf(Integer.parseInt(colname.substring(2, 4))+1)+"0";
          		if( colname.length() < 3 ){
          			colname = ",T0" + colname;
          		}else{
          			colname = ",T" + colname;
          		}
          	}
          	col = col + colname;
    	}
    	
    	cW08040003P.setColname(col);
    	EasyHashMap<String, String> Map = new EasyHashMap<String, String>();
    	Map = mapper.getCheckReservationDis(cW08040003P);
    	
    	for (Map.Entry<String, String> entry : Map.entrySet()) {
            if (!"N".equals(entry.getValue())) {
            	result = 1;
            	break;
            }
    	}
    	return result;
    }
}
