package com.liquidnet.service.goblin.controller;

import com.github.pagehelper.PageInfo;
import com.liquidnet.commons.lang.util.CurrentUtil;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.base.codec.vo.EncryptedReq;
import com.liquidnet.service.goblin.dto.manage.GoblinOrderParam;
import com.liquidnet.service.goblin.dto.vo.*;
import com.liquidnet.service.goblin.param.GoblinAppOrderRefundParam;
import com.liquidnet.service.goblin.param.PayAgainParam;
import com.liquidnet.service.goblin.param.SyncOrderParam;
import com.liquidnet.service.goblin.service.IGoblinOrderAppService;
import com.liquidnet.service.goblin.service.IGoblinOrderService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Slf4j
@Api(tags = "订单相关-App")
@RestController
@RequestMapping("/order")
public class GoblinOrderAppController {

    @Autowired
    IGoblinOrderAppService goblinOrderAppService;

    @PostMapping("list")
    @ApiOperation("列表")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "Integer", name = "page", value = "页数", example = "1"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "pre", value = "活动前缀[ZZ-正在下单|不传为普通订单]", example = "ZZ"),
    })
    public ResponseDto<PageInfo<GoblinAppOrderListVo>> orderList(@RequestParam("page") @Valid int page,
                                                                 @RequestParam(value = "pre", required = false) String pre) {
        return goblinOrderAppService.orderList(page, pre);
    }

    @PostMapping("listByCode")
    @ApiOperation("列表[根据masterCode]")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderMasterCode", value = "主订单号"),
    })
    public ResponseDto<List<GoblinAppOrderListVo>> orderListByMaster(@RequestParam("orderMasterCode") @Valid String orderMasterCode) {
        return goblinOrderAppService.orderListByMaster(orderMasterCode);
    }

    @PostMapping("details")
    @ApiOperation("详情")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id", example = "1"),
    })
    public ResponseDto<GoblinAppOrderDetailsVo> orderDetails(@RequestParam("orderId") @Valid String orderId) {
        String uid = CurrentUtil.getCurrentUid();
        return goblinOrderAppService.orderDetails(orderId, uid);
    }

    @GetMapping("couponState")
    @ApiOperation(value = "券类商品订单查询已发放券状态", notes = "响应参数[1-可用,5-已用]")
    public ResponseDto<Integer> couponState(@NotBlank(message = "子订单Id不能为空") @RequestParam String orderSkuId) {
        return ResponseDto.success(goblinOrderAppService.couponState(orderSkuId, CurrentUtil.getCurrentUid()));
    }

    @PostMapping("getProduce")
    @ApiOperation("已收货")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id", example = "1"),
    })
    public ResponseDto<Boolean> getProduce(@RequestParam("orderId") @Valid String orderId) {
        String uid = CurrentUtil.getCurrentUid();
        return goblinOrderAppService.getProduce(orderId, uid);
    }

    @PostMapping("applyRefund")
    @ApiOperation("退款申请")
    public ResponseDto<Boolean> applyRefund(@RequestBody GoblinAppOrderRefundParam param) {
        return goblinOrderAppService.applyRefund(param);
    }

    @PostMapping("againRefund")
    @ApiOperation("再次退款申请")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderBackId", value = "退款订单id", example = "1"),
    })
    public ResponseDto<Boolean> againRefund(@RequestBody GoblinAppOrderRefundParam param) {
        return goblinOrderAppService.againRefund(param);
    }

    @PostMapping("refundDetails")
    @ApiOperation("退款详情")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id", example = "1"),
    })
    public ResponseDto<List<GoblinBackOrderVo>> refundDetails(@RequestParam("orderId") @Valid String orderId) {
        return goblinOrderAppService.refundDetails(orderId);
    }

    @PostMapping("mailData")
    @ApiOperation("快递详情")
    @ApiResponse(code = 200, message = "接口返回对象参数")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "orderId", example = "1"),
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "mailId", value = "mailId", example = "1"),
    })
    public ResponseDto<List<GoblinMailVo>> getMail(@RequestParam("orderId") @Valid String orderId,
                                                   @RequestParam("mailId") @Valid String mailId) {
        return goblinOrderAppService.getMail(orderId, mailId);
    }

    @PostMapping("logistics/detail")
    @ApiOperation("物流轨迹详情(文字轨迹+地图)")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id"),
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "mailId", value = "快递id"),
    })
    public ResponseDto<GoblinMailTrackCache> logisticsDetail(@RequestParam("orderId") @Valid String orderId,
                                                             @RequestParam("mailId") @Valid String mailId) {
        return goblinOrderAppService.logisticsDetail(orderId, mailId, CurrentUtil.getCurrentUid());
    }

    @PostMapping("store/recommendGoods")
    @ApiOperation("同店最新上架商品推荐(前2个)")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id"),
    })
    public ResponseDto<List<GoblinStoreRecommendGoodsVo>> recommendStoreGoods(@RequestParam("orderId") @Valid String orderId) {
        return goblinOrderAppService.recommendStoreGoods(orderId, CurrentUtil.getCurrentUid());
    }
}
