package com.platform.modules.qk.service.impl;

import com.platform.modules.plm.entity.PlmVideoNewsEntity;
import com.platform.modules.plm.service.PlmMaterialService;
import com.platform.modules.plm.service.PlmVideoNewsService;
import com.platform.modules.plm.thread.QkSynthesisStopThread;
import com.platform.modules.plm.util.FileCalculateUtils;
import com.platform.modules.plm.util.FileSizeUnitEnum;
import com.platform.modules.plm.util.MateriaFileProperties;
import com.platform.modules.qk.service.CloudClipsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service("cloulClipsServiceImpl")
public class CloudClipsServiceImpl implements CloudClipsService {
	
	@Autowired
	private PlmMaterialService materialService;
	@Autowired
	private PlmVideoNewsService plmVideoNewsService;
	@Autowired
	private MateriaFileProperties materiaFileProperties;
	@Override
	public Map<String, Object> queryPage(Map<String, Object> params) {
		return materialService.queryPageMap(params);
	}
	
	@Override
	public void synthesisStart(Map<String, Object> params){
		Long taskId = params.get("taskId")==null ? null : Long.valueOf(params.get("taskId").toString()); 
		String title = params.get("title")==null ? null : params.get("title").toString();
		Long operatorId = params.get("operatorId")==null ? null : Long.valueOf(params.get("operatorId").toString());
		Long startTime = params.get("startTime")==null ? null : Long.valueOf(params.get("startTime").toString());
		PlmVideoNewsEntity videoNewsEntity = new PlmVideoNewsEntity();
		videoNewsEntity.setTitle(title);
		videoNewsEntity.setTaskId(taskId);
		videoNewsEntity.setCreateUser(operatorId);
		videoNewsEntity.setCreateDate(new Date());
		videoNewsEntity.setStartTime(new Date(startTime));
		videoNewsEntity.setVideoSources(1);
		videoNewsEntity.setStatus(0);
		plmVideoNewsService.insert(videoNewsEntity);
	}
	@Override
	public void synthesisStop(Map<String, Object> params){
		Long taskId = params.get("taskId")==null ? null : Long.valueOf(params.get("taskId").toString()); 
		Long materialId = params.get("materialId")==null ? null : Long.valueOf(params.get("materialId").toString()); 
		String materialCoverUrl = params.get("materialCoverUrl")==null ? null : params.get("materialCoverUrl").toString();
		String materialUrl = params.get("materialUrl")==null ? null : params.get("materialUrl").toString();
		Long materialDuration = params.get("materialDuration")==null ? null : Long.valueOf(params.get("materialDuration").toString());
		Long materialSize = params.get("materialSize")==null ? null : Long.valueOf(params.get("materialSize").toString());
		Long endTime = params.get("endTime")==null ? null : Long.valueOf(params.get("endTime").toString());
		String status = params.get("status")==null ? null :params.get("status").toString();
		String message = params.get("message")==null ? null :params.get("message").toString();
		
		PlmVideoNewsEntity videoNewsEntity = plmVideoNewsService.queryByTaskId(taskId);
		if(videoNewsEntity!=null && videoNewsEntity.getId()!=null){
			if(StringUtils.isNotBlank(status) && (status=="success" || status.equals("success"))){
				videoNewsEntity.setMaterialId(materialId);
				videoNewsEntity.setMaterialUrl(materialUrl);
				videoNewsEntity.setCoverUrl(materialCoverUrl);
				
				if(materialSize!=null){
					videoNewsEntity.setMaterialSize(FileCalculateUtils.calculateFileSizeString(materialSize, FileSizeUnitEnum.MB));
					videoNewsEntity.setFileSize(materialSize);
	            }
	            if(materialDuration!=null){
	            	videoNewsEntity.setPlayTime(materialDuration);
	            	videoNewsEntity.setMaterialDuration(FileCalculateUtils.calculateFilePlayTimeString(materialDuration));
	            }
				
				videoNewsEntity.setStatus(1);
			}else{
				videoNewsEntity.setStatus(-1);
			}
			videoNewsEntity.setStopTime(new Date(endTime));
			videoNewsEntity.setMessage(message);
			plmVideoNewsService.updateById(videoNewsEntity);
			
			if(StringUtils.isNotBlank(status) && (status=="success" || status.equals("success"))){
				QkSynthesisStopThread thread = new QkSynthesisStopThread(videoNewsEntity.getId(),materiaFileProperties.getMediaPath(),materiaFileProperties.getMateriaPath());
				thread.start();
			}
		}
	}
}
