package com.zd.speaker.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zd.common.core.domain.R;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.speaker.domain.vo.PlayVo;
import com.zd.speaker.service.ISpeakerService;
import com.zd.system.api.laboratory.RemoteLaboratoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * ip喇叭控制类
 */
@RestController
@RequestMapping("/speaker")
@Api(value = "远程喇叭控制类", tags = "远程喇叭控制类")
public class SpeakerController {

    private static Logger logger = LoggerFactory.getLogger(SpeakerController.class);

    @Autowired
    private ISpeakerService iSpeakerService;

    @Autowired
    private RemoteLaboratoryService remoteLaboratoryService;


    /**
     * 设备登录
     *
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "设备登录", notes = "设备登录")
    public AjaxResult queryOption() {
        String responseStr = iSpeakerService.login();
        return AjaxResult.success("操作成功", responseStr);
    }

    /***
     * 获取设备列表
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/getDeviceList")
    @ApiOperation(value = "获取设备列表", notes = "获取设备列表")
    public AjaxResult getDeviceList(Integer page, Integer pageSize,Long floorId) {
        R r=remoteLaboratoryService.getSpeakerByFloor(floorId);
        if(r.getCode()!=200){
            logger.error(r.getMsg());
            return AjaxResult.error("获取设备列表失败！");
        }
        List<Map<String,Object>> mapList= (List<Map<String, Object>>) r.getData();
        String responseStr = iSpeakerService.getDeviceList(page, pageSize);
        if(StringUtils.isEmpty(responseStr)){
            logger.error(r.getMsg());
            return AjaxResult.error("获取设备列表失败！");
        }
        JSONObject jsonObject=JSONObject.parseObject(responseStr);
        JSONArray jsonArray=JSONArray.parseArray(jsonObject.getString("rows"));
        for(Map<String,Object> map: mapList){
            for(int i=0;i<jsonArray.size();i++){
                JSONObject extra=jsonArray.getJSONObject(i).getJSONObject("extra");
                JSONObject obj = extra.getJSONObject("config");
                if(obj.getString("mac").equals(map.getOrDefault("hardwareNum","").toString())){
                    map.put("deviceSn",obj.getString("sn"));
                    map.put("deviceIp",extra.getString("ip"));
                    map.put("operate",extra.getString("online"));
                }
            }
        }
        return AjaxResult.success(mapList);
    }


    /***
     * 获取 实验室/实验室楼层 设备列表
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/getDeviceListBySub")
    @ApiOperation(value = "获取设备列表", notes = "获取设备列表")
    public AjaxResult getDeviceList(Integer page, Integer pageSize,Long floorId, Long subId) {
        R r=remoteLaboratoryService.getSpeakerBySub(floorId, subId);
        if(r.getCode()!=200){
            return AjaxResult.error("获取设备列表失败！");
        }
        List<Map<String,Object>> mapList= (List<Map<String, Object>>) r.getData();
        String responseStr = iSpeakerService.getDeviceList(page, pageSize);
        if(StringUtils.isEmpty(responseStr)){
            return AjaxResult.error("获取设备列表失败！");
        }
        JSONObject jsonObject=JSONObject.parseObject(responseStr);
        JSONArray jsonArray=JSONArray.parseArray(jsonObject.getString("rows"));
        for(Map<String,Object> map: mapList){
            for(int i=0;i<jsonArray.size();i++){
                JSONObject extra=jsonArray.getJSONObject(i).getJSONObject("extra");
                JSONObject obj = extra.getJSONObject("config");
                if(obj.getString("mac").equals(map.getOrDefault("hardwareNum","").toString())){
                    map.put("deviceSn",obj.getString("sn"));
                    map.put("deviceIp",extra.getString("ip"));
                    map.put("operate",extra.getString("online"));
                }
            }
        }
        return AjaxResult.success(mapList);
    }


    /***
     * 获取设备信息
     * @return
     */
    @PostMapping("/getDevice")
    @ApiOperation(value = "获取设备信息", notes = "获取设备信息")
    public AjaxResult getDeviceList(String deviceId) {
        String responseStr = iSpeakerService.getDevice(deviceId);
        return AjaxResult.success(JSONObject.parseObject(responseStr));
    }


