package com.miya.print.utils;


import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import com.miya.print.BuildConfig;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;

public class PrintLogger {

    private static final String Tag = PrintLogger.class.getSimpleName();

    /**
     * 是否开启logcat日志输出
     */
    static boolean isOpenLogcat = true;

    /**
     * 是否开启记录本地日志文件
     */
    static boolean isOpenWriteLog = true;

    /**
     * log文件
     */
    private static File logFile;
    /**
     * 执行器
     */
    private static ScheduledExecutorService sExecutor;
    /**
     * 文件夹路径
     */
    public static final String CACHE_FILE_PATH = Environment
            .getExternalStorageDirectory().getPath() +
            File.separator +
            "miyaterminal" +
            File.separator +
            "log" +
            File.separator;

    /**
     * 写日志
     *
     * @param input 写入内容
     * @return
     */
    public static boolean writeLog(final Context c, final String input) {
        if (isOpenWriteLog == false) {
            return false;
        }
        if (sExecutor == null) {
            sExecutor = Executors.newScheduledThreadPool(5);
        }
        Future<Boolean> submit = sExecutor.submit(new Callable<Boolean>() {
            @Override
            public Boolean call() {
                try {
                    File dir = new File(CACHE_FILE_PATH);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    String logPath = CACHE_FILE_PATH + Tag + "_" + format(new Date(), "yyyyMMdd") + "_v" + BuildConfig.VERSION_NAME + ".txt";
                    logFile = new File(logPath);
                    FileOutputStream fos = new FileOutputStream(logFile, true);
                    OutputStreamWriter writer = new OutputStreamWriter(fos, Charset.forName("utf-8"));
                    //写上当前时间点
                    writer.write(format(new Date(), "yyyy-MM-dd HH:mm:ss") + "  :");
                    writer.write(input);
                    //换行一下
                    writer.write("\n");
                    writer.flush();
                    writer.close();
                    return true;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
        });
        try {
            return submit.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void log(String text) {
        if (isOpenLogcat) {
            android.util.Log.v(Tag, "-----:" + text);
        }
    }


    public static void v(String tag, String msg) {
        if (isOpenLogcat) {
            android.util.Log.v(tag, msg);
        }
    }

    public static void v(String tag, String msg, Throwable tr) {
        if (isOpenLogcat) {
            android.util.Log.v(tag, msg, tr);
        }
    }

    public static void d(String tag, String msg) {
        if (isOpenLogcat) {
            android.util.Log.d(tag, msg);
        }
    }

    public static void d(String tag, String msg, Throwable tr) {
        if (isOpenLogcat) {
            android.util.Log.d(tag, msg, tr);
        }
    }

    public static void i(String tag, String msg) {
        if (isOpenLogcat) {
            android.util.Log.i(tag, msg);
        }
    }

    public static void i(String tag, String msg, Throwable tr) {
        if (isOpenLogcat) {
            android.util.Log.i(tag, msg, tr);
        }
    }

    public static void w(String tag, String msg) {
        if (isOpenLogcat) {
            android.util.Log.w(tag, msg);
        }
    }

    public static void w(String tag, String msg, Throwable tr) {
        if (isOpenLogcat) {
            android.util.Log.w(tag, msg, tr);
        }
    }

    public static void w(String tag, Throwable tr) {
        if (isOpenLogcat) {
            android.util.Log.w(tag, tr);
        }
    }

    public static void e(String tag, String msg) {
        if (isOpenLogcat) {
            android.util.Log.e(tag, msg);
        }
    }

    public static void e(String tag, String msg, Throwable tr) {
        if (isOpenLogcat) {
            android.util.Log.e(tag, msg, tr);
        }
    }

    /**
     * 将日期转换成字符串
     *
     * @param date
     * @param format
     * @return
     */
    private static String format(Date date, String format) {
        if (date == null) {
            throw new IllegalArgumentException("Param date is null!");
        }
        if (TextUtils.isEmpty(format)) {
            throw new IllegalArgumentException("Param format is blank!");
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
}
