package org.mym.prettylog;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.TextView;

import org.mym.plog.Category;
import org.mym.plog.Formatter;
import org.mym.plog.PrintLevel;
import org.mym.plog.Printer;
import org.mym.plog.SoftWrapper;
import org.mym.plog.Style;
import org.mym.plog.formatter.DefaultFormatter;

/**
 * This is a sample of customizing view printer.
 *
 * @author Muyangmin
 * @since 2.0.0
 */
public class TextViewPrinter implements Printer {

    private static final String DISCLAIMER = "NOTE: This view printer is just for fun, please see logcat " +
            "or file output to get a practical experience.\n\n";
    private TextView mTextView;
    private StringBuilder mBuffer;

    public TextViewPrinter(@NonNull TextView mTextView) {
        this.mTextView = mTextView;
        mTextView.setText(DISCLAIMER);
    }

    @Override
    public boolean onIntercept(@PrintLevel int level, @NonNull String tag, @Nullable Category
            category, @NonNull String msg) {
        //intercept nothing
        return false;
    }

    @Nullable
    @Override
    public Formatter getFormatter() {
        return new DefaultFormatter();
    }

    @Nullable
    @Override
    public Style getStyle() {
        return null;
    }

//    @Override
//    public boolean isSoftWrapDisallowed() {
//        return false;
//    }

    @Override
    public SoftWrapper getSoftWrapper() {
        return null;
    }

    @Override
    public int getMaxLengthPerLine() {
        return 0;
    }

    @Override
    public void print(@PrintLevel int level, @NonNull String tag, @NonNull String msg) {
        if (mBuffer ==null || mBuffer.length() > 150){
            mBuffer = new StringBuilder().append(DISCLAIMER);
        }
        mBuffer.append(msg);
        mTextView.setText(mBuffer);
    }
}

@@ -1692,16 +1692,17 @@
 uffer ==
+ 
 null %7C%7C 
@@ -1722,14 +1722,52 @@
 h() 
-%3E 15
++ msg.length() %3E DISCLAIMER.length() + 30
 0)
+ 
 %7B%0A  
@@ -1863,16 +1863,29 @@
 end(msg)
+.append(%22%5Cn%22)
 ;%0A      

update TextViewPrinter and fix line feeds.