package com.otms.api.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.otms.api.dto.AlipayBizContent;
import com.otms.api.utils.AlipayClientCreator;
import com.otms.api.utils.AlipayConfig;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author frank
 * @Date 17-9-6
 */
@RestController
@RequestMapping("")
public class RfiController {


    private final static Logger log = LoggerFactory.getLogger(RfiController.class);

    @Autowired
    private AlipayConfig alipayConfig;

    @RequestMapping(value = "/rfi/weChat", method = RequestMethod.GET)
    public void testGet(@RequestParam(value = "code", required = false) String code){
        System.out.println(code);
        System.out.println("====================================");
    }

    @RequestMapping(value = "/rfi/weChat", method = RequestMethod.POST)
    public void testPost(@RequestParam(value = "code", required = false) String code) throws IOException {
        System.out.println(code);
        System.out.println("====================================");

        String url = "https://open.weixin.qq.com/connect/qrconnect?appid=wxbdc5610cc59c1631&redirect_uri=https%3A%2F%2Fpassport.yhd.com%2Fwechat%2Fcallback.do&response_type=code&scope=snsapi_login&state=3d6be0a4035d839573b04816624a415e#wechat_redirect";
        HttpClientBuilder builder = HttpClientBuilder.create().useSystemProperties();
        CloseableHttpClient client = builder.build();
        HttpGet get = new HttpGet(url);
        HttpResponse response = client.execute(get);
        InputStream inputStream = response.getEntity().getContent();
        byte [] tt = new byte[1024];
        while (inputStream.read(tt) != -1) {
            System.out.println(new String(tt,"utf-8"));
        }
    }

    @RequestMapping(value = "/rfi/jwt", method = RequestMethod.GET)
    public String testJwt() {
        return null;
    }

    @RequestMapping(value = "/fpwxtest", method = RequestMethod.GET)
    public String testProxy() throws IOException {

        AlipayClient alipayClient = AlipayClientCreator.getAlipayClient(alipayConfig);

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
        //alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

        AlipayBizContent bizContent = new AlipayBizContent();
        String param = objectMapper.writeValueAsString(bizContent);
        alipayRequest.setBizContent(param);

        log.info(param);

        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return form;
    }
}