package cn.reghao.tnb.user.app.controller;

import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.auth.api.dto.AccountAvatar;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.user.app.model.dto.*;
import cn.reghao.tnb.user.app.service.ContactService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2022-04-16 20:43:30
 */
@Tag(name = "用户联系人接口")
@RestController
@RequestMapping("/api/user/contact")
@AuthUser
public class UserContactController {
    private final ContactService contactService;

    public UserContactController(ContactService contactService) {
        this.contactService = contactService;
    }

    @Operation(summary = "获取联系人列表", description = "N")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getContactList(@RequestParam("pn") int pageNumber) {
        List<AccountAvatar> list = contactService.getContactList(pageNumber);
        return WebResult.success(list);
    }

    @Operation(summary = "获取联系人列表", description = "N")
    @GetMapping(value = "/list1", produces = MediaType.APPLICATION_JSON_VALUE)
    public String contactList() {
        long loginUser = UserContext.getUserId();
        List<ContactInfo> list = contactService.getContactList(loginUser);
        return WebResult.success(list);
    }

    @Operation(summary = "查找联系人", description = "N")
    @GetMapping(value = "/search", produces = MediaType.APPLICATION_JSON_VALUE)
    public String contactSearch(@RequestParam("mobile") String mobile) {
        SearchContactResult contactDetail = contactService.searchUser(mobile);
        if (contactDetail == null) {
            return WebResult.failWithMsg("查无此人");
        }

        return WebResult.success(contactDetail);
    }

    @Operation(summary = "获取联系人信息", description = "N")
    @GetMapping(value = "/detail/{userId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String contactDetail(@PathVariable("userId") Long userId) {
        ContactInfoResult contactDetail = contactService.getContactInfoResult(userId);
        if (contactDetail == null) {
            return WebResult.failWithMsg("查无此人");
        }

        return WebResult.success(contactDetail);
    }

    @Operation(summary = "申请添加联系人", description = "N")
    @PostMapping(value = "/apply/create", produces = MediaType.APPLICATION_JSON_VALUE)
    public String contactApplyCreate(@RequestBody ContactAdd contactAdd) {
        Result result = contactService.createApply(contactAdd);
        return WebResult.result(result);
    }

    @Operation(summary = "获取联系人申请列表", description = "N")
    @GetMapping(value = "/apply/records", produces = MediaType.APPLICATION_JSON_VALUE)
    public String contactApplyRecords(@RequestParam("pageNumber") int pageNumber) {
        ContactApplyList pageList = contactService.getApplyList(pageNumber);
        return WebResult.success(pageList);
    }

    @Operation(summary = "获取联系人申请未读消息数目", description = "N")
    @GetMapping(value = "/apply/unread", produces = MediaType.APPLICATION_JSON_VALUE)
    public String contactApplyUnreadNum() {
        UnreadNum unreadNum = contactService.getApplyCount();
        return WebResult.success(unreadNum);
    }

    @Operation(summary = "回复消息", description = "N")
    @PostMapping(value = "/reply", produces = MediaType.APPLICATION_JSON_VALUE)
    public String contactReply(@RequestBody ContactReply contactReply) {
        return WebResult.success();
    }

    @Operation(summary = "接受联系人申请", description = "N")
    @PostMapping(value = "/apply/accept", produces = MediaType.APPLICATION_JSON_VALUE)
    public String contactApplyAccept(@RequestBody ContactAddRespond contactAddRespond) {
        contactService.acceptApply(contactAddRespond);
        return WebResult.success();
    }

    @Operation(summary = "拒绝联系人申请", description = "N")
    @PostMapping(value = "/apply/decline", produces = MediaType.APPLICATION_JSON_VALUE)
    public String contactApplyDecline(@RequestBody ContactAddRespond contactAddRespond) {
        contactService.declineApply(contactAddRespond);
        return WebResult.success();
    }

    @Operation(summary = "设置联系人备注", description = "N")
    @PostMapping(value = "/remark", produces = MediaType.APPLICATION_JSON_VALUE)
    public String contactEditRemark(@RequestBody @Validated ContactRemark contactRemark) {
        contactService.updateContactRemark(contactRemark);
        return WebResult.success();
    }

    @Operation(summary = "拉黑联系人", description = "N")
    @PostMapping(value = "/block", produces = MediaType.APPLICATION_JSON_VALUE)
    public String contactBlock(@RequestParam("friend_id") int friendId) {
        return WebResult.success();
    }

    @Operation(summary = "删除联系人", description = "N")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public String contactDelete(@RequestParam("friend_id") int friendId) {
        return WebResult.success();
    }
}
