package com.gitee.sop.sdk;

import com.alibaba.fastjson.JSON;
import com.gitee.sop.sdk.client.OpenClient;
import com.gitee.sop.sdk.common.FileResult;
import com.gitee.sop.sdk.common.Result;
import com.gitee.sop.sdk.common.UploadFile;
import com.gitee.sop.sdk.model.DemoFileUploadModel;
import com.gitee.sop.sdk.model.GetStoryModel;
import com.gitee.sop.sdk.request.DemoFileUploadRequest;
import com.gitee.sop.sdk.request.GetProductRequest;
import com.gitee.sop.sdk.request.PayTradeWapPayModel;
import com.gitee.sop.sdk.request.PayTradeWapPayRequest;
import com.gitee.sop.sdk.request.ProductDownloadModel;
import com.gitee.sop.sdk.request.ProductDownloadRequest;
import com.gitee.sop.sdk.response.GetProductResponse;
import com.gitee.sop.sdk.response.PayTradeWapPayResponse;
import junit.framework.TestCase;
import okhttp3.Headers;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;


public class SdkTest extends TestCase {
    String url = "http://localhost:8081/api";
    String appId = "2019032617262200001";
    /**
     * 开发者私钥
     */
    String privateKeyIsv = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOL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6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ=";
    /**
     * 开放平台提供的公钥
     * 前往SOP-ADMIN，ISV管理--秘钥管理，生成平台提供的公私钥，然后把【平台公钥】放到这里
     */
    String publicKeyPlatform = "";

    // 声明一个就行
    OpenClient client = new OpenClient(url, appId, privateKeyIsv, publicKeyPlatform);

    @Test
    public void test() {
        PayTradeWapPayRequest request = new PayTradeWapPayRequest();
        PayTradeWapPayModel model = new PayTradeWapPayModel();
        model.setOutTradeNo("70501111111S001111119");
        model.setTotalAmount(new BigDecimal("1000"));
        model.setSubject("衣服");
        model.setProductCode("QUICK_WAP_WAY");
        request.setBizModel(model);

        Result<PayTradeWapPayResponse> result = client.execute(request);
        if (result.isSuccess()) {
            PayTradeWapPayResponse response = result.getData();
            System.out.println(response);
        } else {
            System.out.println(result);
        }
    }

    @Test
    public void testGet() {
        // 创建请求对象
        GetProductRequest request = new GetProductRequest();
        // 请求参数
        GetStoryModel model = new GetStoryModel();
        model.setId(1);
        request.setBizModel(model);

        // 发送请求
        Result<GetProductResponse> result = client.execute(request);

        if (result.isSuccess()) {
            GetProductResponse response = result.getData();
            // 返回结果
            System.out.println(String.format("response:%s",
                    JSON.toJSONString(response)));
        } else {
            System.out.println("错误，subCode:" + result.getSubCode() + ", subMsg:" + result.getSubMsg());
        }
    }


    // 文件上传
    @Test
    public void testUpload() throws IOException {
        DemoFileUploadRequest request = new DemoFileUploadRequest();

        DemoFileUploadModel model = new DemoFileUploadModel();
        model.setProductName("上传文件参数");
        model.setAddTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        request.setBizModel(model);

        String root = System.getProperty("user.dir");
        System.out.println(root);
        // 这里演示将resources下的两个文件上传到服务器
        request.addFile(new UploadFile("idCardFront", new File(root + "/src/main/resources/file1.txt")));
        request.addFile(new UploadFile("idCardBack", new File(root + "/src/main/resources/file2.txt")));

        Result<GetProductResponse> result = client.execute(request);

        System.out.println("--------------------");
        if (result.isSuccess()) {
            GetProductResponse response = result.getData();
            System.out.println("您上传的文件信息：" + response);
        } else {
            System.out.println(JSON.toJSONString(result));
        }
        System.out.println("--------------------");
    }

    // 文件下载
    @Test
    public void testDownload() throws IOException {
        ProductDownloadRequest request = new ProductDownloadRequest();

        ProductDownloadModel model = new ProductDownloadModel();
        model.setId(111);
        request.setBizModel(model);

        FileResult result = client.download(request);
        // 文件流
        byte[] fileData = result.getFileData();
        String content = IOUtils.toString(fileData, "UTF-8");
        System.out.println("下载文件内容：" + content);
        Assert.assertEquals("abc,你好~!@#\n", content);

        System.out.println("----- header -----");
        Headers headers = result.getHeaders();
        for (String name : headers.names()) {
            String value = headers.get(name);
            System.out.println(name + ":" + value);
        }
    }

}
