package dev.momo.library.core.log;

import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
import android.widget.TextView;

import dev.momo.library.core.Helper;

/**
 * Created by Peng on 2016/12/2.
 */
public class LoggerView extends TextView {

    private String filterTag = "";
    private int logLevel = Logger.LOG_ALL;

    boolean topToBottom = false; // show log from top to bottom or bottom to top

    public LoggerView(Context context) {
        super(context);
        if (!isInEditMode()) {
            init();
        }
    }

    public LoggerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode()) {
            init();
        }
    }


    private void init() {
        setMovementMethod(new ScrollingMovementMethod());
    }

    public void addMessage(String TAG, int level, final String message) {
        if (!TAG.contains(filterTag)) return;
        if (logLevel < level) return;

        Helper.mainUIHandler.post(() -> {
            if (isTopToBottom()) {
                // append the new string to bottom
                append(message + "\n");
                // find the amount we need to scroll.  This works by
                // asking the TextView's internal layout for the position
                // of the final line and then subtracting the TextView's height
                final int scrollAmount = getLayout().getLineTop(getLineCount() - 1) - getHeight();
                // if there is no need to scroll, scrollAmount will be <=0
                if (scrollAmount > 0) {
                    scrollTo(0, scrollAmount);
                } else {
                    scrollTo(0, 0);
                }
            } else {
                setText(message + "\n" + getText());
            }
        });
    }


    public void setTagFilter(String tag) {
        this.filterTag = tag;
    }

    public void setLogLevel(int logLevel) {
        this.logLevel = logLevel;
    }

    public boolean isTopToBottom() {
        return topToBottom;
    }

    public void setTopToBottom(boolean topToBottom) {
        this.topToBottom = topToBottom;
    }

}

@@ -1046,16 +1046,84 @@
 ost(
-() -%3E %7B%0A
+new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A%0A    
     
@@ -1165,24 +1165,28 @@
             
+ 
+   
 // append th
@@ -1224,16 +1224,20 @@
         
+    
 append(m
@@ -1268,16 +1268,20 @@
         
+   
+ 
 // find 
@@ -1325,16 +1325,20 @@
 orks by%0A
+    
         
@@ -1403,16 +1403,20 @@
 osition%0A
+    
         
@@ -1503,16 +1503,20 @@
         
+    
 final in
@@ -1606,16 +1606,20 @@
         
+    
 // if th
@@ -1681,24 +1681,28 @@
             
+ 
+   
 if (scrollAm
@@ -1709,24 +1709,28 @@
 ount %3E 0) %7B%0A
+    
             
@@ -1772,32 +1772,36 @@
                 
+  
+  
 %7D else %7B%0A       
@@ -1785,32 +1785,36 @@
        %7D else %7B%0A
+    
                 
@@ -1841,34 +1841,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D el
@@ -1872,32 +1872,36 @@
        %7D else %7B%0A
+    
                 
@@ -1933,24 +1933,42 @@
 getText());%0A
+                %7D%0A
             

revert to java 7