package cokr.xit.fims.sprt.service.bean;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.assertj.core.util.Arrays;
import org.springframework.stereotype.Component;

import cokr.xit.applib.PstnAndSize;
import cokr.xit.base.file.service.FileQuery;
import cokr.xit.base.file.service.bean.FileBean;
import cokr.xit.fims.crdn.dao.CrdnInfoMapper;
import cokr.xit.fims.sprt.MediaUtil;
import cokr.xit.fims.sprt.Sprt;
import cokr.xit.fims.sprt.dao.MediaMapper;
import cokr.xit.foundation.component.AbstractBean;
import cokr.xit.foundation.data.DataObject;

/**미디어 업무지원 서비스 인터페이스의 구현체
 * @author leebj
 */
@Component("mediaBean")
public class MediaBean extends AbstractBean {

	@Resource(name = "crdnInfoMapper")
	private CrdnInfoMapper crdnInfoMapper;

	@Resource(name = "mediaMapper")
	private MediaMapper mediaMapper;

	@Resource(name="fileBean")
	private FileBean fileBean;


	public DataObject getVideoInfo(String crdnId) {
		DataObject fileInfo = new DataObject();

		DataObject cvlcptInfo = crdnInfoMapper.selectCrdnInfo(crdnId);
		String linkTblNm = cvlcptInfo.string("LINK_TBL_NM");
		if(!linkTblNm.equals("")) {
			FileQuery fileQuery = new FileQuery();

			if(linkTblNm.equals("TB_ESB_INTERFACE")) {
				fileQuery.setInfoType("010");
			} else if(linkTblNm.equals("TB_SAEOL_CVLCPT_DTL")) {
				fileQuery.setInfoType("020");
			} else {
				throw new RuntimeException("파일 조회 중 오류가 발생하였습니다.");
			}
			fileQuery.setInfoKeys(cvlcptInfo.string("LINK_ID"));

			List<DataObject> fileList = fileBean.getFileList(fileQuery);
			if(fileList != null && !fileList.isEmpty()) {
				for(int i=0; i < fileList.size() ;i++) {
					DataObject file = fileList.get(i);
					String mimeType = file.string("MIME_TYPE");
					if(Arrays.asList(Sprt.VIDEO_MIME_TYPE).contains(mimeType)){
						fileInfo = file;
						break;
					}
				}
			}
		}

		return fileInfo;
	}

	public List<DataObject> getFileAndMosaic(FileQuery fileQuery){

		List<DataObject> fileList = fileBean.getFileList(fileQuery);
		for(DataObject fileInfo : fileList) {
    		List<DataObject> mosaicInfos = this.getMosaicList(fileInfo.string("FILE_ID"));
    		if(mosaicInfos != null) {
    			String mosaic = "";
    			for(int i=0; i < mosaicInfos.size(); i++) {
    				DataObject mosaicInfo = mosaicInfos.get(i);
    				if(i != 0) {
    					mosaic += "|";
    				}
    				mosaic += mosaicInfo.string("X_AXS")
    						+ "," + mosaicInfo.string("Y_AXS")
    						+ "," + mosaicInfo.string("MOSC_LEN")
    						+ "," + mosaicInfo.string("MOSC_HGT");
    			}
    			fileInfo.set("MOSAIC", mosaic);
    		} else {
    			fileInfo.set("MOSAIC", "");
    		}

    	}

		return fileList;
	}

	/**파일의 모자이크 정보를 조회한다.<br />
	 * @param fileId 파일ID
	 * @return 모자이크 정보
	 */
	public List<DataObject> getMosaicList(String fileId){
		return mediaMapper.selectMosaicList(fileId);
	}

	public List<PstnAndSize> getTransformedMosaic(String imageKey, InputStream inputStream, PstnAndSize pstnAndSize_in_print) {
		List<DataObject> mosaicInfos = this.getMosaicList(imageKey);
		return MediaUtil.getTransformedMosaic(mosaicInfos, inputStream, pstnAndSize_in_print);


	}

	public int reorder(String fileId, int i) {
		Map<String, Object> params = new HashMap<>();
		params.put("fileId", fileId);
		params.put("srtOrd", i);
		return mediaMapper.reorder(params);
	}

	public void copyMosaicInfo(String oldFileId, String newFileId) {

		DataObject param = new DataObject().set("currentUser", currentUser());
		param.put("oldFileId", oldFileId);
		param.put("newFileId", newFileId);

		List<DataObject> mosaicInfos = this.getMosaicList(oldFileId);
		for(DataObject mosaicInfo : mosaicInfos) {
			param.set("oldMoscId", mosaicInfo.string("MOSC_ID"));
			mediaMapper.copyMosaicInfo(param);
		}
	}
}
