package com.ez.ertc.call.ui.widget;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.TextView;

import com.ez.ertc.R;

/**
 * 自动获取assets文件夹下iconfont/iconfont.ttf来设置字体.
 * 支持渐变-只有设置了fromColor和toColor才生效
 * Created by xiezuoyuan on 19/4/9.
 */
@SuppressLint("AppCompatCustomView")
public class MtTextView extends TextView {
    private boolean selected;
    private int toColor;
    private int fromColor;
    public MtTextView(Context context) {
        this(context,null);
    }

    public MtTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MtTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MtTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "iconset/ezmiconfont.ttf");
        setTypeface(typeface);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MtTextView);
        selected = ta.getBoolean(R.styleable.MtTextView_ezm_selected,true);
        toColor = ta.getColor(R.styleable.MtTextView_ezm_toColor, 0);
        fromColor = ta.getColor(R.styleable.MtTextView_ezm_fromColor, 0);
        ta.recycle();
    }

    public void toggleSelect(boolean selected){
        if (this.selected == selected){
            return;
        }
        this.selected = selected;
        if (selected){
            setTextColor(toColor);
        } else {
            setTextColor(fromColor);
        }
    }

    public boolean isFontSelected(){
        return selected;
    }

    public void setFontRes(String res,boolean selected){
        setText(res);
        toggleSelect(selected);
    }
}
