package org.springblade.third.videodevice.wvp.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.utils.JsonUtils;
import org.springblade.core.mp.support.BladePage;
import org.springblade.core.tool.api.R;
import org.springblade.device.entity.GbDeviceChannel;
import org.springblade.third.videodevice.gb28181.conf.WvpConfig;
import org.springblade.third.videodevice.gb28181.entity.GbChannel;
import org.springblade.third.videodevice.gb28181.entity.GbDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WvpService {

	@Autowired
	private WvpConfig wvpConfig;

	public R<GbDevice> getByDeviceId(String deviceId) {
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("deviceId", deviceId);
		String reqUrl = String.format("/api/device/query/devices/%s", deviceId);
		HttpRequest request = new HttpRequest(wvpUrl + reqUrl);
		HttpResponse response = request.form(paramMap).execute();
		log.info("【wvp】getDevice", "【requestParam】：" + reqUrl + JSONObject.toJSONString(paramMap) + ",【response】：" + response.body());
		int status = response.getStatus();
		if (status == 200) {
			GbDevice gbDevice = JSONObject.parseObject(response.body(), GbDevice.class);
			if(gbDevice != null){
				return R.data(gbDevice);
			}else{
				return R.data(null);
			}
		} else {
			return R.fail(null, "查询设备失败");
		}
	}

	public R<List<GbChannel>> getChannelByDeviceId(String deviceId) {
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("deviceId", deviceId);
		paramMap.put("count", 200);
		paramMap.put("page", 1);
		String reqUrl = String.format("/api/device/query/devices/%s/channels", deviceId);
		HttpRequest request = new HttpRequest(wvpUrl + reqUrl);
		HttpResponse response = request.form(paramMap).execute();
		log.info("【wvp】getDeviceChannels", "【requestParam】：" + reqUrl + JSONObject.toJSONString(paramMap) + ",【response】：" + response.body());
		int status = response.getStatus();
		if(status == 200){
			JSONObject data = JSONObject.parseObject(response.body());
			String json = JsonUtils.objectToJson(data.get("list"));
			List<GbChannel> channelList = JSONArray.parseArray(json, GbChannel.class);
			if(channelList != null && channelList.size() > 0){
				return R.data(channelList);
			}else{
				return R.data(new ArrayList<>());
			}
		}else{
			return R.fail(null, "分页查询设备通道失败");
		}
	}

	public R<GbDevice> sync(String deviceId) {
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		String reqUrl = String.format("/api/device/query/devices/%s/sync", deviceId);
		HttpResponse response = HttpRequest.post(wvpUrl + reqUrl).execute();
		log.info("【wvp】deviceSync", "【requestParam】：" + null + "【response】：" + response.body());
		int status = response.getStatus();
		if(status == 200){
			GbDevice gbDevice = JSONObject.parseObject(response.body(), GbDevice.class);
			if(gbDevice != null){
				return R.data(gbDevice);
			}else{
				return R.data(null);
			}
		}else{
			return R.fail(null, "设备通道同步失败");
		}
	}

	public R<JSONObject> play(String deviceId, String channelId) {
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		String reqUrl = String.format("/api/play/start/%s/%s", deviceId, channelId);
		HttpResponse response = HttpRequest.get(wvpUrl + reqUrl).execute();
		log.info("【wvp】devicePlay", "【requestParam】：" + null + "【response】：" + response.body());
		JSONObject result = JSONObject.parseObject(response.body());
		Integer code = (Integer)result.get("code");
		if(code == 0){
			return R.data(result.getJSONObject("data"));
		}else{
			return R.fail(result.getString("msg"));
		}
	}

	public R<String> stop(String deviceId, String channelId) {
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		String reqUrl = String.format("/api/play/stop/%s/%s", deviceId, channelId);
		HttpResponse response = HttpRequest.get(wvpUrl + reqUrl).execute();
		log.info("【wvp】devicePlay", "【requestParam】：" + reqUrl + "【response】：" + response.body());
		String data = response.body();
		if(StringUtils.hasText(data)){
			if(!data.equals("点播未找到") && !data.equals("streamId null") && !data.equals("Timeout")){
				return R.success("停止成功");
			}else{
				return R.fail(data);
			}
		}else{
			return R.fail(data);
		}
	}
}
