package com.xinelu.applet.controller.videoconsultation;

import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.domain.R;
import com.xinelu.common.utils.tencentcloudapi.common.Credential;
import com.xinelu.common.utils.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.xinelu.common.utils.tencentcloudapi.common.profile.ClientProfile;
import com.xinelu.common.utils.tencentcloudapi.common.profile.HttpProfile;
import com.xinelu.common.utils.tencentcloudapi.trtc.v20190722.TLSSigAPIv2;
import com.xinelu.common.utils.tencentcloudapi.trtc.v20190722.TrtcClient;
import com.xinelu.common.utils.tencentcloudapi.trtc.v20190722.models.DissolveRoomRequest;
import com.xinelu.common.utils.tencentcloudapi.trtc.v20190722.models.DissolveRoomResponse;
import com.xinelu.manage.service.consultationInfo.IConsultationInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author gaoyu
 * @description 远程会诊控制器
 * @date 2022-12-07 9:49
 */
@Api(tags = "远程会诊控制器")
@RestController
@RequestMapping("/nurseApplet/consultation")
public class VideoConsultationController {

    @Value("${trtc.sdkappid}")
    private Long sdkappid;
	@Value("${trtc.sdksecretkey}")
	private String sdksecretkey;
    @Value("${trtc.secretid}")
    private String secretid;
    @Value("${trtc.secretkey}")
    private String secretkey;
    private final static String ENDPOINT = "trtc.tencentcloudapi.com";
    private final static String REGION = "ap-beijing";


	@Resource
	private IConsultationInfoService consultationInfoService;

	@ApiOperation("获取userSig")
	@GetMapping("getUserSig/{userId}")
	public R<String> getUserSig(@PathVariable String userId) {
		TLSSigAPIv2 sigAPIv2 = new TLSSigAPIv2(sdkappid, sdksecretkey);
		String userSig = sigAPIv2.genUserSig(userId, 36000);
		return R.ok(userSig);
	}

    @ApiOperation("开始视频--医生APP")
    @GetMapping("start/{goodsOrderId}")
    public AjaxResult start(@PathVariable Long goodsOrderId) {
        return consultationInfoService.startConsultation(goodsOrderId);
    }


    @ApiOperation("解散房间")
    @GetMapping("dissolveRoom/{roomId}")
    public R<?> dissolveRoom(@PathVariable String roomId) throws TencentCloudSDKException {
        Credential cred = new Credential(secretid, secretkey);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(ENDPOINT);
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        TrtcClient client = new TrtcClient(cred, REGION, clientProfile);
        String params = "{\"SdkAppId\":" + sdkappid + ",\"RoomId\":" + roomId + "}";
        DissolveRoomRequest req = DissolveRoomRequest.fromJsonString(params, DissolveRoomRequest.class);
        DissolveRoomResponse resp = client.DissolveRoom(req);
        return R.ok(resp.getRequestId());
    }

	@ApiOperation("小程序通过订单号查询房间号")
	@GetMapping("getRoomId/{goodsOrderId}")
	public AjaxResult getAppletRoomId(@PathVariable Long goodsOrderId) {
		return consultationInfoService.getAppletRoomId(goodsOrderId);
	}

}
