package cn.ysk.cashier.controller.product;

import cn.ysk.cashier.annotation.AnonymousAccess;
import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.cons.rabbit.RabbitConstants;
import cn.ysk.cashier.dto.shoptable.*;
import cn.ysk.cashier.pojo.order.TbCashierCart;
import cn.ysk.cashier.service.product.TbProductService;
import cn.ysk.cashier.service.shop.TbShopTableService;
import cn.ysk.cashier.utils.RabbitMsgUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.UUID;

@RestController
@RequestMapping("/api/place")
public class TbPlaceController {

    private final TbShopTableService tbShopTableService;

    private final TbProductService tbProductService;
    private final Validator validator;
    private final RabbitMsgUtils rabbitMsgUtils;


    @AnonymousAccess
    @GetMapping ("/activate")
    @ApiOperation("查询/product")
    public ResponseEntity<Object> queryActivateTbProductInfo(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "30") Integer size,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam Integer shopId,
            @RequestParam(required = false) Integer productId
    ){
        return new ResponseEntity<>(tbProductService.activateProduct(page, size, categoryId, shopId, productId),HttpStatus.OK);
    }

    public TbPlaceController(TbShopTableService tbShopTableService, TbProductService tbProductService, Validator validator, RabbitTemplate rabbitTemplate, RabbitMsgUtils rabbitMsgUtils) {
        this.tbShopTableService = tbShopTableService;
        this.tbProductService = tbProductService;
        this.validator = validator;
        this.rabbitTemplate = rabbitTemplate;
        this.rabbitMsgUtils = rabbitMsgUtils;
    }

    @AnonymousAccess
    @PostMapping("/addCart")
    @Log("代客下单:#addCartDTO.tableId")
    @ApiOperation("代客下单/shop/table")
    public ResponseEntity<TbCashierCart> addCartForUser(@Valid @RequestBody AddCartDTO addCartDTO) {
        return ResponseEntity.ok(tbShopTableService.addCartForUser(addCartDTO));
    }

    @AnonymousAccess
    @PutMapping("/updateCart")
    @Log("代客下单")
    @ApiOperation("代客下单/shop/table")
    public ResponseEntity<TbCashierCart> updateCart(@Valid @RequestBody UpdateCartDTO updateCartDTO) {
        return ResponseEntity.ok(tbShopTableService.updateCart(updateCartDTO));
    }

    @AnonymousAccess
    @PutMapping("/pack")
    @Log("代客下单")
    @ApiOperation("代客下单/shop/table")
    public ResponseEntity<TbCashierCart> pack(@Valid @RequestBody PackCartDTO packCartDTO) {
        tbShopTableService.pack(packCartDTO);
        return ResponseEntity.ok(null);
    }

    @AnonymousAccess
    @DeleteMapping("/removeCart")
    @Log("代客下单 删除购物车商品")
    @ApiOperation("代客下单 清空购物车 /shop/table")
    public ResponseEntity<Object> removeCart(@Validated @RequestBody RemoveCartDTO removeCartDTO) {
        tbShopTableService.removeCart(removeCartDTO);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @AnonymousAccess

    @DeleteMapping("/clearCart")
    @Log("代客下单 清空购物车")
    @ApiOperation("代客下单 清空购物车 /shop/table"/**/)
    public ResponseEntity<Object> clearCart(@Validated @RequestBody ClearCartDTO clearCartDTO) {
        tbShopTableService.clearCart(clearCartDTO);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @AnonymousAccess

    @GetMapping("/cart")
    @Log("代客下单 查询购物车")
    @ApiOperation("代客下单 查询购物车 /shop/table")
    public ResponseEntity<Object> getCart(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam Long tableId,
            @RequestParam Integer shopId,
            @RequestParam(required = false) Integer vipUserId,
            @RequestParam String masterId
    ) {
        return ResponseEntity.ok(tbShopTableService.getCart(tableId, page, size, shopId, vipUserId, masterId));
    }
    @AnonymousAccess

    @GetMapping("/pending/cart")
    @Log("代客下单 查询购物车")
    @ApiOperation("代客下单 查询购物车 /shop/table")
    public ResponseEntity<Object> getPendingCart(
            @RequestParam Integer shopId,
            @RequestParam String tableId
    ) {
        return ResponseEntity.ok(tbShopTableService.getCar(shopId, tableId));
    }

    @AnonymousAccess

    @GetMapping("/masterId")
    @Log("代客下单 查询购物车")
    @ApiOperation("代客下单 ")
    public ResponseEntity<Object> getMasterId(
            @RequestParam Integer shopId,
            @RequestParam Long tableId,
            @RequestParam(required = false) Integer vipUserId
    ) {
        return ResponseEntity.ok(tbShopTableService.getMasterId(shopId, tableId, vipUserId));
    }

    @AnonymousAccess

    @PostMapping("/order")
    @Log("代客下单 查询购物车")
    @ApiOperation("代客下单 查询购物车 /shop/table")
    public ResponseEntity<Object> createOrder(
           @RequestBody CreateOrderDTO createOrderDTO
    ) {
        return ResponseEntity.ok(tbShopTableService.createOrder(createOrderDTO, !createOrderDTO.isPostPay(), true));
    }

    @AnonymousAccess

    @PostMapping("/pending")
    @Log("代客下单 查询购物车")
    @ApiOperation("代客下单 查询购物车 /shop/table")
    public ResponseEntity<Object> pending(
            @RequestBody PendingDTO pendingDTO
    ) {
        pendingDTO.setIsPending(true);
        return ResponseEntity.ok(tbShopTableService.pending(pendingDTO));
    }

    @AnonymousAccess

    @GetMapping("/payType")
    @Log("代客下单 查询购物车")
    @ApiOperation("代客下单 查询购物车 /shop/table")
    public ResponseEntity<Object> getPayType(
            @RequestParam Integer shopId
    ) {
        return ResponseEntity.ok(tbShopTableService.getPayType(shopId));
    }

    @AnonymousAccess

    @DeleteMapping("/order")
    @Log("代客下单 删除订单")
    @ApiOperation("代客下单 查询购物车 /shop/table")
    public ResponseEntity<Object> delete(
            @Validated @RequestBody DeleteOrderDTO deleteOrderDTO
    ) {
        return ResponseEntity.ok(tbShopTableService.deleteOrder(deleteOrderDTO));
    }

    @AnonymousAccess

    @PutMapping("/pay")
    @Log("代客下单 查询购物车")
    @ApiOperation("代客下单 查询购物车 /shop/table")
    public ResponseEntity<Object> pay(
            @Validated @RequestBody PayDTO payDTO
    ) {

       return ResponseEntity.ok(tbShopTableService.pay(payDTO));
    }

    @AnonymousAccess
    @PutMapping("/updateVip")
    @Log("代客下单 查询购物车")
    @ApiOperation("代客下单 查询购物车 /shop/table")
    public ResponseEntity<Object> updateVip(
            @Validated @RequestBody UpdateVipDTO updateVipDTO
    ) {
        return ResponseEntity.ok(tbShopTableService.updateVip(updateVipDTO));
    }

    private final RabbitTemplate rabbitTemplate;
    @AnonymousAccess
@GetMapping("/test")
    public void test(
            @RequestParam Integer id
    ) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "create");
        jsonObject.put("orderId", id);
//        rabbitTemplate.convertAndSend(RabbitConstants.CART_ORDER_COLLECT_PUT, RabbitConstants.CART_ORDER_COLLECT_ROUTINGKEY_PUT, jsonObject.toJSONString(), new CorrelationData(UUID.randomUUID().toString()));
        rabbitMsgUtils.printTicket(String.valueOf(id));
//        rabbitTemplate.convertAndSend(RabbitConstants.PRINT_MECHINE_COLLECT_PUT, RabbitConstants.PRINT_MECHINE_COLLECT_ROUTINGKEY_PUT, id, new CorrelationData(UUID.randomUUID().toString()));

    }

}
