package com.yihu.wlyy.util;


import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class SendPatientUtil extends  Thread {
	private String from;
	private String fromName;
	private String to;
	private String contentType;
	private String content;
	private String title;
	private String type;
	private String teamCode;

	public SendPatientUtil(String from,String fromName,String to,String contentType,String content,String title,String type,String teamCode){
		this.from = from;
		this.fromName = fromName;
		this.to = to;
		this.contentType = contentType;
		this.content = content;
		this.title = title;
		this.type = type;
		this.teamCode =teamCode;
	}


	public String sendToPatient(String from,String fromName,String to,String contentType,String content,String title,String type,String teamCode){
		JSONArray jsonArray = ImUtill.getParticipants(to+"_"+teamCode+"_"+type);
		JSONObject participants = new JSONObject();
		for(Object object:jsonArray){
			JSONObject jsonObject = (JSONObject)object;
			participants.put(jsonObject.getString("id"),jsonObject.get("role"));
		}
		if(participants.isNull(from)){
			participants.put(from,0);
		}
		if(participants.isNull(to)){
			participants.put(from,0);
		}
		participants.put(to,0);
		if(!"4".equals(contentType)){
			contentType = "0";
		}
		JSONObject sessionJson  = ImUtill.createSession(participants,ImUtill.SESSION_TYPE_MUC,title,to+"_"+teamCode+"_"+type);
		if(sessionJson.getInt("status")==-1){
			throw  new RuntimeException(sessionJson.getString("message"));
		}
		JSONObject session = sessionJson.getJSONObject("data");
		return ImUtill.sendImMsg(from,fromName,session.getString("id"),contentType,content,"1");
	}

	public void run(){
		this.sendToPatient(from,fromName,to,contentType,content,title,type,teamCode);
	}
}
