package cokr.xit.fims.cmmn.service.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cokr.xit.fims.cmmn.OutsourcingArtclStng;
import cokr.xit.fims.cmmn.OutsourcingFileMaker;
import cokr.xit.fims.cmmn.OutsourcingForm;
import cokr.xit.fims.cmmn.OutsourcingStngQuery;
import cokr.xit.fims.cmmn.PredefinedSet;
import cokr.xit.fims.cmmn.dao.OutsourcingStngMapper;
import cokr.xit.foundation.component.AbstractBean;
import cokr.xit.foundation.data.DataObject;

@Component("outsourcingStngBean")
public class OutsourcingStngBean extends AbstractBean {

	@Resource(name = "outsourcingStngMapper")
	private OutsourcingStngMapper outsourcingStngMapper;


	public List<DataObject> getArtclStngList(OutsourcingStngQuery query){
		return outsourcingStngMapper.selectArtclStngList(query);
	}


	public Map<String, Object> getArtclStngMap(OutsourcingStngQuery query) {
		Map<String,Object> outsourcingStngMap = new HashMap<String,Object>();

		List<PredefinedSet> predefinedSetList = OutsourcingFileMaker.predifinedSetForOutsourcing();
		outsourcingStngMap.put("predefinedSetList", OutsourcingFileMaker.predifinedSetForOutsourcing());

		//
		List<DataObject> outsourcingArtclStngList = outsourcingStngMapper.selectArtclStngList(query);
		if(outsourcingArtclStngList == null) {
			outsourcingArtclStngList = new ArrayList<DataObject>();
		}

		for(DataObject outsourcingArtclStng : outsourcingArtclStngList) {
			String outsourcingArtclNm = (String) outsourcingArtclStng.get("ARTCL_NM");

			Optional<PredefinedSet> optional = predefinedSetList.stream()
					.filter(item -> item.getArtclNm().equals(outsourcingArtclNm)).findFirst();

			if(optional.isEmpty()) {
				outsourcingArtclStng.set("DSCRP", outsourcingArtclStng.get("ARTCL_NM"));
			} else {
				PredefinedSet predefinedSet = optional.get();
				outsourcingArtclStng.set("DSCRP", predefinedSet.getArtclDscrp());
			}

		}
		outsourcingStngMap.put("dataList", outsourcingArtclStngList);



		return outsourcingStngMap;
	}

	/** 출력 요소 설정을 저장 및 삭제 처리한다.
	 * @param outsourcingForm 출력 설정
	 * @return 저장여부
	 */
	public boolean saveOutsourcingArtclStng(OutsourcingForm outsourcingForm) {
		try {

			String userId = currentUser().getId();
			List<OutsourcingArtclStng> outsourcingArtclStngList = outsourcingForm.getZipArtclStngList();

			List<String> updateKeyList = new ArrayList<String>();
			List<OutsourcingArtclStng> insertList = new ArrayList<OutsourcingArtclStng>();

			if(outsourcingArtclStngList != null && !outsourcingArtclStngList.isEmpty()) {

				List<OutsourcingArtclStng> updateList = outsourcingArtclStngList.stream().filter(item -> !ifEmpty(item.getOutsourcingFormId(), () -> "").equals("")).toList();
				if(!updateList.isEmpty()) {
					for(OutsourcingArtclStng update : updateList) {
						update.setModifiedBy(userId);
						outsourcingStngMapper.updateArtclStng(update);
						updateKeyList.add(update.getOutsourcingFormId());
					}
				}
				insertList = outsourcingArtclStngList.stream().filter(item -> ifEmpty(item.getOutsourcingFormId(), () -> "").equals("")).toList();
			}

			if(!updateKeyList.isEmpty()) {
				outsourcingForm.setOutsourcingFormIDs(updateKeyList.toArray(new String[updateKeyList.size()]));
			}
			outsourcingForm.setModifiedBy(userId);
			outsourcingStngMapper.deleteArtclStng(outsourcingForm);

			if(!insertList.isEmpty()) {
				for(OutsourcingArtclStng insert : insertList) {
					insert.setCreatedBy(userId);
					outsourcingStngMapper.insertArtclStng(insert);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException("출력 설정 저장 중 오류가 발생하였습니다.");
		}

		return true;
	}
}
