package cokr.xit.fims.nxrp.support;

import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FilenameUtils;

import cokr.xit.base.code.CommonCode;
import cokr.xit.fims.cmmn.CmmnUtil;
import cokr.xit.fims.nxrp.B01;
import cokr.xit.fims.nxrp.C01;
import cokr.xit.foundation.data.DataObject;

public class AttachedTxtMaker extends LinkFileMaker {
	private String innerFilePrefix;
	private String textFileName;

	private List<CommonCode> fimsVltnCodes;
	private List<CommonCode> nxrpVltnCodes;

	public void appendVltnCodeInfo(List<CommonCode> fimsVltnCodes, List<CommonCode> nxrpVltnCodes) {
		this.fimsVltnCodes = fimsVltnCodes;
		this.nxrpVltnCodes = nxrpVltnCodes;
	}

	public AttachedTxtMaker(String nxrpLinkSeCd, String instDigit7){
		String ifId = "EBF_Z000001LGE_1741000NIS_";
		String systemCode = "";
		if(nxrpLinkSeCd.equals("B01")) {
			ifId += "0001";
			systemCode = "PVS";
			maxImageFileCnt = 4;
		} else if(nxrpLinkSeCd.equals("C01")) {
			ifId += "0003";
			systemCode = "BPV";
			maxImageFileCnt = 7;
		}
		this.ifId = ifId;

		SimpleDateFormat ymdhmsFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String currentTime = ymdhmsFormat.format(new Date());

		zipFileName = ifId + "_" + currentTime + "@" + instDigit7 + systemCode + ".zip";

		innerFilePrefix = ifId + "_" + currentTime + "_" + instDigit7 + "_";
		textFileName = innerFilePrefix + "0.txt";
	}

	public String getTextFileName() {
		return textFileName;
	}

	public void writeTextFileContents(String workPath, String... contents) {
		try {
			File txtFile = new File(workPath + "/" + textFileName);
			if(!txtFile.createNewFile()) {
				throw new RuntimeException("[F]파일 생성 오류");
			}

			FileWriter fileWriter = new FileWriter(txtFile);

			for(int i=0; i < contents.length; i++) {
				if(i != 0) {
					fileWriter.write("|");
				}
				fileWriter.write(contents[i]);
			}
			fileWriter.close();
		} catch (Exception e) {
			throw new RuntimeException("[F]"+e);
		}
	}

	@Override
	public B01 extractForPVS(DataObject crdnInfo, boolean includeTxt) {
		B01 b01 = new B01(includeTxt);

		b01.set(1, crdnInfo.string("CRDN_YMD")+crdnInfo.string("CRDN_TM"));
		b01.set(2, crdnInfo.string("VHRNO"));
		String crdnSeCd = crdnInfo.string("CRDN_SE_CD");
		if(!crdnSeCd.equals("01") && !crdnSeCd.equals("02") && !crdnSeCd.equals("07")) {
			throw new RuntimeException("[F]잘못된 단속구분입니다.");
		}
		b01.set(3, crdnSeCd);
		b01.set(4, crdnInfo.string("TEAM_ID"));
		String vltnCd = crdnInfo.string("VLTN_CD");

		boolean vltnCdMatch = false;
		for(CommonCode nxrpVltnCode : this.nxrpVltnCodes) {
			if(nxrpVltnCode.getCode().equals(vltnCd)) {
				vltnCdMatch = true;
			}
		}

		if(vltnCdMatch) {
			b01.set(5, vltnCd);
			b01.set(6, "");
		} else {
			b01.set(5, "99");
			String vltnNm = "";
			for(CommonCode fimsVltnCode : this.fimsVltnCodes) {
				if(fimsVltnCode.getCode().equals(vltnCd)) {
					vltnNm = fimsVltnCode.getValue();
				}
			}
			b01.set(6, vltnNm);
		}

		String law = CmmnUtil.spacedJoin(crdnInfo.string("VLTN_LAW_NM"), crdnInfo.string("VLTN_LAW1"), crdnInfo.string("VLTN_LAW2"));
		b01.set(7, law);
		b01.set(8, crdnInfo.string("CRDN_PLC"));

		String crdnSpareaCd = crdnInfo.string("CRDN_SPAREA_CD");
		if(crdnSpareaCd.equals("00")) {
			crdnSpareaCd = "";
		} else if(crdnSpareaCd.equals("06") || crdnSpareaCd.equals("07")) {
			crdnSpareaCd = "03";
		}
		b01.set(9, crdnSpareaCd);
		b01.set(10, this.gujesiStng);
		b01.set(11, crdnInfo.string("STDG_CD"));
		b01.set(12, crdnInfo.string("GPS_X"));
		b01.set(13, crdnInfo.string("GPS_Y"));
		int atchFileCnt = toInt(crdnInfo.get("ATCH_FILE_CNT"));
		if(atchFileCnt > this.maxImageFileCnt) {
			atchFileCnt = this.maxImageFileCnt;
		}
		b01.set(14, Integer.toString(atchFileCnt));

		return b01;
	}

	@Override
	public C01 extractForBPV(DataObject crdnInfo, boolean includeTxt) {
		C01 c01 = new C01(includeTxt);

		c01.set(1, crdnInfo.string("CRDN_YMD")+crdnInfo.string("CRDN_TM"));
		c01.set(2, crdnInfo.string("VHRNO"));
		String crdnSeCd = crdnInfo.string("CRDN_SE_CD");
		if(!crdnSeCd.equals("01") && !crdnSeCd.equals("02") && !crdnSeCd.equals("07")) {
			throw new RuntimeException("[F]잘못된 단속구분입니다.");
		}
		c01.set(3, crdnSeCd);
		c01.set(4, crdnInfo.string("TEAM_ID"));
		String vltnCd = crdnInfo.string("VLTN_CD");
		c01.set(5, vltnCd);

		String law = CmmnUtil.spacedJoin(crdnInfo.string("VLTN_LAW_NM"), crdnInfo.string("VLTN_LAW1"), crdnInfo.string("VLTN_LAW2"));
		c01.set(6, law);
		c01.set(7, crdnInfo.string("CRDN_PLC"));
		c01.set(8, this.gujesiStng);
		c01.set(9, crdnInfo.string("STDG_CD"));
		c01.set(10, crdnInfo.string("GPS_X"));
		c01.set(11, crdnInfo.string("GPS_Y"));
		c01.set(12, crdnInfo.string("MOSC_X"));
		c01.set(13, crdnInfo.string("MOSC_Y"));

		int atchFileCnt = toInt(crdnInfo.get("ATCH_FILE_CNT"));
		if(atchFileCnt > this.maxImageFileCnt) {
			atchFileCnt = this.maxImageFileCnt;
		}
		c01.set(14, Integer.toString(atchFileCnt));

		return c01;
	}

	@Override
	public void copyFile(String workPath, String filePath, int fileIndex, String... items) {

		String fileExtension = FilenameUtils.getExtension(filePath);

		String newFileName = this.innerFilePrefix + (fileIndex+1) + "." + fileExtension;
		String newPath = workPath + "/" + newFileName;
		try {
			Files.copy(new File(filePath).toPath(), new File(newPath).toPath(), StandardCopyOption.REPLACE_EXISTING);
		} catch (Exception e) {
			throw new RuntimeException("[F]"+e);
		}

	}
}
