package com.sqx.modules.chat.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.chat.entity.ChatContent;
import com.sqx.modules.chat.entity.ChatConversation;
import com.sqx.modules.chat.service.ChatContentService;
import com.sqx.modules.chat.service.ChatConversationService;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.resumes.dao.ResumesDao;
import com.sqx.modules.resumes.entity.ResumesListDto;
import com.sqx.modules.utils.TLSSigAPIv2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@Api(value = "聊天", tags = {"聊天"})
@RequestMapping(value = "/app/chat")
public class AppChatController {

    @Autowired
    private ChatContentService chatContentService;
    @Autowired
    private ChatConversationService chatConversationService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private ResumesDao resumesDao;

    @Login
    @GetMapping("/selectChatConversationPage")
    @ApiOperation("获取聊天会话")
    public Result selectChatConversationPage(Integer page,Integer limit,@RequestAttribute("userId") Long userId){
        return Result.success().put("data",chatConversationService.selectChatConversationPage(page, limit, userId,null));
    }

    @Login
    @PostMapping("/insertChatConversation")
    @ApiOperation("发起聊天")
    public Result insertChatConversation(@RequestBody ChatConversation chatConversation){
        return chatConversationService.insertChatConversations(chatConversation);
    }

    @Login
    @GetMapping("/selectChatContent")
    @ApiOperation("获取聊天记录")
    public Result selectChatContent(Integer page,Integer limit,Long chatConversationId,@RequestAttribute("userId") Long userId){
        //清空未读消息
        chatContentService.updateChatContentStatusByUserIdAndChatId(userId, chatConversationId);
        ChatConversation byId = chatConversationService.getById(chatConversationId);
        if(byId!=null){
            boolean flag=false;
            if(String.valueOf(byId.getIsWxMsg()).equals(String.valueOf(userId))){
                byId.setIsWxMsg(0);
                flag=true;
            }
            if(String.valueOf(byId.getIsSendMsg()).equals(String.valueOf(userId))){
                byId.setIsSendMsg(0);
                flag=true;
            }
            if(flag){
                chatConversationService.updateById(byId);
            }
        }
        return Result.success().put("data",chatContentService.selectChatContentPage(userId,page, limit, chatConversationId,null));
    }

    @Login
    @GetMapping("/selectChatCount")
    @ApiOperation("获取未读消息数量")
    public Result selectChatCount(@RequestAttribute("userId") Long userId){
        int chatCount = chatContentService.selectChatCount(userId);
        int messageCount = messageService.count(new QueryWrapper<MessageInfo>().eq("is_see", 0).eq("user_id", userId));
        Map<String,Object> result=new HashMap<>();
        result.put("chatCount",chatCount);
        result.put("messageCount",messageCount);
        return Result.success().put("data",result);
    }

    @Login
    @PostMapping("/deleteChatContentById")
    @ApiOperation("删除某一句聊天记录")
    public Result deleteChatContentById(Long chatContentId){
        chatContentService.removeById(chatContentId);
        return Result.success();
    }
    @Login
    @PostMapping("/sendPhoneOrWx")
    @ApiOperation("互相发送手机号或微信号")
    public Result sendPhoneOrWx(Integer messageType,Long chatContentId){
        return chatContentService.sendPhoneOrWx(messageType,chatContentId);
    }
    @Login
    @ApiOperation("获取消息对话信息")
    @GetMapping("getConversationInfo")
    public Result getConversationInfo(Long chatConversationId){
        return Result.success().put("data", chatConversationService.getById(chatConversationId));
    }
    @Login
    @ApiOperation("拒绝(通用)")
    @GetMapping("refuseReqs")
    public Result refuseReqs(Long chatContentId){
        return chatContentService.refuseReqs(chatContentId);
    }

    @Login
    @PostMapping("/insertChatConversationByChatVideo")
    @ApiOperation("发起视频或语音")
    public Result insertChatConversationByChatVideo(@RequestAttribute Long userId,@RequestBody ChatConversation chatConversation){
        return chatConversationService.insertChatConversationByChatVideo(userId,chatConversation,chatConversation.getMessageType());
    }

    @Login
    @GetMapping("/selectUserChatVideo")
    @ApiOperation("查询是否有正在进行中的语音通话")
    public Result selectUserChatVideo(@RequestAttribute("userId") Long userId){
        return Result.success().put("data",chatContentService.selectUserChatVideo(userId));
    }

    @Login
    @GetMapping("/selectSign")
    @ApiOperation("生成权限")
    public Result selectSign(@RequestAttribute Long userId){
        String sdkappid = commonInfoService.findOne(814).getValue();
        String key = commonInfoService.findOne(813).getValue();
        String s = TLSSigAPIv2.genUserSig(String.valueOf(userId), 86400, Long.parseLong(sdkappid), key);
        return Result.success().put("data",s);
    }

    @Login
    @PostMapping("/updateChatContent")
    @ApiOperation("关闭房间")
    public Result updateChatContent(Long chatContentId,Integer videoStatus,String voiceLength){
        return chatContentService.updateChatContent(chatContentId, videoStatus, voiceLength);
    }

    @Login
    @GetMapping("/chatUser")
    @ApiOperation("获取和自己聊过天的列表")
    public Page<ResumesListDto> chatUser(@RequestAttribute("userId")Long userId, Integer limit, Integer page){
        List<ChatContent> list = chatContentService.getBaseMapper().selectList(new QueryWrapper<ChatContent>().eq("user_id",userId).orderByDesc("create_time"));

        Set<ResumesListDto> tempSet = new LinkedHashSet<>();
        for(ChatContent chatContent:list){
            ResumesListDto resumesListDto =new ResumesListDto();
            resumesListDto.setResumesId(chatContent.getResumesId());
           tempSet.addAll(resumesDao.selectResumesListHr(resumesListDto));
        }
        List<ResumesListDto> list1 = new ArrayList<>(tempSet);
        Integer currentPage = (page == null || page < 1) ? 1 : page;
        Integer pageSize = (limit == null || limit < 1) ? 10 : limit;
        Page<ResumesListDto> page1 =new Page<>(currentPage,pageSize);
        page1.setRecords(list1);
        return page1;
    }

}