package cn.hmsoft.weixin.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.frame.service.FrameService;
import cn.hmsoft.weixin.constants.WxMaterialType;
import cn.hmsoft.weixin.data.dao.WxMaterialDao;
import cn.hmsoft.weixin.entity.response.MaterialResponseJson;
import cn.hmsoft.weixin.entity.response.WxResponseJson;
import cn.hmsoft.weixin.request.MaterialBatchgetRequest;
import cn.hmsoft.weixin.request.MaterialDelRequest;
import cn.hmsoft.weixin.request.MaterialGetRequest;
import cn.hmsoft.weixin.request.MediaGetRequest;

@Service
public class WxMaterialService extends FrameService {
	@Autowired
	private WxMaterialDao daoMaterial;

	public void uploadMaterial(int wx_id,  String type, FrameOptr optr) {
		// 首先检查file是否已经存在
//		WxMaterial wm = this.daoMaterial.find(WxMaterial.class, "local_file_id", file.getFile_id());
//		FrameAssertUtil.isNull(wm, "该素材已上传到微信服务器，请检查");
//		// 判断文件是否存在
//		File localFile = new File(FrameParamUtil.getFrameUploadFilePath() + file.getFile_path());
//		if (!localFile.exists())
//			throw new BusinessException("文件[" + file.getFile_name() + "]未找到，请重新上传");
//		if (!localFile.isFile())
//			throw new BusinessException("[" + file.getFile_name() + "]为文件夹，请选择其他素材");
//		MaterialAddRequest mr = new MaterialAddRequest(type, localFile);
//		mr.setWxDefine(wx_id);
//		MaterialResponseJson json = mr.request();
//		if (json == null)
//			throw new BusinessException("素材上传至微信服务器出现错误，请重试！");
//		// 成功，添加到本地
//		WxMaterial material = new WxMaterial();
//		material.setLocal_file_id(file.getFile_id());
//		material.setMaterial_type(type);
//		material.setWx_id(wx_id);
//		material.setMedia_id(json.getMedia_id());
//		material.setStatus(FrameStatus.Active.toString());
//		material.setUpdate_time(new Date());
//		this.daoMaterial.insert(material);
	}

	public void downloadMaterial(int wx_id, String media_id, FrameOptr optr) {
		MaterialGetRequest mg = new MaterialGetRequest(media_id);
		mg.setWxDefine(wx_id);
		mg.request();

	}

	public void downloadMaterialTemp(int wx_id, String media_id, String type, FrameOptr optr) {
		MediaGetRequest mg = new MediaGetRequest(media_id, type);
		mg.setWxDefine(wx_id);
		mg.request();

	}

	public void delMaterial(String app_id, String media_id, FrameOptr optr) {
		MaterialDelRequest md = new MaterialDelRequest(media_id);
		md.setWxDefine(app_id);
		WxResponseJson json = md.request();
		if (json == null)
			throw new BusinessException("出现未知错误，操作失败");
		this.daoMaterial.delete("media_id", media_id);
	}

	public void syncMaterial(int wx_id,FrameOptr optr) {

		syncMaterial(wx_id,WxMaterialType.Image);
		syncMaterial(wx_id,WxMaterialType.Video);
		syncMaterial(wx_id,WxMaterialType.Voice);
		// 图文

	}

	private void syncMaterial(int wx_id,String type) {
		// 一次取10个
		int count = 20, offset = 0;
		while (offset > -1) {
			MaterialBatchgetRequest mb = new MaterialBatchgetRequest(type, offset, count);
			mb.setWxDefine(wx_id);
			MaterialResponseJson json = mb.request();
			if (json.getItem_count() == 0)
				offset = -1;
			else
				offset += count;
		}
	}

}
