package com.liquidnet.service.kylin.controller;

import com.github.pagehelper.PageInfo;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.kylin.dto.vo.groupbuy.*;
import com.liquidnet.service.kylin.service.IKylinGroupBuyActivityService;
import com.liquidnet.service.kylin.service.IKylinGroupBuyOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "前端-拼团")
@RestController
@RequestMapping("/groupBuy")
@Validated
public class KylinGroupBuyController {

    @Autowired
    private IKylinGroupBuyActivityService activityService;
    @Autowired
    private IKylinGroupBuyOrderService orderService;

    @GetMapping("/activities")
    @ApiOperation("演出下进行中的拼团活动")
    public ResponseDto<List<KylinGroupBuyActivityVo>> activities(
            @RequestParam("performanceId") @NotNull(message = "演出ID不能为空") String performanceId) {
        return activityService.activities(performanceId);
    }

    @GetMapping("/orders/pending")
    @ApiOperation("待成团公开拼单")
    public ResponseDto<List<KylinGroupBuyOrderCardVo>> pending(
            @RequestParam("activityId") @NotNull(message = "活动ID不能为空") String activityId,
            @RequestParam("timeId") @NotNull(message = "场次ID不能为空") String timeId,
            @RequestParam("ticketId") @NotNull(message = "票种ID不能为空") String ticketId) {
        return orderService.pending(activityId, timeId, ticketId);
    }

    @GetMapping("/my")
    @ApiOperation("我的拼团列表")
    public ResponseDto<PageInfo<KylinGroupBuyMyGroupVo>> my(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        return orderService.my(page, size);
    }

    @GetMapping("/detail")
    @ApiOperation("拼团详情")
    public ResponseDto<KylinGroupBuyOrderDetailVo> detail(@RequestParam(value = "groupOrderId", required = false) String groupOrderId,
                                                          @RequestParam(value = "shareToken", required = false) String shareToken) {
        return orderService.detail(groupOrderId, shareToken);
    }

    @PostMapping("/cancel")
    @ApiOperation("团长取消拼团")
    public ResponseDto<Boolean> cancel(@RequestParam("groupOrderId") @NotNull(message = "拼团订单ID不能为空") String groupOrderId) {
        return orderService.cancel(groupOrderId);
    }

    @PostMapping("/refreshShare")
    @ApiOperation("刷新邀请链接")
    public ResponseDto<KylinGroupBuyRefreshShareVo> refreshShare(
            @RequestParam("groupOrderId") @NotNull(message = "拼团订单ID不能为空") String groupOrderId) {
        return orderService.refreshShare(groupOrderId);
    }

    @GetMapping("/price")
    @ApiOperation("查询拼团价")
    public ResponseDto<KylinGroupBuyPriceVo> price(
            @RequestParam("activityId") @NotNull(message = "活动ID不能为空") String activityId,
            @RequestParam("timeId") @NotNull(message = "场次ID不能为空") String timeId,
            @RequestParam("ticketId") @NotNull(message = "票种ID不能为空") String ticketId) {
        return activityService.price(activityId, timeId, ticketId);
    }
}
