package com.izouma.nineth.web;

import com.alibaba.fastjson.JSON;
import com.izouma.nineth.service.HMPayService;
import com.izouma.nineth.service.SandPayService;
import com.izouma.nineth.utils.SnowflakeIdWorker;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("/payChannelMgmt")
@AllArgsConstructor
public class PayChannelMgmtController {
    private final SandPayService    sandPayService;
    private final HMPayService      hmPayService;
    private final SnowflakeIdWorker snowflakeIdWorker;

    @GetMapping(value = "/pay")
    @PreAuthorize("hasRole('ADMIN')")
    public String pay(@RequestParam String channel) {
        switch (channel) {
            case "sandPay":
                return sandPayService.requestAlipay(snowflakeIdWorker.nextId() + "",
                        new BigDecimal("0.01"), "话费充值", "话费充值",
                        SandPayService.getTimeout(180), "{\"type\":\"test\",\"id\":1}");
            case "hmPay":
                return hmPayService.requestAlipay(snowflakeIdWorker.nextId() + "",
                        new BigDecimal("0.01"), "话费充值",
                        HMPayService.getTimeout(180), "test", "https://www.baidu.com");
        }
        return null;
    }

    @GetMapping(value = "/transfer")
    @PreAuthorize("hasRole('ADMIN')")
    public String testTransfer(@RequestParam String name, @RequestParam String bank, @RequestParam BigDecimal amount) {
        return JSON.toJSONString(sandPayService.transfer(snowflakeIdWorker.nextId() + "", name, bank, amount), true);
    }

    @GetMapping(value = "/refund")
    @PreAuthorize("hasRole('ADMIN')")
    public String refund(@RequestParam String channel, @RequestParam String orderId, @RequestParam BigDecimal amount) {
        switch (channel) {
            case "sandPay":
                return JSON.toJSONString(sandPayService.refund(orderId, amount), true);
            case "hmPay":
                return JSON.toJSONString(hmPayService.refund(orderId, amount), true);
        }
        return null;
    }

    @GetMapping(value = "/query")
    @PreAuthorize("hasRole('ADMIN')")
    public String refund(@RequestParam String channel, @RequestParam String id) {
        switch (channel) {
            case "sandPay":
                return JSON.toJSONString(sandPayService.query(id), true);
            case "hmPay":
                return JSON.toJSONString(hmPayService.query(id), true);
        }
        return null;
    }

    @GetMapping("/queryTransfer")
    @PreAuthorize("hasRole('ADMIN')")
    public String queryTransfer(@RequestParam String tranTime, @RequestParam String orderId) {
        return JSON.toJSONString(sandPayService.queryTransfer(tranTime, orderId), true);
    }
}
