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.ExtrArtclStng;
import cokr.xit.fims.cmmn.ExtrForm;
import cokr.xit.fims.cmmn.ExtrStngQuery;
import cokr.xit.fims.cmmn.PredefinedSet;
import cokr.xit.fims.cmmn.dao.ExtrStngMapper;
import cokr.xit.fims.cmmn.pdf.extraction.format.ElectronicNotice;
import cokr.xit.fims.cmmn.pdf.extraction.format.PDFExtractFormat;
import cokr.xit.foundation.component.AbstractBean;
import cokr.xit.foundation.data.DataObject;

@Component("extrStngBean")
public class ExtrStngBean extends AbstractBean {
	@Resource(name = "extrStngMapper")
	private ExtrStngMapper extrStngMapper;

	/**지정한 조건에 따라 자료 추출 템플릿 문구를 조회한다.
	 * @param query 조회 조건
	 * @return 출력물 전역 설정
	 */
	public DataObject getExtrBscStng(ExtrStngQuery extrStngQuery) {
		return extrStngMapper.selectExtrBscStng(extrStngQuery);
	}

	/**지정한 조건에 따라 추출요소별 설정을 조회한다.
	 * @param query 조회 조건
	 * @return 출력 요소 설정
	 */
	public Map<String, Object> getExtrArtclStngMap(ExtrStngQuery query){
		List<DataObject> extrArtclStngList = ifEmpty(extrStngMapper.selectExtrArtclStngList(query), ArrayList::new);
		PDFExtractFormat pdfFormat = new ElectronicNotice();
		pdfFormat.appendPredefinedYn(extrArtclStngList);
		List<PredefinedSet> predefinedSetList = pdfFormat.getPredefinedSetList();

		List<DataObject> predefinedDataList = extrArtclStngList.stream().filter(item -> item.string("PREDEFINED_YN").equals("Y")).toList();
		List<DataObject> customDataList = extrArtclStngList.stream().filter(item -> item.string("PREDEFINED_YN").equals("N")).toList();

		Map<String, Object> predefined = new HashMap<String,Object>();
		predefined.put("dataList", predefinedDataList);

		Map<String, Object> custom = new HashMap<String,Object>();
		custom.put("dataList", customDataList);

		return Map.of(
			"predefinedSetList", predefinedSetList,
			"predefined", predefined,
			"custom", custom
		);
	}

	public boolean saveExtrBscStng(ExtrForm extrForm) {
		String userId = currentUser().getId();

		if(isEmpty(extrForm.getExtrBscId())) {
			extrForm.setCreatedBy(userId);
			return extrStngMapper.insertBscStng(extrForm) == 1;

		} else {
			extrForm.setModifiedBy(userId);
			return extrStngMapper.updateBscStng(extrForm) == 1;
		}
	}

	public boolean saveExtrArtclStng(ExtrForm extrForm) {
		try {
			String userId = currentUser().getId();
			List<String> updateKeyList = new ArrayList<String>();
			List<ExtrArtclStng> insertList = new ArrayList<ExtrArtclStng>();

			List<ExtrArtclStng> extrArtclStngList = extrForm.getExtrArtclStngList();
			if(!isEmpty(extrArtclStngList)) {
				List<ExtrArtclStng> updateList = extrArtclStngList.stream()
					.filter(item -> !isEmpty(item.getExtrFormId()))
					.toList();
				if(!updateList.isEmpty()) {
					for(ExtrArtclStng update : updateList) {
						update.setModifiedBy(userId);
						extrStngMapper.updateArtclStng(update);
						updateKeyList.add(update.getExtrFormId());
					}
				}
				insertList = extrArtclStngList.stream().filter(item -> ifEmpty(item.getExtrFormId(), () -> "").equals("")).toList();
			}

			if(!updateKeyList.isEmpty()) {
				extrForm.setExtrFormIDs(updateKeyList.toArray(new String[updateKeyList.size()]));
			}

			extrForm.setModifiedBy(userId);
			extrStngMapper.deleteArtclStng(extrForm);

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

		return true;
	}
}