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

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

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platform.common.exception.RRException;
import com.platform.common.utils.DesUtils;
import com.platform.common.utils.HttpClientHelper;
import com.platform.common.utils.MapUtils;
import com.platform.common.utils.ThirdConstant;
import com.platform.modules.plm.entity.PlmMaterialEntity;
import com.platform.modules.plm.service.PlmMaterialService;
import com.platform.modules.plm.thread.MediaDownloadThread;
import com.platform.modules.plm.util.MateriaFileProperties;
import com.platform.modules.scene.service.SceneService;

@Service("sceneService")
public class SceneServiceImpl implements SceneService{
	
	@Autowired
	private PlmMaterialService materialService;
	@Autowired
	private MateriaFileProperties materiaFileProperties; 

	@Override
	public Map<String, Object> queryPage(Map<String, Object> params) {
		params.put("token", DesUtils.encryptThirdType(DesUtils.thirdTypeScene));
		String result_json = HttpClientHelper.sendGet(ThirdConstant.SCENE_PAGE_URL,params,"UTF-8",false);
		return MapUtils.json2map(result_json);
	}
	
	@Override
	public Map<String, Object> findById(Integer sceneId){
		Map<String, Object> params = new  HashMap<String, Object>();
		params.put("token", DesUtils.encryptThirdType(DesUtils.thirdTypeGetScene));
		params.put("sceneId", sceneId);
		String result_json = HttpClientHelper.sendGet(ThirdConstant.GET_SCENE_URL,params,"UTF-8",false);
		return MapUtils.json2map(result_json);
	}
	
