package com.palmnest.application.core.util;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.superdesk.libs.common.json.JsonUtils;
import kafka.utils.Json;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.esotericsoftware.minlog.Log;
import com.palmnest.application.dto.PushCustomMessageVo;
import com.palmnest.application.dto.UserInfoVo;
import com.palmnest.model.User;

public class PushMessageHelper {

	private static Logger logger = LoggerFactory.getLogger(PushMessageHelper.class);
	
	public static       String newMsg               = PropertiesKit.readPropertiesValue("newMsg", "wechat.properties");
	public static       String newMsgUrl               = PropertiesKit.readPropertiesValue("newMsgUrl", "wechat.properties");


	/**
	 * app通知调此方法  不需要url
	 *
	 * @param id
	 * @param code
	 * @param users
	 * @param title
	 * @param content
	 * @throws Exception
	 */
	public void pushMessage(Long id, String code, List<User> users, String title, String content) throws Exception {


		logger.info("PushMessageHelper pushMessage======");
		logger.info("sendPushMessage===[id]===" + id);
		logger.info("sendPushMessage===[code]===" + code);
		logger.info("sendPushMessage===[title]===" + title);
		logger.info("sendPushMessage===[content]===" + content);
		logger.info("sendPushMessage===[userList]===" + JsonUtils.toJson(users));

		if(StringUtils.isNotEmpty(newMsgUrl)){
			pushNewMessage(id.toString(),code,"",users,title,content);
		}else{
			Map<String, String> extra = new HashMap<String, String>();
			extra.put("code", code);
			extra.put("id", id.toString());
			List<String> androidNos = new ArrayList<String>();
			List<String> iosNos = new ArrayList<String>();
			for (User u : users) {
				if (u.getDeviceNo() != null && u.getDeviceType() != null && StringUtils.isNotEmpty(u.getDeviceNo())) {
					if (u.getDeviceType().intValue() == 1) {
						androidNos.add(u.getDeviceNo());
					} else if (u.getDeviceType().intValue() == 2) {
						iosNos.add(u.getDeviceNo());
					}
				}
			}
			String result = "";
			if (androidNos.size() > 0) {
//					System.out.println("androidNos"+androidNos);
				result = PushMessageUtils.sendPushMessageByAndroidWithResult(androidNos, title, content, extra);
			}
			if (iosNos.size() > 0) {
//					System.out.println("iosNos"+iosNos);
				result = PushMessageUtils.sendPushMessageByIosWithResult(iosNos, title, content, extra);
			}
			if (result.equals("success")) {
				logger.info("消息推送成功");
			} else {
				logger.info("消息推送失败:" + result);
			}
			logger.info("PushMessageHelper pushMessage======");
		}
	}

	
//	public static void main(String[] args) {
////		User u=new User();
////		u.setDeviceType(2);
////		u.setDeviceNo("23b7e30356a46f8253831446d46dbe7f511815e6a6a8d584ae7b2d76f44b62c4");
////		List<User> list=new ArrayList<User>();
////		list.add(u);
////		Long id=200L;
////		try {
////			pushMessage(id,"500",list,"测试推送","我是内容");
////		} catch (Exception e) {
////			// TODO Auto-generated catch block
////			e.printStackTrace();
////		}
//    }

	
	
	
	/**
	 * 参数问题加一个方法
	 *
	 * @param id
	 * @param code
	 * @param users
	 * @param title
	 * @param content
	 * @throws Exception
	 */
	public void pushMessageTwo(String id, String code, List<User> users, String title, String content) throws Exception {


		logger.info("PushMessageHelper pushMessage======");
		logger.info("sendPushMessage===[id]===" + id);
		logger.info("sendPushMessage===[code]===" + code);
		logger.info("sendPushMessage===[title]===" + title);
		logger.info("sendPushMessage===[content]===" + content);
		logger.info("sendPushMessage===[userList]===" + users.toString());

		if(StringUtils.isNotEmpty(newMsgUrl)){
			pushNewMessage(id.toString(),code,"",users,title,content);
		}else{
			Map<String, String> extra = new HashMap<String, String>();
			extra.put("code", code);
			extra.put("id", id);
			List<String> androidNos = new ArrayList<String>();
			List<String> iosNos = new ArrayList<String>();
			for (User u : users) {
				if (u.getDeviceNo() != null && u.getDeviceType() != null && StringUtils.isNotEmpty(u.getDeviceNo())) {
					if (u.getDeviceType().intValue() == 1) {
						androidNos.add(u.getDeviceNo());
					} else if (u.getDeviceType().intValue() == 2) {
						iosNos.add(u.getDeviceNo());
					}
				}
			}
			String result = "";
			if (androidNos.size() > 0) {
//					System.out.println("androidNos"+androidNos);
				result = PushMessageUtils.sendPushMessageByAndroidWithResult(androidNos, title, content, extra);
			}
			if (iosNos.size() > 0) {
//					System.out.println("iosNos"+iosNos);
				result = PushMessageUtils.sendPushMessageByIosWithResult(iosNos, title, content, extra);
			}
			if (result.equals("success")) {
				logger.info("消息推送成功");
			} else {
				logger.info("消息推送失败:" + result);
			}
			logger.info("PushMessageHelper pushMessage======");
		}
	}


