package shop.alien.store.controller;

import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.vo.LifeFansVo;
import shop.alien.entity.store.vo.LifeMessageVo;
import shop.alien.store.service.LifeMessageService;

import java.util.List;

/**
 * 消息
 */
@Api(tags = {"三期-消息"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/message")
@RequiredArgsConstructor
public class LifeMessageController {

    private final LifeMessageService lifeMessageService;

    @ApiOperation("消息列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "receiverId", value = "当前登录人", dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "friendType", value = "聊天类型  0-搜索 1-聊过 2-未聊过", dataType = "Integer", paramType = "query"),
                    @ApiImplicitParam(name = "userName", value = "按展示名过滤（门店为店铺名、用户为昵称）", dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "search", value = "搜索关键词（昵称/店主姓名/店铺名）", dataType = "String", paramType = "query")
            }
                      )
    @GetMapping("/getMessageList")
    public R<List<LifeMessageVo>> getMessageList(@RequestParam String receiverId, @RequestParam int friendType, String search, String userName) throws Exception {
        log.info("LifeMessageController.getMessageList?receiverId={}, friendType={}, search={}", receiverId, friendType, search);
        return R.data(lifeMessageService.getMessageList(receiverId, friendType, search, userName));
    }

    @ApiOperation("未读消息与通知数量")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "receiverId", value = "当前登录人", dataType = "String", paramType = "query")})
    @GetMapping("/getAllNoReadCount")
    public R<Integer> getAllNoReadCount(@RequestParam String receiverId) throws Exception {
        log.info("LifeMessageController.getAllNoReadCount?receiverId={}", receiverId);
        return R.data(lifeMessageService.getAllNoReadCount(receiverId));
    }

    @ApiOperation("未聊过的消息数据")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "receiverId", value = "当前登录人", dataType = "String", paramType = "query")})
    @GetMapping("/getStrangerMessageNum")
    public R<LifeMessageVo> getStrangerMessageNum(@RequestParam String receiverId) throws Exception {
        log.info("LifeMessageController.getStrangerMessageNum?receiverId={}", receiverId);
        return R.data(lifeMessageService.getStrangerMessageNum(receiverId));
    }

    @ApiOperation("消息详情")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "receiverId", value = "当前登录人", dataType = "String", paramType = "query"), @ApiImplicitParam(name = "senderId", value = "聊天对方", dataType = "String", paramType = "query")})
    @GetMapping("/getMessageListByReceiverId")
    public R<JSONObject> getMessageListByReceiverId(@RequestParam String receiverId, @RequestParam String senderId) throws Exception {
        log.info("LifeMessageController.getMessageListByReceiverId?receiverId={}, senderId={}", receiverId, senderId);
        return R.data(lifeMessageService.getMessageListByReceiverId(receiverId, senderId));
    }

    @ApiOperation("是否是陌生人")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({@ApiImplicitParam(name = "receiverId", value = "当前登录人", dataType = "String", paramType = "query"), @ApiImplicitParam(name = "senderId", value = "聊天对方", dataType = "String", paramType = "query")})
    @GetMapping("/isStranger")
    public R<String> isStranger(@RequestParam String receiverId, @RequestParam String senderId) throws Exception {
        log.info("LifeMessageController.isStranger?receiverId={}, senderId={}", receiverId, senderId);
        return R.data(lifeMessageService.isStranger(receiverId, senderId));
    }

    @ApiOperation("消息已读")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({@ApiImplicitParam(name = "receiverId", value = "当前登录人", dataType = "String", paramType = "query"), @ApiImplicitParam(name = "senderId", value = "聊天对方", dataType = "String", paramType = "query")})
    @GetMapping("/read")
    public R<Boolean> read(@RequestParam String receiverId, @RequestParam String senderId) throws Exception {
        log.info("LifeMessageController.read?receiverId={}, senderId={}", receiverId, senderId);
        lifeMessageService.read(receiverId, senderId);
        return R.success("操作成功");
    }

    @ApiOperation("删除消息")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({@ApiImplicitParam(name = "senderId", value = "聊天对方", dataType = "String", paramType = "query"), @ApiImplicitParam(name = "receiverId", value = "当前登录人", dataType = "String", paramType = "query")})
    @GetMapping("/deleteMessageByPhoneId")
    public R<Integer> deleteMessageByPhoneId(@RequestParam String senderId, @RequestParam String receiverId) throws Exception {
        log.info("LifeMessageController.deleteMessageByPhoneId?senderId={},receiverId={}", senderId, receiverId);
        return R.data(lifeMessageService.deleteMessageByPhoneId(senderId, receiverId));
    }

    @ApiOperation("未关注人的消息数据")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({@ApiImplicitParam(name = "receiverId", value = "当前登录人", dataType = "String", paramType = "query")})
    @GetMapping("/getNoFriendMessage")
    public R<LifeMessageVo> getNoFriendMessage(@RequestParam String receiverId) throws Exception {
        log.info("LifeMessageController.getNoFriendMessageNum?receiverId={}", receiverId);
        return R.data(lifeMessageService.getNoFriendMessageNum(receiverId));
    }

    @ApiOperation("未读消息数量")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({@ApiImplicitParam(name = "receiverId", value = "当前登录人", dataType = "String", paramType = "query")})
    @GetMapping("/noReadCount")
    public R<Integer> noReadCount(@RequestParam String receiverId) throws Exception {
        log.info("LifeMessageController.noReadCount?receiverId={}", receiverId);
        return R.data(lifeMessageService.noReadCount(receiverId));
    }

    @ApiOperation("聊过天的用户信息")
    @ApiOperationSupport(order = 10)
    @GetMapping("/getTalkedUserList")
    public R<List<LifeFansVo>> getTalkedUserList() throws Exception {
        log.info("LifeMessageController.getTalkedUserList");
        return R.data(lifeMessageService.getTalkedUserList());
    }

    @ApiOperation("聊天用户是否已被封禁")
    @ApiOperationSupport(order = 11)
    @GetMapping("/isBanned")
    public R<Boolean> isBanned(String phoneId) throws Exception {
        log.info("LifeMessageController.isBanned");
        return R.data(lifeMessageService.isBanned(phoneId));
    }

}