package com.interplug.qcast.biz.canvasBasicSetting;

import com.interplug.qcast.biz.canvasBasicSetting.dto.CanvasBasicSettingInfo;
import com.interplug.qcast.biz.canvasBasicSetting.dto.CanvasBasicSettingResponse;
import com.interplug.qcast.biz.canvasBasicSetting.dto.RoofAllocationInfo;
import com.interplug.qcast.biz.canvasBasicSetting.dto.RoofAllocationSettingInfo;
import com.interplug.qcast.biz.canvasBasicSetting.dto.RoofMaterialsAddInfo;
import com.interplug.qcast.config.Exception.ErrorCode;
import com.interplug.qcast.config.Exception.QcastException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class CanvasBasicSettingService {
  private final CanvasBasicSettingMapper canvasBasicSettingMapper;

//Canvas Basic Setting 조회(objectNo)
	public List<CanvasBasicSettingResponse> selectCanvasBasicSetting(String objectNo, Integer planNo) throws QcastException {		
		try {
			return canvasBasicSettingMapper.selectCanvasBasicSetting(objectNo, planNo);
		} catch (Exception e) {
			throw new QcastException(ErrorCode.INTERNAL_SERVER_ERROR, e.getMessage());
		}
	}

	// Canvas Basic Setting 등록
	public Map<String, String> insertCanvasBasicSetting(CanvasBasicSettingInfo csi) throws QcastException {
		
		Map<String, String> response = new HashMap<>();
		
		if (csi.getObjectNo() == null && csi.getPlanNo() == null) {			
			throw new QcastException (ErrorCode.INVALID_INPUT_VALUE ,"올바르지 않은 입력값입니다.");
		}

		try {
			
			// 먼저 데이터가 존재하는지 확인
			CanvasBasicSettingInfo cntData = canvasBasicSettingMapper.getCanvasBasicSettingCnt(csi.getObjectNo(), csi.getPlanNo());
			
			log.debug("cntData ::::: " + cntData);
			
			// 데이터가 존재하지 않으면 insert
			if (cntData.getRoofCnt().intValue() < 1) {
				// 도면/치수/각도 정보 insert
				canvasBasicSettingMapper.insertCanvasBasicSetting(csi);
				
				// for-each 루프를 사용하여 지붕재추가 Setting
		        for (RoofMaterialsAddInfo rma : csi.getRoofMaterialsAddList()) {
		        	
		        	rma.setObjectNo(csi.getObjectNo());
		        	rma.setPlanNo(csi.getPlanNo());
		        	// 신규 지붕재추가 정보 insert
		            canvasBasicSettingMapper.insertRoofMaterialsAdd(rma);
		        }				
				response.put("objectNo", csi.getObjectNo());
				response.put("returnMessage", "common.message.confirm.mark");
			} else {
				// 도면/치수/각도 정보 update
				canvasBasicSettingMapper.updateCanvasBasicSetting(csi);
				
				// for-each 루프를 사용하여 지붕재추가 Setting
		        for (RoofMaterialsAddInfo rma : csi.getRoofMaterialsAddList()) {
		        	
		        	rma.setObjectNo(csi.getObjectNo());
		        	rma.setPlanNo(csi.getPlanNo());
		        	// 신규 지붕재추가 정보 insert
		            canvasBasicSettingMapper.updateRoofMaterialsAdd(rma);
		        }				
				response.put("objectNo", csi.getObjectNo());
				response.put("returnMessage", "common.message.confirm.mark");
			}			
		} catch (Exception e) {
		      throw new QcastException(ErrorCode.INTERNAL_SERVER_ERROR, e.getMessage());
		}

		// 생성된 objectNo 반환
		return response;
	}
	
	// Canvas Basic Setting 삭제
	public void deleteCanvasBasicSetting(String objectNo, Integer planNo) throws QcastException {
		
		if ((objectNo == null || objectNo.trim().isEmpty()) && (planNo == null)) {
			throw new QcastException (ErrorCode.INVALID_INPUT_VALUE ,"올바르지 않은 입력값입니다.");
		}
		
		try {
			// Canvas Basic Setting 정보 삭제
			canvasBasicSettingMapper.deleteCanvasBasicSetting(objectNo, planNo);				
			// 지붕재추가 정보 삭제
			canvasBasicSettingMapper.deleteRoofMaterialsAdd(objectNo, planNo);
		} catch (Exception e) {
	      if (e instanceof QcastException) throw e;
	      throw new QcastException(ErrorCode.INTERNAL_SERVER_ERROR, e.getMessage());
	    }
		
	}
	
	// 지붕면 할당 Setting 등록
	public Map<String, String> insertRoofAllocSetting(RoofAllocationSettingInfo rasi) throws QcastException {
		
		Map<String, String> response = new HashMap<>();
		 
		if (rasi.getObjectNo() == null && rasi.getPlanNo() == null) {			
			throw new QcastException (ErrorCode.INVALID_INPUT_VALUE ,"올바르지 않은 입력값입니다.");
		}

		try {
			
			// 기존 지붕재추가 정보 삭제 후 insert
			canvasBasicSettingMapper.deleteRoofMaterialsAdd(rasi.getObjectNo(), rasi.getPlanNo());
			
			// for-each 루프를 사용하여 지붕재추가 Setting
	        for (RoofAllocationInfo rai : rasi.getRoofAllocationList()) {
	        	rai.setObjectNo(rasi.getObjectNo());
	        	rai.setPlanNo(rasi.getPlanNo());
	            canvasBasicSettingMapper.insertRoofAllocation(rai);
	        }
	        response.put("objectNo", rasi.getObjectNo());
	        response.put("returnMessage", "common.message.confirm.mark");
		} catch (Exception e) {
		      throw new QcastException(ErrorCode.INTERNAL_SERVER_ERROR, e.getMessage());
		}

		// 생성된 objectNo 반환
		return response;

	}
	
	// 지붕재추가 삭제
	public void deleteRoofMaterialsAdd(String objectNo, Integer planNo) throws QcastException {
		
		if ((objectNo == null || objectNo.trim().isEmpty()) && (planNo == null)) {
			throw new QcastException (ErrorCode.INVALID_INPUT_VALUE ,"올바르지 않은 입력값입니다.");
		}
		
		try {
			canvasBasicSettingMapper.deleteRoofMaterialsAdd(objectNo, planNo);
		} catch (Exception e) {
	      if (e instanceof QcastException) throw e;
	      throw new QcastException(ErrorCode.INTERNAL_SERVER_ERROR, e.getMessage());
	    }
	}
}
