package com.example.iot_controlhost.utils.log;

import android.util.Log;

import com.example.iot_controlhost.utils.database.LogDBManager;
import com.example.iot_controlhost.utils.global.RxTag;
import com.example.iot_controlhost.utils.network.TopicClass;
import com.xuexiang.rxutil2.rxbus.RxBusUtils;
import com.xuexiang.rxutil2.rxjava.RxJavaUtils;
import com.xuexiang.rxutil2.rxjava.task.RxIOTask;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import timber.log.Timber;

/**
 * 日志用户查看用
 */
public class UserLog extends Timber.Tree {
    /**
     * 用户-操作类
     */
    public static final String TAG_USER_OPERATE = "用户-操作类";
    /**
     * 用户-传感器类
     */
    public static final String TAG_USER_SENSOR = "用户-传感器类";
    /**
     * 用户-人工作业类
     */
    public static final String TAG_USER_TASK = "用户-作业类";
    /**
     * 用户-系统类
     */
    public static final String TAG_USER_SYSTEM = "用户-系统类";

    public static List<String> getUserLogTags() {
        List<String> tags = new ArrayList<>();
        Collections.addAll(tags, TAG_USER_OPERATE, TAG_USER_SENSOR, TAG_USER_TASK, TAG_USER_SYSTEM);
        return tags;
    }

    public static void operate(String msg) {
        Timber.tag(TAG_USER_OPERATE).i(msg);
    }

    public static void operateError(String msg) {
        Timber.tag(TAG_USER_OPERATE + MyLog.TAG_ERROR).e(msg);
    }

    public static void task(String msg) {
        Timber.tag(TAG_USER_TASK).i(msg);
    }

    public static void taskError(String msg) {
        Timber.tag(TAG_USER_TASK + MyLog.TAG_ERROR).e(msg);
    }

    public static void sensor(String msg) {
        Timber.tag(TAG_USER_SENSOR).i(msg);
    }

    public static void sensorError(String msg) {
        Timber.tag(TAG_USER_SENSOR + MyLog.TAG_ERROR).e(msg);
    }

    public static void system(String msg) {
        Timber.tag(TAG_USER_SYSTEM).i(msg);
    }

    public static void systemError(String msg) {
        Timber.tag(TAG_USER_SYSTEM + MyLog.TAG_ERROR).e(msg);
    }

    @Override
    protected void log(int priority, String tag, String message, Throwable t) {
        //只有特定UserLog日志才能处理
        if (!tag.contains(TAG_USER_OPERATE) && !tag.contains(TAG_USER_SENSOR) && !tag.contains(TAG_USER_TASK) && !tag.contains(TAG_USER_SYSTEM)) {
            return;
        }
        RxJavaUtils.doInIOThread(new RxIOTask<>(null) {
            @Override
            public Void doInIOThread(Object o) {
                //发送日志到服务器
                int tagInt = 0;
                if (tag.contains(TAG_USER_OPERATE)) {
                    tagInt = 1;
                } else if (tag.contains(TAG_USER_SENSOR)) {
                    tagInt = 2;
                } else if (tag.contains(TAG_USER_TASK)) {
                    tagInt = 3;
                } else if (tag.contains(TAG_USER_SYSTEM)) {
                    tagInt = 4;
                }
                int newPriority;
                //1调试 2信息 3警告
                if (tag.contains(MyLog.TAG_ERROR)) {
                    newPriority = 3;
                } else {
                    newPriority = 2;
                }
                //保存日志到数据库
                LogDBManager.insertLog(new com.example.iot_controlhost.model.Log(tag, priority, new Date(), message));
                //输出日志到控制台
                Log.println(priority, tag, message);
                //上传日志
                TopicClass.uploadLog(tagInt, newPriority, message);
                return null;
            }
        });
    }

}
