package com.bocloud.ims.service.utils;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.DatatypeConverter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.common.enums.HttpRequestMethod;
import com.bocloud.common.enums.PostDataFormat;
import com.bocloud.common.http.HttpClient;
import com.bocloud.common.model.Result;

/**
 * @author aile
 * @date 2020/3/23 14:10
 */
public class HarborUtil {

    private static final Logger logger = LoggerFactory.getLogger(HarborUtil.class);

    private Map<String, Object> authHeader = new HashMap<>();
    private String sectionUrl;
    public HarborUtil(String username, String password) {
        initHarborAuth(username, password);
    }

    private void initHarborAuth(String username, String password) {
        String encoding = null;
        try {
            encoding = DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            logger.warn("不支持的编码" + e);
        }
        authHeader.put("Authorization", "Basic " + encoding);
    }

    /**
     * 发送HTTP请求
     *
     * @param url
     * @return
     */
    public Result httpConnection(String url, HttpRequestMethod method, Map<String, Object> params) {
        Result result;
        HttpClient client = new HttpClient(20 * 1000 * 1000, PostDataFormat.RAW);
        switch (method) {
            case GET:
                result = client.get(this.authHeader, params, url);
                break;
            case POST:
                result = client.post(this.authHeader, params, url);
                break;
            case PUT:
                result = client.put(this.authHeader, params, url);
                break;
            default:
                result = client.delete(this.authHeader, params, url);
        }
        if (result.isFailed()) {
            return new Result(false, "http请求失败:" + result.getMessage());
        }
        if (result.getMessage() == null){
            result.setMessage("[]");
        }
        return new Result(true, result.getMessage());
    }

    /**
     * 转换harbor里面时间的格式
     * @param time
     * @return
     */
    public static String timeTransfer(String time){
        String[] times = time.split("T|\\.|Z");
        String temp = "";
        for (int i = 0; i < times.length && i < 2; i++) {
            temp += times[i] + " ";
        }
        SimpleDateFormat sdfFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        Date dt = new Date();
        try {
            dt = sdfFormat.parse(temp);
        } catch (ParseException e) {
            logger.error("时间转换失败");
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        //harbor里面的时间需要做转换，全部都是在基础上加8小时
        cal.add(Calendar.HOUR_OF_DAY, +8);
        dt = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dt);
    }

    public String getSectionUrl() {
        return sectionUrl;
    }

    public void setSectionUrl(String sectionUrl) {
        this.sectionUrl = sectionUrl;
    }
}