	@Override
	public void download(Integer sceneId,Long operatorId){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("token", DesUtils.encryptThirdType(DesUtils.thirdTypeGetScene));
		params.put("sceneId", sceneId);
		String result_json = HttpClientHelper.sendGet(ThirdConstant.GET_SCENE_URL, params, "UTF-8",false);
		try {
			JSONObject json = new JSONObject(result_json);
			if(json.getBoolean("success")){
				JSONObject  dataJson = json.getJSONObject("data");
				if(dataJson!=null){
					PlmMaterialEntity plmMaterial = new PlmMaterialEntity();
		    		plmMaterial.setMaterialTitle(dataJson.get("title")==null ? null : dataJson.getString("title"));
		    		plmMaterial.setMaterialType(1);
		    		plmMaterial.setMaterialDescription(dataJson.get("description")==null ? null : dataJson.getString("description"));
		    		plmMaterial.setMaterialSrcId(dataJson.get("id")==null?null : dataJson.get("id").toString());
		    		
	            	String downloadUrl = dataJson.get("videoUrl")==null ? null : dataJson.getString("videoUrl");
	            	String coverUrl = dataJson.get("titleImg")==null ? null : dataJson.getString("titleImg");
	            	plmMaterial.setMaterialOriUrl(downloadUrl);
	            	plmMaterial.setMaterialCoverOriUrl(coverUrl);
	            	plmMaterial.setMaterialStatus(0);
		    		plmMaterial.setCreateUser(operatorId);
		            plmMaterial.setCreateDate(new Date());
		            plmMaterial.setMaterialSources(3);
		            plmMaterial.setIsDelete(0);
		            this.materialService.insert(plmMaterial);
		            System.out.println(plmMaterial.getId());
		            MediaDownloadThread thread = new MediaDownloadThread(plmMaterial.getId(),materiaFileProperties.getMediaPath(),materiaFileProperties.getMateriaPath());
					thread.start();
				}
			}else{
				throw new RRException("请求失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RRException("请求失败");
		}
	}
	

	@Override
	public Map<String, Object> queryReportPage(Map<String, Object> params) {
		params.put("token", DesUtils.encryptThirdType(DesUtils.thirdTypeSceneReport));
		String result_json = HttpClientHelper.sendGet(ThirdConstant.SCENE_REPRORT_PAGE_URL,params,"UTF-8",false);
		return MapUtils.json2map(result_json);
	}
	
	@Override
	public Map<String, Object> findReportById(Integer sceneId,Integer sceneReportId){
		Map<String, Object> params = new  HashMap<String, Object>();
		params.put("token", DesUtils.encryptThirdType(DesUtils.thirdTypeGetSceneReport));
		params.put("sceneId", sceneId);
		params.put("reportId", sceneReportId);
		String result_json = HttpClientHelper.sendGet(ThirdConstant.GET_SCENE_REPRORT_URL,params,"UTF-8",false);
		return MapUtils.json2map(result_json);
	}
	
	
	@Override
	public void downloadReport(String title,Integer sceneId,Integer sceneReportId,Long operatorId){
		Map<String, Object> params = new  HashMap<String, Object>();
		params.put("token", DesUtils.encryptThirdType(DesUtils.thirdTypeGetSceneReport));
		params.put("sceneId", sceneId);
		params.put("reportId", sceneReportId);
		String result_json = HttpClientHelper.sendGet(ThirdConstant.GET_SCENE_REPRORT_URL,params,"UTF-8",false);
		try {
			JSONObject json = new JSONObject(result_json);
			if(json!=null && json.getBoolean("success")){
				PlmMaterialEntity plmMaterial = new PlmMaterialEntity();
				JSONObject  dataJson = json.getJSONObject("data");
				if(dataJson!=null){
					Integer reportType =  dataJson.get("reportType") ==null ? null : Integer.valueOf(dataJson.get("reportType")+"");
					if(reportType!=null && (reportType==6002 || reportType.equals(6002))){//图文
						JSONArray  imgList =  dataJson.get("imgList")==null ? null : dataJson.getJSONArray("imgList");
						if(imgList!=null && imgList.length()>0){
							String url = "";
							for(int i=0;i<imgList.length();i++){
								url += imgList.getString(i)+",";
							}
							plmMaterial.setMaterialOriUrl(url);
							plmMaterial.setMaterialCoverOriUrl(imgList.getString(0));
						}
						plmMaterial.setMaterialType(3);
					}else if(reportType!=null && ((reportType==6003 || reportType.equals(6003)) || (reportType==6004 || reportType.equals(6004)) || (reportType==6005 || reportType.equals(6005)))){//视频
						if(reportType==6004 || reportType.equals(6004)){
							plmMaterial.setMaterialType(2);
						}else{
							plmMaterial.setMaterialType(1);
						}
		            	String coverUrl = dataJson.get("videoImgUrl")==null ? null : dataJson.getString("videoImgUrl");
						plmMaterial.setMaterialCoverOriUrl(coverUrl);
					}
					plmMaterial.setMaterialSrcId(sceneId+">"+sceneReportId);
		    		plmMaterial.setMaterialTitle(title);
		    		plmMaterial.setMaterialDescription(dataJson.get("content")==null ? null : dataJson.getString("content"));
		    		plmMaterial.setMaterialStatus(0);
		    		plmMaterial.setCreateUser(operatorId);
		            plmMaterial.setCreateDate(new Date());
		            plmMaterial.setMaterialSources(4);
		            plmMaterial.setIsDelete(0);
		            this.materialService.insert(plmMaterial);
		            
					MediaDownloadThread thread = new MediaDownloadThread(plmMaterial.getId(),materiaFileProperties.getMediaPath(),materiaFileProperties.getMateriaPath());
					thread.start();
				}
			}else{
				throw new RRException("请求失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RRException("请求失败");
		}
	}
	
	@Override
	public void downloadReportImages(String title,Integer sceneId,Integer sceneReportId,Long operatorId,String[] images){
		Map<String, Object> params = new  HashMap<String, Object>();
		params.put("token", DesUtils.encryptThirdType(DesUtils.thirdTypeGetSceneReport));
		params.put("sceneId", sceneId);
		params.put("reportId", sceneReportId);
		String result_json = HttpClientHelper.sendGet(ThirdConstant.GET_SCENE_REPRORT_URL,params,"UTF-8",false);
		try {
			JSONObject json = new JSONObject(result_json);
			if(json!=null && json.getBoolean("success")){
				JSONObject  dataJson = json.getJSONObject("data");
				if(dataJson!=null){
					for(int i=0;i<images.length;i++){
						String image = images[i];
						PlmMaterialEntity plmMaterial = new PlmMaterialEntity();
						plmMaterial.setMaterialOriUrl(image);
						plmMaterial.setMaterialType(3);
						plmMaterial.setMaterialSrcId(sceneId+">"+sceneReportId);
			    		plmMaterial.setMaterialTitle(title+"("+sceneReportId+"["+i+"])");
			    		plmMaterial.setMaterialDescription(dataJson.get("content")==null ? null : dataJson.getString("content"));
			    		plmMaterial.setMaterialStatus(0);
			    		plmMaterial.setCreateUser(operatorId);
			            plmMaterial.setCreateDate(new Date());
			            plmMaterial.setMaterialSources(4);
			            plmMaterial.setIsDelete(0);
			            this.materialService.insert(plmMaterial);
			            
						MediaDownloadThread thread = new MediaDownloadThread(plmMaterial.getId(),materiaFileProperties.getMediaPath(),materiaFileProperties.getMateriaPath());
						thread.start();
					}
				}
			}else{
				throw new RRException("请求失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RRException("请求失败");
		}
	}

}
