package cokr.xit.fims.nxrp.support;

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

import org.apache.commons.io.FilenameUtils;

import cokr.xit.fims.nxrp.B01;
import cokr.xit.fims.nxrp.C01;
import cokr.xit.foundation.data.DataObject;

public class OnlyImageMaker extends LinkFileMaker {

	public OnlyImageMaker(String nxrpLinkSeCd, String instDigit7){

		String ifId = "EBF_Z000001LGE_1741000NIS_";
		String systemCode = "";
		if(nxrpLinkSeCd.equals("B01")) {
			ifId += "0002";
			systemCode = "PVS";
			this.maxImageFileCnt = 4;
		} else if(nxrpLinkSeCd.equals("C01")) {
			ifId += "0004";
			systemCode = "BPV";
			this.maxImageFileCnt = 7;
		}
		this.ifId = ifId;

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

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

	@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"));
		b01.set(5, crdnInfo.string("VLTN_CD"));
		b01.set(6, 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(7, crdnSpareaCd);
		b01.set(8, this.gujesiStng);
		b01.set(9, crdnInfo.string("STDG_CD"));
		b01.set(10, crdnInfo.string("GPS_X"));
		b01.set(11, crdnInfo.string("GPS_Y"));

		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"));
		c01.set(5, crdnInfo.string("VLTN_CD"));
		c01.set(6, crdnInfo.string("CRDN_PLC"));
		c01.set(7, this.gujesiStng);
		c01.set(8, crdnInfo.string("STDG_CD"));
		c01.set(9, crdnInfo.string("GPS_X"));
		c01.set(10, crdnInfo.string("GPS_Y"));

		return c01;
	}

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

		String fileExtension = FilenameUtils.getExtension(filePath);

		String delim = "_";
		String newFileName = String.join(delim, items) + delim + fileIndex + "." + 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);
		}

	}

}