    /***
     * 音乐播放
     * @return
     */
    @PostMapping("/playMusic")
    @ApiOperation(value = "音乐播放", notes = "音乐播放")
    public AjaxResult playMusic(@RequestParam(required = true) String ip, @RequestBody PlayVo playVo) {
        playVo.getParams().setTid(UUID.randomUUID().toString());
        String body = JSONObject.toJSON(playVo).toString();
        String responseStr = iSpeakerService.playMusic(ip, body);
        return AjaxResult.success(JSONObject.parseObject(responseStr));
    }


    /***
     * 文字转语音播放
     * @return
     */
    @PostMapping("/textPlayMusic")
    @ApiOperation(value = "文字转语音播放", notes = "文字转语音播放")
    public AjaxResult textPlayMusic(@RequestParam(required = true) String ip, @RequestParam(required = true) List<String> texts, @RequestBody PlayVo playVo) {
        playVo.getParams().setTid(UUID.randomUUID().toString());
        String responseStr = iSpeakerService.textParseVoice(ip, texts, playVo);
        return AjaxResult.success(JSONObject.parseObject(responseStr));
    }


    /***
     * 文字转语音播放
     * @return
     */
    @PostMapping("/textParseUrl")
    @ApiOperation(value = "文字转语音播放地址", notes = "文字转语音播放地址")
    public AjaxResult textParseUrl(@RequestParam(required = true) Integer speed, @RequestParam(required = true) Integer volume, @RequestParam(required = true) String text) {
        String responseStr = iSpeakerService.textParseVoiceURL(speed, volume, text);
        return AjaxResult.success(JSONObject.parseObject(responseStr));
    }


    /***
     * 文字转语音播放
     * @return
     */
    @RequestMapping("/textParseVideo")
    @ApiOperation(value = "文字转语音播放地址", notes = "文字转语音播放地址")
    public String textParseVideo(@RequestParam(required = true) Integer speed, @RequestParam(required = true) Integer volume, @RequestParam(required = true) String text) {
        String responseStr = iSpeakerService.textParseVoiceURL(speed, volume, text);
        return responseStr;
    }

    /***
     * 文字转语音播放
     * @return
     */
    @PostMapping("/textParseUrlIps")
    @ApiOperation(value = "文字转语音播放地址", notes = "文字转语音播放地址")
    public AjaxResult textParseUrl(@RequestParam(required = true) String text,@RequestBody List<PlayVo> playVo) {
        iSpeakerService.textParseVoiceIps(text,playVo);
        Map<String,Object> map=new HashMap<>();
        map.put("text",text);
        String date=DateUtils.parseDateToStr("HH:MM",new Date());
        map.put("date",date);
        return AjaxResult.success(map);
    }


    /***
     * 文字转语音播放
     * @return
     */
    @PostMapping("/textParseUrlAppIps")
    @ApiOperation(value = "文字转语音小程序播放地址", notes = "文字转语音小程序播放地址")
    public AjaxResult textParseUrlAppIps(@RequestParam(required = true) String text,@RequestBody List<PlayVo> playVo) {
        if(StringUtils.isEmpty(text) || playVo.size()==0){
            return AjaxResult.error("参数有误,请检查参数！");
        }
        text=text.replace("https","http");
        text=text.replace("https://lab.sxitdlc.com/jdlabSystem/","http://192.168.8.101/jdlabSystem/");
        iSpeakerService.textParseVoiceAppIps(text,playVo);
        Map<String,Object> map=new HashMap<>();
        map.put("text",text);
        String date=DateUtils.parseDateToStr("HH:MM",new Date());
        map.put("date",date);
        return AjaxResult.success(map);
    }

    /**
     * 停止播放
     * @param sn
     * @param ip
     * @return
     */
    @GetMapping("/stopPlayMusic")
    @ApiOperation(value = "文字转语音小程序播放地址", notes = "文字转语音小程序播放地址")
    public AjaxResult stopPlayMusic(@RequestParam(required = true) String sn,@RequestParam(required = true) String ip) {
        boolean istrue=iSpeakerService.stopPlayMusic(sn,ip);
        return AjaxResult.success(istrue);
    }
}
