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

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

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cokr.xit.fims.cmmn.OtptArtclStng;
import cokr.xit.fims.cmmn.OtptForm;
import cokr.xit.fims.cmmn.OtptStngQuery;
import cokr.xit.fims.cmmn.dao.OtptStngMapper;
import cokr.xit.fims.cmmn.pdf.print.DefaultOtptArtclStng;
import cokr.xit.fims.cmmn.pdf.print.format.Advntce;
import cokr.xit.fims.cmmn.pdf.print.format.Nht;
import cokr.xit.fims.cmmn.pdf.print.format.PDFPrintFormat;
import cokr.xit.foundation.component.AbstractBean;
import cokr.xit.foundation.data.DataObject;

@Component("otptStngBean")
public class OtptStngBean extends AbstractBean {

	@Resource(name = "otptStngMapper")
	private OtptStngMapper otptStngMapper;

	/**지정한 조건에 따라 출력물 전역 설정을 조회한다.
	 * @param query 조회 조건
	 * @return 출력물 전역 설정
	 */
	public DataObject getOtptBscStng(OtptStngQuery otptStngQuery) {
		return otptStngMapper.selectOtptBscStng(otptStngQuery);
	}

	/**지정한 조건에 따라 출력요소별 설정을 조회한다.
	 * @param query 조회 조건
	 * @return 출력 요소 설정
	 */
	public Map<String, Object> getOtptArtclStngMap(OtptStngQuery query){
		Map<String,Object> otptStngMap = new HashMap<String,Object>();

		PDFPrintFormat pdfFormat = null;
		switch(query.getSndngSeCd()) {
		case "02" :
			pdfFormat = new Advntce();
			break;
		case "03" :
			pdfFormat = new Nht();
			break;

		}


		List<DataObject> otptArtclStngList = otptStngMapper.selectOtptArtclStngList(query);
		if(otptArtclStngList == null) {
			otptArtclStngList = new ArrayList<DataObject>();
		}

		pdfFormat.appendOtptAttribute(otptArtclStngList);

		List<DefaultOtptArtclStng> uniquePrototypeList = pdfFormat.getPrototypeStngs(true);
		List<DefaultOtptArtclStng> multiplePrototypeList = pdfFormat.getPrototypeStngs(false);

		List<DataObject> uniqueDataList = otptArtclStngList.stream().filter(item -> item.string("UNIQUE_YN").equals("Y")).toList();
		List<DataObject> multipleDataList = otptArtclStngList.stream().filter(item -> item.string("UNIQUE_YN").equals("N")).toList();

		Map<String, Object> unique = new HashMap<String,Object>();
		unique.put("prototypeList", uniquePrototypeList);
		unique.put("dataList", uniqueDataList);

		Map<String, Object> multiple = new HashMap<String,Object>();
		multiple.put("prototypeList", multiplePrototypeList);
		multiple.put("dataList", multipleDataList);


		otptStngMap.put("unique", unique);
		otptStngMap.put("multiple", multiple);
		return otptStngMap;
	}

	public boolean saveOtptBscStng(OtptForm otptForm) {

		boolean result = false;
		String userId = currentUser().getId();

		if(ifEmpty(otptForm.getOtptBscId(), ()-> "").equals("")) {
			otptForm.setCreatedBy(userId);
			result = otptStngMapper.insertBscStng(otptForm) == 1;

		} else {
			otptForm.setModifiedBy(userId);
			result = otptStngMapper.updateBscStng(otptForm) == 1;
		}

		return result;
	}

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

			String userId = currentUser().getId();
			List<OtptArtclStng> otptArtclStngList = otptForm.getOtptArtclStngList();

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

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

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

			if(!updateKeyList.isEmpty()) {
				otptForm.setOtptFormIDs(updateKeyList.toArray(new String[updateKeyList.size()]));
			}
			otptForm.setModifiedBy(userId);
			otptStngMapper.deleteArtclStng(otptForm);

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

		return true;
	}


}
