package com.sqx.modules.chat.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.chat.dao.ChatContentDao;
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.chats.vo.ChatContextVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class ChatContentServiceImpl extends ServiceImpl<ChatContentDao, ChatContent> implements ChatContentService {
    @Autowired
    private UserService userService;
    @Autowired
    private ChatConversationService conversationService;

    @Override
    public PageUtils selectChatContentPage(Long userId, Integer page, Integer limit, Long chatConversationId, String content) {
        Integer userType = null;
        UserEntity userEntity = userService.getById(userId);
        if (userEntity != null) {
            userType = userEntity.getUserType();
        }
        return new PageUtils(baseMapper.selectChatContentPage(new Page<>(page, limit), chatConversationId, content, userType));
    }

    @Override
    public int updateChatContentStatusByUserIdAndChatId(Long userId, Long chatConversationId) {
        return baseMapper.updateChatContentStatusByUserIdAndChatId(userId, chatConversationId);
    }

    @Override
    public int selectChatCount(Long userId) {
        UserEntity userEntity = userService.getById(userId);
        if (userEntity!=null){
            Integer userType = userEntity.getUserType();
            return baseMapper.selectChatCount(userId, userType);
        }else {
            return 0;
        }


    }

    @Override
    public Result sendPhoneOrWx(Integer messageType, Long chatContentId) {
        ChatContent chatContent = baseMapper.selectById(chatContentId);
        if (chatContent == null) {
            return Result.error("消息未找到,请重试");
        }
        ChatConversation chatConversation = conversationService.getById(chatContent.getChatConversationId());
        if (chatConversation == null) {
            return Result.error("聊天会话未找到,请重试");
        }
        //将求职者和企业用户绑定的用户的手机号或微信号添加至消息内容
        ChatContextVo chatContextVo = new ChatContextVo();
        UserEntity userEntity = userService.getById(chatConversation.getUserId());
        UserEntity focusedUserEntity = userService.getById(chatConversation.getFocusedUserId());
        if (messageType == 5) {
            if (userEntity.getPhone() == null || focusedUserEntity.getPhone() == null) {
                return Result.error("对方或你未绑定手机号码,请添加后重试");
            }
            chatContextVo.setUserPhone(userEntity.getPhone());
            chatContextVo.setCompanyPhone(focusedUserEntity.getPhone());
            chatConversation.setIsSendPhone(1);
        } else {
            if ("".equals(userEntity.getWeChatNum()) || "".equals(focusedUserEntity.getWeChatNum())) {
                return Result.error("对方或你未绑定微信,请添加后重试");
            }
            chatContextVo.setUserWx(userEntity.getWeChatNum());
            chatContextVo.setCompanyWx(focusedUserEntity.getWeChatNum());
            chatConversation.setIsSendWx(1);
        }
        conversationService.updateById(chatConversation);

        chatContextVo.setMessageType(messageType);
        chatContent.setContent(JSONObject.toJSONString(chatContextVo));
        baseMapper.updateById(chatContent);

        //互相再发送一次
        if (messageType == 5) {
            chatContent.setMessageType("7");
        } else {
            chatContent.setMessageType("8");
        }
        chatContent.setChatContentId(null);
        chatContent.setUserId(chatConversation.getUserId());
        baseMapper.insert(chatContent);

        chatContent.setChatContentId(null);
        chatContent.setUserId(chatConversation.getFocusedUserId());
        baseMapper.insert(chatContent);


        return Result.success();
    }

    @Override
    public Result refuseReqs(Long chatContentId) {

        ChatContent chatContent = baseMapper.selectById(chatContentId);

        chatContent.setMessageType("12");

        return Result.upStatus(baseMapper.updateById(chatContent));

    }

    @Override
    public ChatContent selectUserChatVideo(Long userId){
        return baseMapper.selectUserChatVideo(userId);
    }

    @Override
    public Result updateChatContent(Long chatContentId,Integer videoStatus,String voiceLength){
        ChatContent chatContent = baseMapper.selectById(chatContentId);
        chatContent.setVideoStatus(videoStatus);
        chatContent.setVoiceLength(voiceLength);
        baseMapper.updateById(chatContent);
        return Result.success();
    }

}