package com.fs.qw.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.qw.domain.*;
import com.fs.qw.dto.QwMessageDTO;
import com.fs.qw.service.*;

import com.fs.qw.vo.QwForbiddenMessageListVO;
import com.fs.qw.vo.QwMessageListExportVO;
import com.fs.qw.vo.QwMessageListVO;

//import com.fs.websocket.bean.MsgBean;
//import com.fs.websocket.WebSocketServer;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/qw")
public class QwMessageController extends BaseController {
    @Autowired
    private IQwMaterialService qwMaterialService;

    @Autowired
    private IQwMessageService qwMessageService;

    @Autowired
    private IQwUserService qwUserService;

    @Autowired
    private IQwConversationService conversationService;

    @Autowired
    private IQwContactService qwContactService;

    @Autowired
    private IQwConversationService qwConversationService;


//    @Autowired
//    WebSocketServer webSocketServer;

    @ApiOperation("获取消息列表")
    @GetMapping("/message/getList")
    @PreAuthorize("@ss.hasPermi('qw:message:list')")
    public R getMessageList(QwMessageListParam param){
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<QwMessage> list  = new ArrayList<>();
        if(param.getMessageId()!=null){
            list = qwMessageService.selectQwMessageListContextVO(param);
        }else{
            list = qwMessageService.selectQwMessageListByTypeVO(param);
        }
        int isRoom = qwConversationService.selectConType(param.getConversationId());
        List<QwMessageListVO> listVOS=qwMessageService.getQwMessageListVO(list,param.getConversationId(),isRoom);
        PageInfo listPageInfo= new PageInfo(list);
        Collections.reverse(listVOS);
        listPageInfo.setList(listVOS);
        return R.ok().put("data",listPageInfo);
    }

    @Log(title = "消息导出", businessType = BusinessType.EXPORT)
    @GetMapping("/message/export")
    public AjaxResult export(QwMessageListParam param){
        List<QwMessageListExportVO> list = qwMessageService.selectMessageListExportVO(param.getConversationId());
        ExcelUtil<QwMessageListExportVO> util = new ExcelUtil<QwMessageListExportVO>(QwMessageListExportVO.class);
        QwConversation qwConversation = conversationService.selectQwConversationById(param.getConversationId());
        if (qwConversation.getType() == 0){
            JSONArray jsonArray = JSON.parseArray(qwConversation.getExt());
            StringBuilder nickNames = new StringBuilder();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (jsonObject.getString("remark")!=null&&!jsonObject.getString("remark").equals("")){
                    String nickName = jsonObject.getString("remark");
                    if (i > 0) {
                        nickNames.append("_");
                    }
                    nickNames.append(nickName);
                }else {
                    String nickName = jsonObject.getString("nickName");
                    if (i > 0) {
                        nickNames.append("_");
                    }
                    nickNames.append(nickName);
                }
            }
            return util.exportExcel(list, nickNames.toString());
        }

        JSONObject jsonStr = JSONObject.parseObject(qwConversation.getExt());
        if (jsonStr.getString("roomName") == null && jsonStr.getString("roomName").equals("")) {
            return util.exportExcel(list,"未命名的群聊");
        }
        return util.exportExcel(list, jsonStr.getString("roomName"));
    }

    @PostMapping("/message/text")
    public R sendMessageText(@RequestBody QwMessageDTO qwMessageDTO){
        return qwMessageService.sendMessageText(qwMessageDTO);
    }

    @PostMapping("/message/image")
    public R sendMessageImage(@RequestBody QwMessageDTO qwMessageDTO){
        return qwMessageService.sendMessageImage(qwMessageDTO);
    }

    @PostMapping("/message/voice")
    public R sendMessageVoice(@RequestBody QwMessageDTO qwMessageDTO){
        return qwMessageService.sendMessageVoice(qwMessageDTO);
    }

    @PostMapping("/message/revoke")
    public R messageRevoke(@RequestBody QwMessageDTO qwMessageDTO){
        return qwMessageService.messageRevoke(qwMessageDTO);
    }



}
