package com.liquidnet.service;

import com.alibaba.fastjson.JSON;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.dragon.utils.ObjectUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author AnJiabin <anjiabin@zhengzai.tv>
 * @version V1.0
 * @Description: TODO
 * @class: DragonPayTest
 * @Package com.liquidnet.service
 * @Copyright: LightNet @ Copyright (c) 2021
 * @date 2021/7/27 13:47
 */
//@RunWith(SpringJUnit4ClassRunner.class)
//@SpringBootTest
public class DragonPayTest {

    public static void testMap(){
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("status", "1"));
        nvps.add(new BasicNameValuePair("type", "TICKET"));
        nvps.add(new BasicNameValuePair("code", "PAY20210727111746415639688"));
        nvps.add(new BasicNameValuePair("paymentId", "4200001121202107274119605305"));
        nvps.add(new BasicNameValuePair("orderCode", "11766977997930496418880635"));
        nvps.add(new BasicNameValuePair("price", "0.01"));
        nvps.add(new BasicNameValuePair("paymentType", "APPWEPAY"));
        nvps.add(new BasicNameValuePair("paymentAt", "2021-07-27 11:18:03"));
        String jsonData = JSON.toJSONString(nvps);
        System.out.println("jsonDate === "+jsonData);
        System.out.println("covertNVPS2Params jsonDate === "+covertNVPS2Params(nvps));
        System.out.println("covertNVPS2Params jsonDate === "+ JsonUtils.toJson(covertNVPS2Params(nvps)));
        System.out.println("covertNVPS2KeyValue jsonDate === "+ covertNVPS2KeyValue(nvps));
    }

    public static void testMap2() {
        MultiValueMap<String, String> params = ObjectUtil.cloneLinkedMultiValueMapStringAndString();
        params.add("status", "1");
        params.add("type", "TICKET");
        params.add("code","PAY20210727111746415639688");
        params.add("paymentId", "4200001121202107274119605305");
        params.add("orderCode", "11766977997930496418880635");
        params.add("price", "0.01");
        params.add("paymentType", "APPWEPAY");
        params.add("paymentAt", "2021-07-27 11:18:03");
        String jsonData = JsonUtils.toJson(params);
        System.out.println("jsonData === "+jsonData);
        System.out.println("jsonData === " + JsonUtils.toJson(covertNVPS2Params(params)));
    }
    private static ArrayList<NameValuePair> covertParams2NVPS(Map<String, Object> params) {
        ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
        for (Map.Entry<String, Object> param : params.entrySet()) {
            pairs.add(new BasicNameValuePair(param.getKey(), String.valueOf(param.getValue())));
        }

        return pairs;
    }
    private static ArrayList<NameValuePair> covertParams2NVPS(MultiValueMap<String, String> params) {
        ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
        for (Map.Entry<String, List<String>> param : params.entrySet()) {
            pairs.add(new BasicNameValuePair(param.getKey(), param.getValue().get(0)));
        }

        return pairs;
    }
    private static Map<String, Object> covertNVPS2Params(List<NameValuePair> nvpList) {
        Map<String, Object> rsMap = new HashMap<>();
        for (NameValuePair nameValuePair : nvpList) {
            rsMap.put(nameValuePair.getName(),nameValuePair.getValue());
        }
        return rsMap;
    }

    private static String covertNVPS2KeyValue(List<NameValuePair> nvpList) {
        StringBuffer sb = new StringBuffer();
        for (NameValuePair nameValuePair : nvpList) {
            sb.append(nameValuePair.getName()+":"+nameValuePair.getValue()+"\n");
        }
        return sb.toString();
    }

    private static String covertNVPS2KeyValue(MultiValueMap<String, String> mapParam) {
        ArrayList<NameValuePair> pairs = covertParams2NVPS(mapParam);
        return covertNVPS2KeyValue(pairs);
    }

    private static Map<String, Object> covertNVPS2Params(MultiValueMap<String, String> params) {
        Map<String, Object> rsMap = new HashMap<>();
        for (Map.Entry<String, List<String>> param : params.entrySet()) {
            rsMap.put(param.getKey(), param.getValue().get(0));
        }
        return rsMap;
    }

    public static void main(String[] args) {

//        testMap();
          testMap2();
    }
}
