package com.cirle.scientific.controller.pay;


import com.cirle.scientific.utils.IpUtil;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.cirle.scientific.pojo.BizCodeEnum;
import com.cirle.scientific.pojo.Result;
import com.cirle.scientific.service.OrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/pay")
@Api("支付相关接口")
public class PayController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/pay")
    @ApiOperation(value = "去往支付页")
    public String pay(
            HttpServletRequest request,
            @ApiParam(name = "from",value = "请求类型 web：网站 H5：手机端") @RequestParam(name = "from",required = true) String from,
            @ApiParam(name = "objType",value = "类型，1-精品课， 2-直播，3-会议，4-课件") @RequestParam(name = "objType",required = true) String objType,
            @ApiParam(name = "objId",value = "对象ID") @RequestParam(name = "objId",required = true) String objId,
            @ApiParam(name = "objCount",value = "对象数量") @RequestParam(name = "objCount",defaultValue = "1") Integer objCount,
            @ApiParam(name = "orderId",value = "订单ID") @RequestParam(name = "orderId",required = false) String orderId
    ){
        request.setAttribute("objType",objType);
        request.setAttribute("objId",objId);
        request.setAttribute("objCount",objCount);
        request.setAttribute("orderId",orderId);

        request.setAttribute("appId", request.getParameter("appId"));
        request.setAttribute("timeStamp", request.getParameter("timeStamp"));
        request.setAttribute("nonceStr", request.getParameter("nonceStr"));
        request.setAttribute("packages", request.getParameter("packages"));
        request.setAttribute("signType", request.getParameter("signType"));
        request.setAttribute("paySign", request.getParameter("paySign"));

        if (StringUtils.isNotBlank(from) && from.equals("H5")) {
            return "html5/pay/pay";
        } else {
            return "web/pay/pay";
        }
    }

    @RequestMapping(value = "/getObjInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result getObjInfo(
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @RequestParam Integer objType,
            @RequestParam Long objId,
            @RequestParam(required = false,defaultValue = "1") Integer objCount
    ) {
        if(memCard == null){
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }
        return orderService.getObjInfo(memCard, objType, objId, objCount);
    }


    @RequestMapping(value = "/getObjInfoWeb", method = RequestMethod.POST)
    @ResponseBody
    public Result getObjInfoWeb(
            HttpServletRequest request,
            @RequestParam Integer objType,
            @RequestParam Long objId,
            @RequestParam(required = false,defaultValue = "1") Integer objCount
    ) {
        String memCard = (String)request.getSession().getAttribute("UserCard");
        if(memCard == null){
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }
        return orderService.getObjInfo(memCard, objType, objId, objCount);
    }

    @RequestMapping(value = "/addFreeBuyApi", method = RequestMethod.POST)
    @ResponseBody
    public Result addFreeBuyApi(
            HttpServletRequest request,
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @RequestParam Integer orderType,
            @RequestParam Integer objId,
            @RequestParam Integer paySource,
            @RequestParam(required = false) String fromMemCard,
            @RequestParam(required = false,defaultValue = "1") Integer objCount,
            @RequestParam(required = false) String orderId
    ) {

        if(memCard == null && request.getSession().getAttribute("UserCard")==null){
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }
        memCard = memCard == null ? (String) request.getSession().getAttribute("UserCard") : memCard ;
        Result result = orderService.addFreeBuyApi(memCard, orderType, objId, paySource, fromMemCard,
                IpUtil.getIpAddr(request), objCount, orderId);
        return result;
    }
    @PostMapping("/pointPay")
    @ResponseBody
    @ApiModelProperty(value = "积分兑换")
    public Result pointPay(
            HttpServletRequest request,
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @RequestParam Integer objType,
            @RequestParam Integer objId,
            @RequestParam Integer paySource,
            @RequestParam(required = false) String fromMemCard,
            @RequestParam(required = false,defaultValue = "1") Integer objCount,
            @RequestParam(required = false) String orderId
    ) {
        if(memCard == null){
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }

        Result result = orderService.pointPay(memCard, objType, objId, paySource, fromMemCard,
                IpUtil.getIpAddr(request), objCount, orderId);
        if(result.getCode() == 200){
            //增加任务积分
            orderService.addTask(memCard, objType, objId);
        }
        return result;
    }

    @PostMapping("/pointPayWeb")
    @ResponseBody
    @ApiModelProperty(value = "积分兑换")
    public Result pointPay(
            HttpServletRequest request,
            @RequestParam Integer objType,
            @RequestParam Integer objId,
            @RequestParam Integer paySource,
            @RequestParam(required = false) String fromMemCard,
            @RequestParam(required = false,defaultValue = "1") Integer objCount,
            @RequestParam(required = false) String orderId
    ) {
        String memCard ;
        if(request.getSession().getAttribute("UserCard")==null){
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }else{
            memCard=(String)request.getSession().getAttribute("UserCard");
        }
        Result result = orderService.pointPay(memCard, objType, objId, paySource, fromMemCard,
                IpUtil.getIpAddr(request), objCount, orderId);
        if(result.getCode() == 200){
            //增加任务积分
            orderService.addTask(memCard, objType, objId);
        }
        return result;
    }

    @PostMapping("/createOrder")
    @ResponseBody
    @ApiModelProperty(value = "支付创建订单")
    public Result createOrder(
            HttpServletRequest request,
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @RequestParam Integer objType,
            @RequestParam Integer objId,
            @RequestParam Integer paySource,
            @RequestParam(required = false) String fromMemCard,
            @RequestParam(required = false,defaultValue = "1") Integer objCount,
            @RequestParam(required = false) String orderId
    ) {
        if(memCard == null){
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }
        return orderService.createOrder(memCard, objType, objId, paySource, fromMemCard,
                IpUtil.getIpAddr(request), objCount, orderId);
    }


    @PostMapping("/createOrderWeb")
    @ResponseBody
    @ApiModelProperty(value = "支付创建订单")
    public Result createOrder(
            HttpServletRequest request,
            @RequestParam Integer objType,
            @RequestParam Integer objId,
            @RequestParam Integer paySource,
            @RequestParam(required = false) String fromMemCard,
            @RequestParam(required = false,defaultValue = "1") Integer objCount,
            @RequestParam(required = false) String orderId
    ) {
        String memCard ;
        if(request.getSession().getAttribute("UserCard")==null){
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }else{
            memCard=(String)request.getSession().getAttribute("UserCard");
        }
        return orderService.createOrder(memCard, objType, objId, paySource, fromMemCard,
                IpUtil.getIpAddr(request), objCount, orderId);
    }
}