package com.engine.web.meeting;

import com.alibaba.fastjson.JSONObject;
import com.api.meeting.cusvideo.util.YealinkVideoUtil;
import com.engine.common.util.ParamUtil;
import weaver.general.BaseBean;
import weaver.meeting.video.consumer.MeetingVideoFactory;
import weaver.meeting.video.consumer.VideoMeetingClient;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;


public class NewMeet {
    @Path("/newMeet")
    @POST
    @Produces("application/json")
    public String newMeet(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException {
        // try {
        // Map<String, Object> map = getRequestParams(request);
        // Map<String, Object> map = ParamUtil.request2Map(request);
        JSONObject requestBody = getJson(request);
        JSONObject meetingInfo = requestBody.getJSONObject("data").getJSONObject("meetingInfo");
        HashMap<String, String> param = new HashMap<>();
        param.put("meetingNum",meetingInfo.getString("meetingNum"));
        param.put("meetingID",meetingInfo.getString("meetingID"));
        param.put("subject",meetingInfo.getString("subject"));
        param.put("startTime",meetingInfo.getString("startTime"));
        param.put("createType",meetingInfo.getString("createType"));
        param.put("subjectID",meetingInfo.getJSONObject("organizer").getString("subjectID"));
        param.put("displayName",meetingInfo.getJSONObject("organizer").getString("displayName"));
        VideoMeetingClient videoMeetingClient = MeetingVideoFactory.getInstance("yealink");
        // videoMeetingClient.addMeeting()




        return JSONObject.toJSONString(param);


    }


    public static JSONObject getJson(HttpServletRequest request) throws IOException {
        //从前端获取输入字节流
        ServletInputStream requestInputStream = request.getInputStream();
        //将字节流转换为字符流,并设置字符编码为utf-8
        InputStreamReader ir = new InputStreamReader(requestInputStream,"utf-8");
        //使用字符缓冲流进行读取
        BufferedReader br = new BufferedReader(ir);
        StringBuilder sb = new StringBuilder();
        try {
            //开始拼装json字符串
            String line = null;

            while((line = br.readLine())!=null) {
                sb.append(line);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            requestInputStream.close();
            ir.close();
            br.close();
        }
        JSONObject json = JSONObject.parseObject(sb.toString());
        return json;
        // StringBuilder stringBuilder = new StringBuilder();
        // BufferedReader reader = request.getReader();
        // try {
        //     String line;
        //     while ((line = reader.readLine()) != null) {
        //         stringBuilder.append(line).append('\n');
        //     }
        // } finally {
        //     reader.close();
        // }
        // return JSONObject.parseObject(stringBuilder.toString()) ;
    }


    private String getYealinkUrl(String host, String token, String mtnum, String userId, String meetingPassword) {
        String url = host + "/meeting-join-links/" + mtnum;
        JSONObject body = new JSONObject();
        body.put("language", "zh");
        body.put("password", meetingPassword);
        JSONObject resultJson = YealinkVideoUtil.doGet(url, token, null, body);
        return resultJson.getString("joinLink");
    }

}