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 ReportOnClaims extends HWPFormat {

	public ReportOnClaims(Print print, FimsPrintOption printOption, List<DataObject> dataObjectList) {
		super(print, printOption, dataObjectList);
		this.maxRunCount = 1;
		this.formatFilePath = "template/hwp/reportOnClaims.hwp";
		this.print.setFormatKorName("채권신고서");
		this.print.setFormatName("reportOnClaims");
	}

	@Override
	public List<String> getDownloadDataNames() {
		List<String> list = new ArrayList<>();
		list.add("과세번호");
		list.add("법정기일");
		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("주소", globalVariable.get("deptAddr"));
		writer.setValue("전화", globalVariable.get("deptTelno"));
		writer.setValue("팩스", globalVariable.get("deptFxno"));
		writer.setValue("담당자", print.getPrintRequestUserInfo().getName());
		writer.setValue("사건번호", EgovStringUtil.null2void(globalVariable.get("caseNo")));
		writer.setValue("체납자성명", EgovStringUtil.null2void(globalVariable.get("rtpyrNm")));
		writer.setValue("체납자번호", EgovStringUtil.null2void(globalVariable.get("rtpyrNo")));
		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));

		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);

		String instNm = (String) print.getPrintRequestUserInfo().getInfo().get("instNm");
		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("FYR"))
			.set("과세번호", one.string("GOJI_NO"))
			.set("법정기일", DataFormat.yyyy_mm_dd(one.string("DUDT_YMD")))
			.set("세목", one.string("TXITM_NM"))
			.set("세액", DataFormat.n_nn0(one.string("LEVY_PCPTAX")))
			.set("가산금", DataFormat.n_nn0(one.string("LEVY_ADAMT")))
			.set("합계", DataFormat.n_nn0(one.string("LAST_AMT")));
			return newObj;
		}).toList();
		writer.table(0, 9, 2).setValues(list);

	}



}
