package com.qxgmat.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.nuliji.tools.Response;
import com.nuliji.tools.ResponseHelp;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.pay.data.PayRequestData;
import com.nuliji.tools.pay.data.PayResponseData;
import com.qxgmat.data.constants.enums.SettingKey;
import com.qxgmat.data.constants.enums.trade.PayChannel;
import com.qxgmat.data.dao.entity.*;
import com.qxgmat.dto.request.PayProductDto;
import com.qxgmat.help.ShiroHelp;
import com.qxgmat.service.extend.TradeService;
import com.qxgmat.service.inline.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/base")
@Api(tags = "商城接口", description = "商城相关接口", produces = MediaType.APPLICATION_JSON_VALUE)
public class ShopController {

    @Autowired
    private ShiroHelp shiroHelp;

    @Autowired
    private SettingService settingService;

    @Autowired
    private TradeService tradeService;

    @Autowired
    private PayService payService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "商品信息", httpMethod = "GET")
    private Response<JSONObject> index(){
        Setting entity = settingService.getByKey(SettingKey.INDEX);
        return ResponseHelp.success(entity.getValue());
    }

    @RequestMapping(value = "/pay/wechat_qr", method = RequestMethod.POST)
    @ApiOperation(value = "通过微信二维码支付", notes = "通过微信二维码支付", httpMethod = "POST")
    public Response<PayResponseData> wechatQr(@RequestBody @Validated PayProductDto dto, HttpServletRequest request) throws Exception {
        User user = (User) shiroHelp.getLoginUser();
        PayResponseData data = tradeService.pay(user.getId(), "", "", dto.getModule(), dto.getModuleExtend(), BigDecimal.valueOf(0), PayChannel.WECHAT_QR, request);
        return ResponseHelp.success(data);
    }

    @RequestMapping(value = "/pay/wechat_js", method = RequestMethod.POST)
    @ApiOperation(value = "通过微信内支付", notes = "通过微信内支付", httpMethod = "POST")
    public Response<PayResponseData> wechatJs(@RequestBody @Validated PayProductDto dto, HttpServletRequest request) throws Exception {
        User user = (User) shiroHelp.getLoginUser();
        PayResponseData data = tradeService.pay(user.getId(), "", "", dto.getModule(), dto.getModuleExtend(), BigDecimal.valueOf(0), PayChannel.WECHAT_JS, request);
        return ResponseHelp.success(data);
    }

    @RequestMapping(value = "/pay/alipay_qr", method = RequestMethod.POST)
    @ApiOperation(value = "通过支付宝二维码支付", notes = "通过支付宝二维码支付", httpMethod = "POST")
    public Response<PayResponseData> alipayQr(@RequestBody @Validated PayProductDto dto, HttpServletRequest request) throws Exception {
        User user = (User) shiroHelp.getLoginUser();
        PayResponseData data = tradeService.pay(user.getId(), "", "", dto.getModule(), dto.getModuleExtend(), BigDecimal.valueOf(0), PayChannel.ALIPAY_QR, request);
        return ResponseHelp.success(data);
    }

    @RequestMapping(value = "/pay/query", method = RequestMethod.GET)
    @ApiOperation(value = "支付结果查询", notes = "支付结果查询", httpMethod = "GET")
    public Response<Boolean> response(
            @RequestParam(required = true, name="id") Long payId
    ) {
        Pay pay = payService.get(payId);
        User user = (User) shiroHelp.getLoginUser();

        if (!pay.getUserId().equals(user.getId())){
            throw new ParameterException("支付信息不存在");
        }
        return ResponseHelp.success(payService.isPayed(pay));
    }
}
