package com.example.system.chat;

import com.example.system.chat.model.Friend;
import com.example.system.chat.service.FriendService;
import com.example.system.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/friend")
public class FriendController {
    @Autowired
    private FriendService friendService;
    /**
     * 获取好友列表
     * @return 返回好友列表
     */
    @RequestMapping("/getList")
    public List<Friend> getList(HttpServletRequest request) {
        log.info("获取好友列表");
        String userToken =request.getHeader("Authorization");
        Claims claims = JWTUtil.checkToken(userToken);

        return friendService.getFriendList((Integer) claims.get("userId"));
    }
    /**
     * 添加好友
     */
    @RequestMapping("/addFriend")
    public Object addFriend(Integer friendId, String friendName, HttpServletRequest request) {
        log.info("添加好友 friendId: {}", friendId);
        String userToken =request.getHeader("Authorization");
        Claims claims = JWTUtil.checkToken(userToken);
        Integer userId = (Integer) claims.get("userId");
        String userName = (String) claims.get("userName");
        return friendService.addFriend(friendId, friendName, userId, userName);
    }
}
