package com.sheep.gamegroup.util.string;

import android.graphics.Color;
import android.support.annotation.DimenRes;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;

import com.sheep.gamegroup.util.ResourceUtils;

/**
 * Created by realicing on 2018/11/23.
 * realicing@sina.com
 * 用于TextView,方便使用SpannableStringBuilder，来设置文本，颜色等--开发中
 */
public class SpannableSb {
    private SpannableStringBuilder ssb = new SpannableStringBuilder();
    private int start;
    private int end;

    public SpannableSb append(String text) {
        if(TextUtils.isEmpty(text)){
            return this;
        }
        ssb.append(text);
        start = end;
        end += text.length();
        return this;
    }

    //前面有内容才换行
    public SpannableSb checkAndBr() {
        if(TextUtils.isEmpty(ssb)){
            return this;
        }
        return append("\n");
    }
    //换行
    public SpannableSb br() {
        return append("\n");
    }

    public SpannableSb setTextSize(@DimenRes int textSize) {
        //AbsoluteSizeSpan 参数 dip: size单位为px，true；size单位为dip（默认为false）。
        AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(ResourceUtils.getXmlDef(textSize), true);
        ssb.setSpan(absoluteSizeSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        return this;
    }

    public SpannableSb setTextColor(String textColor) {
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor(textColor));
        ssb.setSpan(colorSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        return this;
    }
    public SpannableSb append(String text, @DimenRes int textSize) {
        return append(text).setTextSize(textSize);
    }
    public SpannableSb append(String text, String textColor) {
        return append(text).setTextColor(textColor);
    }
    public SpannableSb append(String text, @DimenRes int textSize, String textColor) {
        return append(text).setTextSize(textSize).setTextColor(textColor);
    }


    public SpannableStringBuilder getSsb() {
        return ssb;
    }
}
