/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.yshop.module.product.controller.app.product;


import cn.hutool.core.util.ObjectUtil;
import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.module.product.controller.app.product.param.AppStoreProductQueryParam;
import co.yixiang.yshop.module.product.controller.app.product.vo.*;
import co.yixiang.yshop.module.product.enums.product.ProductEnum;
import co.yixiang.yshop.module.product.service.storeproduct.AppStoreProductService;
import co.yixiang.yshop.module.product.service.storeproductreply.AppStoreProductReplyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * <p>
 * 商品控制器
 * </p>
 *
 * @author hupeng
 * @since 2023-6-12
 */
@Slf4j
@RestController
@Tag(name = "用户 APP - 商品")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/product")
public class AppStoreProductController {

    private final AppStoreProductService storeProductService;
    private final AppStoreProductReplyService appStoreProductReplyService;


    /**
     * 获取首页产品
     */
    @GetMapping("/shop/index")
    @Operation(summary = "获取首页信息")
    public CommonResult<AppIndexVo> index(){
        AppIndexVo indexVo = AppIndexVo.builder()
                .firstList(storeProductService.getList(1,3, ProductEnum.TYPE_3.getValue()))
                .likeInfo(storeProductService.getList(1,3,ProductEnum.TYPE_2.getValue()))
                .build();
        return success(indexVo);
    }




    /**
     * 获取产品列表
     */
    @GetMapping("/products")
    @Operation(summary = "商品列表")
    public CommonResult<List<AppStoreProductRespVo>> goodsList(AppStoreProductQueryParam productQueryParam){
        return success(storeProductService.getGoodsList(productQueryParam));
    }



    /**
     * 为你推荐
     */
    @GetMapping("/hot")
    @Operation(summary = "为你推荐")
    public CommonResult<List<AppStoreProductRespVo>> productRecommend(){
        return success(storeProductService.getList(1, 4,
                ProductEnum.TYPE_2.getValue()));
    }



    /**
     * 普通商品详情
     */
    @GetMapping("/detail/{id}")
    @Operation(summary = "普通商品详情")
    @Parameter(name = "id", description = "商品ID", required = true, example = "1024")
    public CommonResult<AppProductVo> detail(@PathVariable long id){
        long uid = 0;
        System.out.println("uid:"+getLoginUserId());
        if(ObjectUtil.isNotNull(getLoginUserId())) {
            uid = getLoginUserId();
        }
        AppProductVo productVo = storeProductService.goodsDetail(id,uid);
        return success(productVo);
    }


    /**
     * 获取产品评论
     */
    @GetMapping("/reply/list/{id}")
    @Parameters({
            @Parameter(name = "id", description = "商品ID",
                    required = true, example = "1"),
            @Parameter(name = "type", description = "评论分数类型 0-全部 1-好评 2-中评 3-差评",
                    required = true, example = "1"),
            @Parameter(name = "page", description = "页码,默认为1",
                    required = true, example = "1"),
            @Parameter(name = "limit", description = "页大小,默认为10",
                    required = true, example = "10      ")
    })
    @Operation(summary = "获取产品评论")
    public CommonResult<List<AppStoreProductReplyQueryVo>> replyList(@PathVariable Long id,
                                                                  @RequestParam(value = "type",defaultValue = "0") int type,
                                                                  @RequestParam(value = "page",defaultValue = "1") int page,
                                                                  @RequestParam(value = "limit",defaultValue = "10") int limit){
        return success(appStoreProductReplyService.getReplyList(id,type, page,limit));
    }

    /**
     * 获取产品评论数据
     */
    @GetMapping("/reply/config/{id}")
    @Parameter(name = "id", description = "商品ID", required = true, example = "1")
    @Operation(summary = "获取产品评论数据")
    public CommonResult<AppReplyCountVo> replyCount(@PathVariable Integer id){
        return success(appStoreProductReplyService.getReplyCount(id));
    }



}

