package kr.co.hanwha.hifive.network.hanwha;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.NonNull;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * Created by sanghoonkwon on 2018. 1. 11..
 */

public class HanwhaNetworkUtil {
    public static boolean isNetworkConnected(@NonNull Context context) {
        if (context == null) return false;
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        return (networkInfo != null && networkInfo.isConnectedOrConnecting());
    }

    /**********************************************************************
     * Default Methods
     **********************************************************************/
    public static String getParam(String key, String value) {
        if (value == null) value = "";
        return key + "=" + value;
    }

    public static String getParamURLEncoding(String key, String value) {
        if (value == null) value = "";
        return key + "=" + URLEncoder.encode(value);
    }

    public static String getQuestionmarkWithParamURLEncoding(String key, String value) {
        if (value == null) value = "";
        return "?" + key + "=" + URLEncoder.encode(value);
    }

    public static String getQuestionmarkWithParam(String key, String value) {
        if (value == null) value = "";
        return "?" + key + "=" + value;
    }

    public static String getQuestionmarkWithParam(String key, int value) {
        return "?" + key + "=" + value;
    }

    public static String getAmpersandWithParam(String key, String value) {
        if (value == null) value = "";
        return "&" + key + "=" + value;
    }

    public static String getAmpersandWithParam(String key, int value) {
        return "&" + key + "=" + value;
    }

    public static String getAmpersandWithParam(String key, long value) {
        return "&" + key + "=" + value;
    }

    public static String getAmpersandWithParamURLEncoding(String key, String value) {
        if (value == null) value = "";
        return "&" + key + "=" + URLEncoder.encode(value);
    }

    public static String getSlashWithParam(String value) {
        if (value == null) value = "";
        return "/" + value;
    }

    public static String getSlashWithParam(int value) {
        return "/" + value;
    }

    public static String getURLEncoding(String value) {
        String ret = null;
        try {
            ret = URLEncoder.encode(value, StandardCharsets.UTF_8.name());
        } catch (Exception e) {
            ret = value;
        }
        return ret;
    }
}
