package com.amazon.ads.apidemo.utils;

import android.util.Log;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class L {
    private static final Pattern URL_PATTERN = Pattern.compile("(https?://)([^/:]+)(:\\d+)?(/[^\\s]*)?");

    private static final String TAG = "ADS_API_DEMO";

    private static long time = 0;

    private static boolean say() {
        return true;
    }

    public static void v(final Object... l) {
        if (!say()) {
            return;
        }

        try {
            String log = getHeader() + dump(l);
            Log.v(TAG, desensitize(log));
        } catch (Throwable e) {
            L.e(TAG, "Error output log.");
        }
    }

    public static void i(final Object... l) {
        if (!say()) {
            return;
        }

        try {
            String log = getHeader() + dump(l);
            Log.i(TAG, desensitize(log));
        } catch (Throwable e) {
            L.e(TAG, "Error output log.");
        }
    }

    public static void d(final Object... l) {
        if (!say()) {
            return;
        }

        try {
            String log = getHeader() + dump(l);
            Log.d(TAG, desensitize(log));
        } catch (Throwable e) {
            L.e(TAG, "Error output log.");
        }
    }

    public static void w(final Object... l) {
        if (!say()) {
            return;
        }

        try {
            String log = getHeader() + dump(l);
            Log.w(TAG, desensitize(log));
        } catch (Throwable e) {
            L.e(TAG, "Error output log.");
        }
    }

    public static void e(final Object... l) {
        if (!say()) {
            return;
        }

        try {
            String log = getHeader() + dump(l);
            Log.e(TAG, desensitize(log));
        } catch (Throwable e) {
            L.e(TAG, "Error output log.");
        }
    }

    public static void hex(final byte[] d) {
        if (say()) {
            final StringBuilder hex = new StringBuilder();
            for (byte b : d) {
                hex.append(Integer.toHexString(b));
                hex.append(" ");
            }
            Log.i(TAG, getHeader() + hex);
        }
    }

    public static void tc() {
        final long now = new Date().getTime();

        if (time != 0) {
            Log.i(TAG, "Time consumed (ms): " + (now - time));
        }

        time = now;
    }

    public static String dump(Object... args) {
        StringBuilder sb = new StringBuilder();
        for (Object o : args) {
            if (o == null) {
                o = "null";
            }
            if (o instanceof Throwable) {
                sb.append(Crash.dump(o));
            } else if (o instanceof String) {
                sb.append(o);
            } else {
                try {
                    sb.append(JsonUtils.toJson(o));
                } catch (Throwable e) {
                    sb.append(o);
                }
            }
            sb.append(" ");
        }

        return sb.toString();
    }

    private static String getHeader() {
        final StackTraceElement[] st = new Throwable().getStackTrace();
        String tag = "";
        String clz = st[2].getClassName();
        clz = clz.substring(clz.lastIndexOf('.') + 1);
        tag = clz + ".java:" + st[2].getLineNumber() + " " + st[2].getMethodName() + "() ";

        return tag;
    }

    public static String desensitize(String message) {
        String desensitized = message;

        // desensitized = maskHostInLog(message);

        return desensitized;
    }

    public static String maskHostInLog(String message) {
        Matcher matcher = URL_PATTERN.matcher(message);
        StringBuffer result = new StringBuffer();
        while (matcher.find()) {
            String protocol = matcher.group(1) != null ? matcher.group(1) : "http://";
            String host = matcher.group(2) != null ? matcher.group(2) : "";
            String port = matcher.group(3) != null ? matcher.group(3) : "";
            String path = matcher.group(4) != null ? matcher.group(4) : "";
            matcher.appendReplacement(result, protocol + "****" + port + path);
        }
        matcher.appendTail(result);
        return result.toString();
    }
}
