package com.ssj.service.sys.easemob.service.impl;


import com.ssj.conch.easemob.impl.EasemobIMUsers;
import com.ssj.conch.easemob.impl.EasemobSendMessage;
import com.ssj.conch.easemob.util.EasemobConstants;
import com.ssj.service.sys.easemob.service.EasemobService;
import io.swagger.client.model.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class EasemobServiceImpl implements EasemobService{
	protected static final Logger logger = LoggerFactory.getLogger("aijia");

	@Autowired
	private EasemobIMUsers easemobIMUsers;
	@Autowired
	private EasemobSendMessage easemobSendMessage;

	@Override
	public void createUserSingle(String easemobName,String nickName) {
		logger.error("调用新建环信用户API接口");
		RegisterUsers users = new RegisterUsers();
		User user = new User();
		user.setUsername(easemobName);
		user.setPassword(EasemobConstants.EASEMOB_PASSWORD);
        users.add(user);
        Object result = easemobIMUsers.createNewIMUserSingle(users);
		logger.error("调用创建环信用户信息API,返回结果result={}",result);
		if(result == null) {
			logger.error("创建环信用户异常,环信号:{}",easemobName);
			//如果返回结果为空，则抛出运行时异常，回滚
			throw new RuntimeException();
		}
		if(StringUtils.isNotBlank(nickName)) {
			//修改用户昵称
			Nickname nickname = new Nickname();
			nickname.setNickname(getValidateNickName(nickName));
			result = easemobIMUsers.modifyIMUserNickNameWithAdminToken(easemobName, nickname);
			logger.error("调用修改环信用户信息API,返回结果result={}",result);
			if(result == null) {
				//如果返回结果为空，则抛出运行时异常，回滚
				logger.error("修改环信用户信息异常,环信号:{}",easemobName);
				throw new RuntimeException();
			}
		}
	}

	@Override
	public void updateUserNickName(String easemobName,String nickName) {
		logger.error("调用修改环信用户信息API接口");
		//修改用户昵称
		Nickname nickname = new Nickname();
		nickname.setNickname(getValidateNickName(nickName));
		Object result = easemobIMUsers.modifyIMUserNickNameWithAdminToken(easemobName, nickname);
		logger.error("调用修改环信用户信息API,返回结果result={}",result);
		if(result == null) {
			//如果返回结果为空，则抛出运行时异常，回滚
			logger.error("修改环信用户信息异常,环信号:{}",easemobName);
			throw new RuntimeException();
		}
	}

	@Override
	public void addFriendSingle(String easemobName, String friendEasemobName) {
		logger.error("调用添加环信好友API接口,环信号{},{}",easemobName,friendEasemobName);
		//校验好友的环信账号是否存在
		Object result = easemobIMUsers.getIMUserByUserName(friendEasemobName);
		logger.error("调用校验好友的环信账号是否存在API,返回结果result={}",result);
		if(result == null) {
			//创建环境账号
			createUserSingle(friendEasemobName, null);
		}
		result = easemobIMUsers.addFriendSingle(easemobName, friendEasemobName);
		logger.error("调用添加环信好友API,返回结果result={}",result);
		if(result == null) {
			logger.error("添加环信好友API异常,环信号:{},{}",easemobName,friendEasemobName);
			//如果返回结果为空，则抛出运行时异常，回滚
			throw new RuntimeException();
		}
	}

	@Override
	public void deleteFriendSingle(String easemobName, String friendEasemobName) {
		logger.error("调用删除环信好友API接口,环信号{},{}",easemobName,friendEasemobName);
		Object result = easemobIMUsers.deleteFriendSingle(easemobName, friendEasemobName);
		logger.error("调用删除环信好友API,返回结果result={}",result);
		if(result == null) {
			//如果返回结果为空，则抛出运行时异常，回滚
			logger.error("删除环信好友API异常,环信号:{},{}",easemobName,friendEasemobName);
			throw new RuntimeException();
		}
	}

	@Override
	public void deleteUserSingle(String easemobName) {
		logger.error("调用删除环信用户API接口,环信号{}",easemobName);
		//查询有没有该环信号，如果没有则不用调删除
		Object result = easemobIMUsers.getIMUserByUserName(easemobName);
		logger.error("调用获取环信用户API,返回结果result={}",result);
		if(result != null) {
			result = easemobIMUsers.deleteIMUserByUserName(easemobName);
			logger.error("调用删除环信用户API,返回结果result={}",result);
			if(result == null) {
				//如果返回结果为空，则抛出运行时异常，回滚
				logger.error("调用删除环信用户API异常,环信号:{}",easemobName);
				throw new RuntimeException();
			}
		}
	}
	
	private String getValidateNickName(String nickName) {
		return nickName.replaceAll("[^0-9a-zA-Z\u4e00-\u9fa5.，,。？“”]+","");
	}
	
	@Override
	public void sendSystemMessages(String easemobName, String title, String content) {
		logger.error("调用发送环信推送消息API接口,环信号:{}，标题:{},内容:{}",easemobName,title,content);
		Msg msg = new Msg();
		//发送的目标类型
		msg.setTargetType("users");
		//发送目标
		UserName target = new UserName();
		target.add(easemobName);
		msg.setTarget(target);
		
		//消息内容，标题，类型等信息
		Map<String,String> titleMsg = new HashMap<String,String>();
		titleMsg.put("type", "txt");
		titleMsg.put("msg", title);
		msg.setMsg(titleMsg);
		
		//发送扩展信息
		Map<String,String> extMsg = new HashMap<String,String>();
		extMsg.put("push", content);
		msg.setExt(extMsg);
		
		Object result = easemobSendMessage.sendMessage(msg);
		logger.error("调用发送环信推送消息API接口,返回结果result={}",result);
		if(result == null) {
			//如果返回结果为空，则抛出运行时异常，回滚
			logger.error("调用发送环信推送消息API接口异常,环信号:{}，标题:{},内容:{}",easemobName,title,content);
			throw new RuntimeException();
		}
	}
}
