package com.bocloud.ims.service.resource.azure.utils;

import com.alibaba.fastjson.JSON;
import com.bocloud.common.enums.PostDataFormat;
import com.bocloud.common.http.HttpClient;
import com.bocloud.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: ItsmWorkOrderReportProvider
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2020/11/27 14:23
 */

@Slf4j
@Component
public class AzurePriceProviderUtils {

    private static final String DOMAIN = "https://prices.azure.com";
    private HttpClient client;

    public String request(RequestStatus method, String url, String params) {
        Result result = this.connection(method, url, null, JSON.parseObject(params, Map.class));
        if (result.isFailed()) {
            throw new IllegalStateException("[" + unicodeToString(result.getErrorMsg()) + "]"
                    + unicodeToString(result.getMessage()));
        }
        return unicodeToString(result.getMessage());
    }

    private Result connection(RequestStatus method, String url, Map<String, Object> header, Map<String, Object> params) {
        if (MapUtils.isEmpty(header)) {
            header = this.buildHeader();
        }
        Result result = new Result();
        url = this.DOMAIN + url;
//        log.info("获取定价url:{}", url);
        switch (method) {
            case GET:
                result = this.get(url, header, params);
                break;
            case POST:
                result = this.post(url, header, params);
            default:
                break;
        }
        return result;
    }

    private Map<String, Object> buildHeader() {
        Map<String, Object> header = new HashMap<>();
        header.put("Content-Type", "application/json");
        return header;
    }

    private Result get(String url, Map<String, Object> header, Map<String, Object> params) {
        client = new HttpClient(10 * 60 * 1000, PostDataFormat.RAW);
        try {
            Map<String, Object> auth = new HashMap<>();
            if (null != header)
                auth.putAll(header);
            client.setDataFormat(null);
            Result result = client.get(auth, params, url);
            client.close();
            return result;
        } finally {
            client.close();
        }
    }

    public Result post(String url, Map<String, Object> header, Map<String, Object> params) {
        client = new HttpClient(10 * 60 * 1000, PostDataFormat.FORM_DATA);
        try {
            Map<String, Object> auth = new HashMap<>();
            if (null != header)
                auth.putAll(header);
            Result result = this.client.post(auth, params, url);
            client.close();
            return result;
        } finally {
            this.client.close();
        }
    }

    public enum RequestStatus {
        GET, POST, DELETE, PUT, PATCH
    }

    private String unicodeToString(String string) {
        if (StringUtils.isEmpty(string)) {
            return string;
        }
        StringBuffer stringBuffer = new StringBuffer();
        String pattern = "\\\\u[0-9,a-f,A-F]{4}";
        Matcher matcher = Pattern.compile(pattern).matcher(string);
        while (matcher.find()) {
            char c = (char) Integer.parseInt(matcher.group().replaceAll("\\\\u", ""), 16);
            matcher.appendReplacement(stringBuffer, new String(new char[]{c}));
        }
        matcher.appendTail(stringBuffer);
        return stringBuffer.toString();
    }
}