	/**
	 * app公告推送 需跳转详情
	 *
	 * @param id
	 * @param code
	 * @param users
	 * @param title
	 * @param content
	 * @throws Exception
	 */
	public void pushMessage(Long id, String code, String url, List<User> users, String title, String content) throws Exception {

		
		logger.info("PushMessageHelper pushMessage======带url");
		logger.info("sendPushMessage===[id]===" + id);
		logger.info("sendPushMessage===[code]===" + code);
		logger.info("sendPushMessage===[url]===" + url);
		logger.info("sendPushMessage===[title]===" + title);
		logger.info("sendPushMessage===[content]===" + content);
		logger.info("sendPushMessage===[userList]===" + JsonUtils.toJson(users));
		logger.info("sendPushMessage===[是否调用直接调用新框架-newMsgUrl]===" + newMsgUrl.toString());
		
		
		
		if(StringUtils.isNotEmpty(newMsgUrl)){
			pushNewMessage(id.toString(),code,url,users,title,content);
		}else{
			Map<String, String> extra = new HashMap<String, String>();
			extra.put("code", code);
			extra.put("id", id.toString());
			extra.put("url", url);
			List<String> androidNos = new ArrayList<String>();
			List<String> iosNos = new ArrayList<String>();
			for (User u : users) {
				if (u.getDeviceNo() != null && u.getDeviceType() != null && StringUtils.isNotEmpty(u.getDeviceNo())) {
					if (u.getDeviceType().intValue() == 1) {
						androidNos.add(u.getDeviceNo());
					} else if (u.getDeviceType().intValue() == 2) {
						iosNos.add(u.getDeviceNo());
					}
				}
			}
			String result = "";
			if (androidNos.size() > 0) {
//					System.out.println("androidNos"+androidNos);
				result = PushMessageUtils.sendPushMessageByAndroidWithResult(androidNos, title, content, extra);
			}
			if (iosNos.size() > 0) {
//					System.out.println("iosNos"+iosNos);
				extra.remove("url");
				result = PushMessageUtils.sendPushMessageByIosWithResult(iosNos, title, content, extra, url);
			}
			if (result.equals("success")) {
				logger.info("消息推送成功");
			} else {
				logger.info("消息推送失败:" + result);
			}
		}
	}
	
	/**
	 * 新版本的推送
	 * @param id
	 * @param code
	 * @param url
	 * @param users
	 * @param title
	 * @param content
	 */
	public void pushNewMessage(String id, String code, String url, List<User> users, String title, String content){
		PushCustomMessageVo vo=new PushCustomMessageVo();
		vo.setId(id);
		vo.setCode(code);
		vo.setContent(content);
		vo.setTitle(title);
		vo.setUrl(url);
		List<UserInfoVo> userList=new ArrayList<UserInfoVo>();
		for (User u : users) {
			UserInfoVo userVo=new UserInfoVo();
			userVo.setDeviceNo(u.getDeviceNo());
			userVo.setDeviceType(u.getDeviceType());
			userVo.setUserId(u.getId().toString());
			userList.add(userVo);
		}
		if(CollectionUtils.isEmpty(userList)){
			return;
		}
		vo.setUsers(JSON.toJSONString(userList));
		String params=JSONArray.toJSONString(vo);
		try {
			logger.info("======>pushNewMessage-params:"+params);
			String result=HttpKit.HttpClientPost(params,newMsgUrl);
			logger.info("======>pushNewMessage-推送结果:"+result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		List<User> users=new ArrayList<User>();
		User user=new User();
		user.setDeviceNo("34e1ea9a4ef5255764ab4b69b10e049ea0e16f5e8f3eade7d4e1287d939fc654");
		user.setDeviceType(2);
		user.setId(1L);
		users.add(user);
//		pushNewMessage("100", "200", "", users, "我是标题哦", "我是内容哦");
	}
}
