package com.ruoyi.web.controller.im;


import com.ruoyi.im.domain.Friend;
import com.ruoyi.im.domain.dto.PrivateMessageDTO;
import com.ruoyi.im.domain.vo.FriendVO;
import com.ruoyi.im.domain.vo.PrivateMessageVO;
import com.ruoyi.im.service.FriendService;
import com.ruoyi.im.service.PrivateMessageService;
import com.ruoyi.mall.domain.Product;
import com.ruoyi.mall.domain.order.OrderItem;
import com.ruoyi.mall.service.ProductService;
import com.ruoyi.mall.service.impl.OrderItemService;
import com.ruoyi.mall.service.impl.OrderService;
import com.ruoyi.web.controller.im.result.Result;
import com.ruoyi.web.controller.im.result.ResultUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Tag(name = "私聊消息")
@RestController
@RequestMapping("/api/message/private")
@RequiredArgsConstructor
public class PrivateMessageController {

    private final PrivateMessageService privateMessageService;

    private final ProductService productService;

    private final OrderService orderService;
    private final OrderItemService orderItemService;

    @PostMapping("/send")
    @Operation(summary = "发送消息", description = "发送私聊消息")
    public Result<PrivateMessageVO> sendMessage(@Valid @RequestBody PrivateMessageDTO vo) {
        return ResultUtils.success(privateMessageService.sendMessage(vo));
    }

    /**
     *
     * @param minId 最大已读消息的id
     * @return
     */
    @GetMapping("/pullOfflineMessage")
    @Operation(summary = "拉取离线消息", description = "拉取离线消息,消息将通过webscoket异步推送")
    public Result pullOfflineMessage(@RequestParam String minId) {
        privateMessageService.pullOfflineMessage(Long.parseLong(minId.toString()));
        return ResultUtils.success();
    }

    @PutMapping("/readed")
    @Operation(summary = "消息已读", description = "将会话中接收的消息状态置为已读")
    public Result readedMessage(@RequestParam String friendId) {
        privateMessageService.readedMessage(Long.parseLong(friendId.toString()));
        return ResultUtils.success();
    }


    @GetMapping("/maxReadedId")
    @Operation(summary = "获取最大已读消息的id", description = "获取某个会话中已读消息的最大id")
    public Result<Long> getMaxReadedId(@RequestParam String friendId) {
        return ResultUtils.success(privateMessageService.getMaxReadedId(Long.parseLong(friendId.toString())));
    }

//    @GetMapping("/history")
    @Operation(summary = "查询聊天记录", description = "查询聊天记录")
    public Result<List<PrivateMessageVO>> recallMessage(
            @NotNull(message = "好友id不能为空") @RequestParam Long friendId,
            @NotNull(message = "页码不能为空") @RequestParam Long page,
            @NotNull(message = "size不能为空") @RequestParam Long size) {
        return ResultUtils.success(privateMessageService.findHistoryMessage(friendId, page, size));
    }

//    选择咨询商品/订单
    @PostMapping("/getConsultProduct")
    public Result getConsultProduct(@RequestBody Friend friend) {
        Long storeId= friend.getStoreId();
        return ResultUtils.success(privateMessageService.getConsultProduct(storeId));
    }

    //商品信息
    @PostMapping("/getProductForIm")
    public Result getProduct(@RequestBody Map map) {
       Long productId= Long.parseLong(map.get("productId").toString());
        return ResultUtils.success(productService.getById(productId));
    }
    //订单信息
    @PostMapping("/getOrderForIm")
    public Result getOrder(@RequestBody Map map) {
        Long orderItemId= Long.parseLong(map.get("orderItemId").toString());
        return ResultUtils.success(orderItemService.getById(orderItemId));
    }

}

