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

import java.util.ArrayList;
import java.util.List;

import cokr.xit.applib.Print;
import cokr.xit.applib.hwp.format.HWPFormat;
import cokr.xit.base.docs.hwp.HWPWriter;
import cokr.xit.fims.cmmn.CmmnUtil;
import cokr.xit.fims.cmmn.FimsPrintOption;
import cokr.xit.foundation.data.DataFormat;
import cokr.xit.foundation.data.DataObject;
import kr.dogfoot.hwplib.object.HWPFile;


public class CvlcptOrgn extends HWPFormat {
	private List<DataObject> data2 = null;

	public CvlcptOrgn(Print print, FimsPrintOption printOption, List<DataObject> dataObjectList) {
		super(print, printOption, dataObjectList);
		this.maxRunCount = 1;
		this.formatFilePath = "template/hwp/cvlcptOrgnl_text.hwp";
		this.print.setFormatKorName("민원원본내역");
		this.print.setFormatName("cvlcptOrgnl");

		this.data2 = new ArrayList<DataObject>();
		if(!dataObjectList.get(0).string("PHOTO0").equals("")) {

			int photoCnt = dataObjectList.get(0).number("ORGN_PHOTO_CNT").intValue();
			int PHOTO_COUNT_PER_PAGE = 4;

			for(int i=0; i < photoCnt; i += PHOTO_COUNT_PER_PAGE) {

				DataObject photoObj = new DataObject();

				photoObj.set("PHOTO1", dataObjectList.get(0).string("PHOTO"+i));
				if(i+1 < photoCnt)
					photoObj.set("PHOTO2", dataObjectList.get(0).string("PHOTO"+(i+1)));
				if(i+2 < photoCnt)
					photoObj.set("PHOTO3", dataObjectList.get(0).string("PHOTO"+(i+2)));
				if(i+3 < photoCnt)
					photoObj.set("PHOTO4", dataObjectList.get(0).string("PHOTO"+(i+3)));

				data2.add(photoObj);
			}
		}
	}

	@Override
	public List<String> getDownloadDataNames() {
		return List.of("접수번호","신청자","접수일자","처리기한","민원목록번호","민원내용");
	}

	@Override
	public HWPFormat makeFile(){
		HWPFile hwpFile = HWPWriter.classpath(this.formatFilePath);
		this.writer = new HWPWriter(hwpFile);

		runAsWriter();
		String tempPath = this.print.pathForNewFile();
		writer.write(tempPath);
		resultFilesPath.add(tempPath);

		writer = null;
		currentRunCount++;

		if(data2 != null && data2.size() > 0) {

			formatFilePath = "template/hwp/cvlcptOrgnl_photo.hwp";
			maxRunCount = data2.size()+1;

			while(currentRunCount != maxRunCount) {
				HWPFile hwpFile_i = HWPWriter.classpath(this.formatFilePath);
				this.writer = new HWPWriter(hwpFile_i);
				runAsWriter();
				String tempPath_i = this.print.pathForNewFile();
				writer.write(tempPath_i);
				resultFilesPath.add(tempPath_i);

				writer = null;
				currentRunCount++;
			}
		}

		this.merge();

		return this;
	}



	@Override
	public void runAsWriter() {

		if(this.formatFilePath.equals("template/hwp/cvlcptOrgnl_text.hwp")) {

			DataObject one = data.get(0);

			writer.setValue("신청자", one.string("CVLCPT_APLCNT_NM"));
			writer.setValue("접수일자", DataFormat.yyyy_mm_dd(one.string("CVLCPT_RCPT_YMD")));

			String cvlcptRrcsPrnmntDt = one.string("CVLCPT_PRCS_PRNMNT_DT");
			if(cvlcptRrcsPrnmntDt.length() > 8) {
				cvlcptRrcsPrnmntDt = cvlcptRrcsPrnmntDt.substring(0, 8);
			}

			writer.setValue("처리기한", DataFormat.yyyy_mm_dd(cvlcptRrcsPrnmntDt));
			writer.setValue("접수번호", one.string("CVLCPT_RCPT_NO"));
			writer.setValue("목록번호", one.string("CVLCPT_LIST_NO"));

			String cvlcptAplyCn = one.string("CVLCPT_APLY_CN");
			cvlcptAplyCn = CmmnUtil.escapeHTMLEntity(cvlcptAplyCn);
			writer.setValue("민원내용", cvlcptAplyCn);

		} else {

			DataObject one = data2.get(currentRunCount-1);
			DataObject newObj = new DataObject();
			if(!one.string("PHOTO1").equals("")) {
				newObj.set("왼쪽위사진", writer.image().add(one.string("PHOTO1")));
			}
			if(!one.string("PHOTO2").equals("")) {
				newObj.set("오른쪽위사진", writer.image().add(one.string("PHOTO2")));
			}
			if(!one.string("PHOTO3").equals("")) {
				newObj.set("왼쪽아래사진", writer.image().add(one.string("PHOTO3")));
			}
			if(!one.string("PHOTO4").equals("")) {
				newObj.set("오른쪽아래사진", writer.image().add(one.string("PHOTO4")));
			}
			writer.table(0, 0, 2).setValues(List.of(newObj));

		}

	}



}
