package com.platform.modules.plm.thread;

import java.io.File;

import org.apache.commons.lang3.StringUtils;

import com.platform.common.exception.RRException;
import com.platform.common.utils.FileUtils;
import com.platform.common.utils.HttpClientHelper;
import com.platform.common.utils.SpringContextUtils;
import com.platform.modules.plm.entity.PlmMaterialEntity;
import com.platform.modules.plm.service.PlmMaterialService;
import com.platform.modules.plm.util.FileCalculateUtils;
import com.platform.modules.plm.util.FileSizeUnitEnum;
import com.platform.modules.plm.util.MaterialUtils;
import com.platform.modules.video.service.RealPathResolver;
import com.platform.modules.video.util.VideoUtils;

public class MediaDownloadThread extends Thread{

	private Long id;
	private String mediaPath;
	private String materiaPath;
	
	public MediaDownloadThread(Long id,String mediaPath,String materiaPath){
		this.id = id;
		this.mediaPath = mediaPath;
		this.materiaPath = materiaPath;
	}

	@Override
	public void run() {
		try{
			if(id==null){
				return;
			}
			
			PlmMaterialService plmMaterialService = (PlmMaterialService) SpringContextUtils.getBean("plmMaterialService");
			RealPathResolver realPathResolver = (RealPathResolver) SpringContextUtils.getBean("realPathResolver");
			
			PlmMaterialEntity materialEntity = plmMaterialService.selectById(id);
			if(materialEntity==null || materialEntity.getId()==null || StringUtils.isBlank(materialEntity.getMaterialOriUrl())){
				return;
			}
			String oriUrl = materialEntity.getMaterialOriUrl();
			boolean isProxy = false;
			if(materialEntity.getMaterialSources()==2 || materialEntity.getMaterialSources().equals(2)){
				isProxy = true;
			}
	    	if(StringUtils.isNotBlank(oriUrl)){
	    		String[] urls = oriUrl.split(",");
	    		if(urls!=null && urls.length>0){
					String urlStr = "";
					String size = "";
					String duration = "";
					long totalFileSize = 0;
					long totalDuration=0;
					for(String url : urls){
						if(StringUtils.isBlank(url)){
							continue;
						}
						url = (url.indexOf("?")>0 ? url.substring(0, url.lastIndexOf("?")) : url);
	                	String serverVideoPath = mediaPath + "/" + VideoUtils.getDateDir() + "/" + FileUtils.createFileName() + "." + FileUtils.getSuffix(url); // 存放在服务器地址
	                    String newFilePath = realPathResolver.get(materiaPath + "/" + serverVideoPath);
	                	HttpClientHelper.sendGetAndSaveFile(url, null, newFilePath,isProxy);
	                	File file = new File(newFilePath);
	                	urlStr += serverVideoPath+",";
	    	    		
	                	long fileSize = MaterialUtils.getFileSize(file);
	    	    		String s = FileCalculateUtils.calculateFileSizeString(fileSize, FileSizeUnitEnum.MB);
	    	    		totalFileSize += fileSize;
	    				size += s+",";
	    				if(materialEntity.getMaterialType()!=3){
		    				long playTime = MaterialUtils.getPlayTime(file);
		    				String d = FileCalculateUtils.calculateFilePlayTimeString(playTime);
		    				totalDuration += playTime;
		    	    		duration +=  d +",";
	    				}
					}
					
					materialEntity.setMaterialUrl(urlStr.substring(0, urlStr.lastIndexOf(",")));
					if(materialEntity.getMaterialType().equals(3)){
						materialEntity.setMaterialSize(FileCalculateUtils.calculateFileSizeString(totalFileSize, FileSizeUnitEnum.MB));
						materialEntity.setMaterialCoverUrl(urlStr.substring(0, urlStr.lastIndexOf(",")));
						materialEntity.setMaterialDuration(urls.length+"");
						materialEntity.setFileSize(totalFileSize);
					}else{
						materialEntity.setMaterialSize(size.substring(0, size.lastIndexOf(",")));
						materialEntity.setMaterialDuration(duration.substring(0, duration.lastIndexOf(",")));
						materialEntity.setFileSize(totalFileSize);
						materialEntity.setPlayTime(totalDuration);
					}
					
				}
	    	}
	    	
	    	String coverUrl = materialEntity.getMaterialCoverOriUrl();
	    	if(StringUtils.isNotBlank(coverUrl)){
	    		coverUrl = (coverUrl.indexOf("?")>0 ? coverUrl.substring(0, coverUrl.lastIndexOf("?")) : coverUrl);
	    		String serverCoverPath = mediaPath +"/" + VideoUtils.getDateDir() + "/" + FileUtils.createFileName() + "." + FileUtils.getSuffix(coverUrl); // 存放在服务器地址
	    		String newFilePath = realPathResolver.get(materiaPath + "/" + serverCoverPath);
	        	HttpClientHelper.sendGetAndSaveFile(coverUrl, null, newFilePath,isProxy);
	        	materialEntity.setMaterialCoverUrl(serverCoverPath);
	    	}
	    	materialEntity.setMaterialStatus(1);
			plmMaterialService.updateById(materialEntity);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RRException("请求失败");
		}
	}
}
