package com.ruoyi.web.controller.im;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.basic.service.YjAppUserService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.im.domain.vo.FriendVO;
import com.ruoyi.im.domain.vo.OnlineTerminalVO;
import com.ruoyi.im.domain.vo.UserVO;
import com.ruoyi.im.service.FriendService;
import com.ruoyi.web.controller.im.result.Result;
import com.ruoyi.web.controller.im.result.ResultUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/friend")
@RequiredArgsConstructor
public class FriendController {

    private final FriendService friendService;
    private final YjAppUserService appUserService;
    @GetMapping("/list")
    @Operation(summary = "好友列表", description = "获取好友列表")
    public Result<Map> findFriends() {
        return ResultUtils.success(friendService.findAllFriends());
    }

//    @GetMapping("/find/{phonenumber}")
    @Operation(summary = "查找用户", description = "根据电话查找用户")
    public Result<UserVO> findById(@NotNull @PathVariable("phonenumber") String phonenumber) {
        //权限：课程顾问 店长 教练  查询客户信息
        //todo
        AppUser appUser=appUserService.getOne(new QueryWrapper<AppUser>().eq("phoneNumber",phonenumber));
         ResultUtils.success(appUser);
        return null;
    }

    @RepeatSubmit
    @PostMapping("/add")
    @Operation(summary = "添加好友", description = "双方建立好友关系")
    public Result addFriend(@NotNull(message = "好友id不可为空") @RequestParam Long friendId) {

        friendService.addFriend(friendId);
        return ResultUtils.success();
    }

    @GetMapping("/find/{friendId}")
    @Operation(summary = "查找好友信息", description = "查找好友信息")
    public Result<FriendVO> findFriend(@NotNull(message = "好友id不可为空") @PathVariable String friendId) {
        Long userId= SecurityUtils.getAppLoginUser().getAppUser().getId();
        Long fId=Long.valueOf(friendId);
        if (fId.equals(userId)) {
            return null;
        }
        return ResultUtils.success(friendService.findFriend(userId,fId));
    }


    @DeleteMapping("/delete/{friendId}")
    @Operation(summary = "删除好友", description = "解除好友关系")
    public Result delFriend(@NotNull(message = "好友id不可为空") @PathVariable Long friendId) {
        friendService.delFriend(friendId);
        return ResultUtils.success();
    }
    @GetMapping("/terminal/online")
    @Operation(summary = "判断用户哪个终端在线", description = "返回在线的用户id的终端集合")
    public Result<List<OnlineTerminalVO>> getOnlineTerminal(@NotNull @RequestParam("userIds") String userIds) {
        return ResultUtils.success(friendService.getOnlineTerminals(userIds));
    }
}
