package cokr.xit.fims.cmmn.hwp.format;

import java.awt.Rectangle;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.stream.IntStream;

import org.egovframe.rte.fdl.string.EgovStringUtil;

import cokr.xit.applib.HttpStatusCodeException;
import cokr.xit.applib.Print;
import cokr.xit.applib.hwp.OffcsCellFinder;
import cokr.xit.applib.hwp.format.HWPFormat;
import cokr.xit.fims.cmmn.CmmnUtil;
import cokr.xit.fims.cmmn.FimsPrintOption;
import cokr.xit.fims.mngt.OgdpUtil;
import cokr.xit.foundation.data.DataFormat;
import cokr.xit.foundation.data.DataObject;
import kr.dogfoot.hwplib.object.bodytext.control.table.Cell;
import kr.dogfoot.hwplib.object.bodytext.paragraph.Paragraph;

/**
 * 교부청구서
 */
public class RequestForDelivery extends HWPFormat {

	public RequestForDelivery(Print print, FimsPrintOption printOption, List<DataObject> dataObjectList) {
		super(print, printOption, dataObjectList);
		this.maxRunCount = 1;
		this.formatFilePath = "template/hwp/requestForDelivery.hwp";
		this.print.setFormatKorName("교부청구서");
		this.print.setFormatName("requestForDelivery");
	}

	@Override
	public List<String> getDownloadDataNames() {
		List<String> list = new ArrayList<>();
		list.add("징수과목");
		list.add("고지번호");
		list.add("과태료계");
		list.add("법정기일");
		return list;
	}




	@Override
	public void runAsWriter() {

		Map<String,String> globalVariable = printOption.getGlobalVariable();

		String offcsFilePath = EgovStringUtil.null2void(globalVariable.get("offcsFilePath"));
		if(!offcsFilePath.isEmpty()) {
			File offcsFile = new File(offcsFilePath);
			if(!offcsFile.exists()) {
				throw new HttpStatusCodeException(500, "HWP파일 생성 중 오류가 발생하였습니다.\n직인 파일을 찾을 수 없습니다.");
			}
			BiConsumer<Paragraph, Rectangle> bc = writer.image().add(offcsFilePath);

			Cell cell = OffcsCellFinder.find(writer);
			if(cell != null) {
				Paragraph para = cell.getParagraphList().getParagraph(0);
				int width = (int)cell.getListHeader().getWidth(),
					height = (int)cell.getListHeader().getHeight();
				bc.accept(para, new Rectangle(1, 1, width, height));
			}

		}


		writer.setValue("문서번호", EgovStringUtil.null2void(globalVariable.get("docNo")));
		writer.setValue("사건번호", EgovStringUtil.null2void(globalVariable.get("caseNo")));
		writer.setValue("수신", EgovStringUtil.null2void(globalVariable.get("receiver")));
		writer.setValue("체납자성명", EgovStringUtil.null2void(globalVariable.get("rtpyrNm")));
		writer.setValue("체납자번호", EgovStringUtil.null2void(globalVariable.get("rtpyrNo")));
		writer.setValue("차량번호", EgovStringUtil.null2void(globalVariable.get("vhrno")));
		writer.setValue("주소", EgovStringUtil.null2void(globalVariable.get("rtpyrFullAddr")));

		int sumOfList = data.stream().mapToInt(item -> item.number("LAST_AMT").intValue()).sum();
		writer.setValue("합계금액", DataFormat.n_nn0(sumOfList));
		writer.setValue("교부청구금액", DataFormat.n_nn0(sumOfList));
		writer.setValue("교부청구금액한글", CmmnUtil.convertNumberToHangul(sumOfList));
		writer.setValue("개설은행", EgovStringUtil.null2void(globalVariable.get("dtbnBankNm")));
		writer.setValue("계좌번호", EgovStringUtil.null2void(globalVariable.get("dtbnActno")));

		String instNm = (String) print.getPrintRequestUserInfo().getInfo().get("instNm");
		String deptNm = (String) print.getPrintRequestUserInfo().getInfo().get("deptNm");
		writer.setValue("예금주", instNm + " " + deptNm);

		String printRequestYmd = print.getPrintRequestDt().substring(0, 8);
		String yyyy = printRequestYmd.substring(0, 4);
		String mm = printRequestYmd.substring(4, 6);
		String dd = printRequestYmd.substring(6, 8);
		String ymd = yyyy+"년 "+mm+"월 "+dd+"일";
		writer.setValue("연월일", ymd);

		instNm = OgdpUtil.getLastWord(instNm);
		instNm = OgdpUtil.getInstituteLeader(instNm, false);
		writer.setValue("발신", instNm);

		List<DataObject> list = IntStream.rangeClosed(1, this.data.size()).boxed().map(i -> {
			DataObject one = data.get(i-1);
			DataObject newObj = (DataObject) new DataObject()
			.set("징수과목", one.string("TXITM_NM"))
			.set("고지번호", one.string("GOJI_NO"))
			.set("과태료계", DataFormat.n_nn0(one.string("LAST_AMT")))
			.set("법정기일", DataFormat.yyyy_mm_dd(one.string("DUDT_YMD")))
			;
			return newObj;
		}).toList();
		writer.table(0, 9, 1).setValues(list);

	}



}
