package com.gree.mall.miniapp.controller.order;

import com.gree.mall.miniapp.bean.order.ShoppingCartBean;
import com.gree.mall.miniapp.bean.order.ShoppingCartDetail;
import com.gree.mall.miniapp.bean.order.ShoppingCartList;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.order.ShoppingCartLogic;
import com.gree.mall.miniapp.plus.entity.MiniNotice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Slf4j
@RestController
@Api(value = "APP师傅端-购物车", tags ={"APP师傅端-购物车"} )
@RequestMapping(value = "/shpping/cart", produces = "application/json; charset=utf-8")
public class ShoppingCartController {

    @Autowired
    ShoppingCartLogic shoppingCartLogic;

    @GetMapping("/notice")
    @ApiOperation("消息通知")
    public ResponseHelper<String> notice(HttpServletRequest request){
        return ResponseHelper.success(shoppingCartLogic.miniNotice(request));
    }


    @GetMapping("/list")
    @ApiOperation("购物车列表")
    public ResponseHelper<ShoppingCartDetail> list(
            @ApiParam(value = "用户id",required = true) @RequestParam String userId
    ) throws InterruptedException {
        ShoppingCartDetail shoppingCartLists = shoppingCartLogic.queryShoppingCart(userId);
        return ResponseHelper.success(shoppingCartLists);
    }

    @PostMapping("/add/one")
    @ApiOperation("商品详情添加商品购物车")
    public ResponseHelper<ShoppingCartDetail> addOne(HttpServletRequest request,@RequestBody ShoppingCartBean shoppingCartBean) throws RemoteServiceException, InterruptedException {
        ShoppingCartDetail shoppingCartDetail = shoppingCartLogic.addOne(request, shoppingCartBean);
        return ResponseHelper.success(shoppingCartDetail);
    }


    @PostMapping("/add")
    @ApiOperation("添加到购物车")
    public ResponseHelper<ShoppingCartDetail> addShoppingCart(HttpServletRequest request,@RequestBody ShoppingCartBean shoppingCartBean) throws RemoteServiceException, InterruptedException {
        ShoppingCartDetail shoppingCartDetail = shoppingCartLogic.addShoppingCart(request , shoppingCartBean);
        return ResponseHelper.success(shoppingCartDetail);
    }

    @PostMapping("/clear")
    @ApiOperation("清空购物车")
    public ResponseHelper clearShoppingCart(
            @ApiParam(value = "用户id",required = true) @RequestParam String userId
    ){
        shoppingCartLogic.clearShoppingCart(userId);
        return ResponseHelper.success();
    }

    @PostMapping("/remove")
    @ApiOperation("删除购物车商品")
    public ResponseHelper<ShoppingCartDetail> clearShoppingCart(
            @ApiParam(value = "用户id",required = true) @RequestParam String userId,
            @ApiParam(value = "购物车ids",required = true) @RequestParam List<String> shoppingCartIds
    ) throws InterruptedException {
        ShoppingCartDetail remove = shoppingCartLogic.remove(userId, shoppingCartIds);
        return ResponseHelper.success(remove);
    }

}
