package com.xty.common.marqueen;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

import androidx.annotation.ColorInt;

import com.xty.common.R;
import com.xty.common.marqueen.util.Util;

import java.util.List;

/**
 * Created by GongWen on 17/9/15.
 * <p>
 * 属性(继承自父View的属性＋自身特有属性)
 * SimpleMarqueeView_smvTextColor
 * SimpleMarqueeView_smvTextSize
 * SimpleMarqueeView_smvTextGravity
 */

public class SimpleMarqueeView<E> extends MarqueeView<TextView, E> {
    private ColorStateList smvTextColor = null;
    private float smvTextSize = 15;
    private int smvTextGravity = Gravity.NO_GRAVITY;
    private boolean smvTextSingleLine = false;
    private int smvTextStyle = 0;
    private TextUtils.TruncateAt smvTextEllipsize;

    public SimpleMarqueeView(Context context) {
        this(context, null);
    }

    public SimpleMarqueeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        int ellipsize = -1;
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.SimpleMarqueeView, 0, 0);
            smvTextColor = a.getColorStateList(R.styleable.SimpleMarqueeView_smvTextColor);
            if (a.hasValue(R.styleable.SimpleMarqueeView_smvTextSize)) {
                smvTextSize = a.getDimension(R.styleable.SimpleMarqueeView_smvTextSize, smvTextSize);
                smvTextSize = Util.px2Sp(getContext(), smvTextSize);
            }
            smvTextGravity = a.getInt(R.styleable.SimpleMarqueeView_smvTextGravity, smvTextGravity);
            smvTextSingleLine = a.getBoolean(R.styleable.SimpleMarqueeView_smvTextSingleLine, smvTextSingleLine);
            smvTextStyle = a.getInt(R.styleable.SimpleMarqueeView_smvTextStyle, smvTextStyle);
            ellipsize = a.getInt(R.styleable.SimpleMarqueeView_smvTextEllipsize, ellipsize);
            a.recycle();
        }
        if (smvTextSingleLine && ellipsize < 0) {
            ellipsize = 3; // END
        }

        switch (ellipsize) {
            case 1:
                smvTextEllipsize = TextUtils.TruncateAt.START;
                break;
            case 2:
                smvTextEllipsize = TextUtils.TruncateAt.MIDDLE;
                break;
            case 3:
                smvTextEllipsize = TextUtils.TruncateAt.END;
                break;
        }
    }

    @Override
    protected void refreshChildViews() {
        super.refreshChildViews();
        List<TextView> views = factory.getMarqueeViews();
        for (TextView textView : views) {
            textView.setTextSize(smvTextSize);
            textView.setGravity(smvTextGravity);
            textView.setTypeface(Typeface.create(Typeface.DEFAULT, smvTextStyle));
            if (smvTextColor != null) {
                textView.setTextColor(smvTextColor);
            }
            textView.setSingleLine(smvTextSingleLine);
            textView.setEllipsize(smvTextEllipsize);
        }
    }

    public void setTextSize(float textSize) {
        this.smvTextSize = textSize;
        if (factory != null) {
            for (TextView textView : factory.getMarqueeViews()) {
                textView.setTextSize(textSize);
            }
        }
    }

    public void setTextColor(@ColorInt int color) {
        setTextColor(ColorStateList.valueOf(color));
    }

    public void setTextColor(ColorStateList colorStateList) {
        this.smvTextColor = colorStateList;
        if (factory != null) {
            for (TextView textView : factory.getMarqueeViews()) {
                textView.setTextColor(smvTextColor);
            }
        }
    }

    public void setDrawable(Drawable left, Drawable top,
                            Drawable right, Drawable bottom){

        if (factory != null) {
            for (TextView textView : factory.getMarqueeViews()) {
                if (null != left){
                    left.setBounds(0, 0, left.getMinimumWidth(), left.getMinimumHeight());
                }
                textView.setCompoundDrawablePadding(12);
                textView.setCompoundDrawables(left, top, right, bottom);
            }
        }
    }

    public void setTextGravity(int gravity) {
        this.smvTextGravity = gravity;
        if (factory != null) {
            for (TextView textView : factory.getMarqueeViews()) {
                textView.setGravity(smvTextGravity);
            }
        }
    }

    public void setTextStyle(int textStyle){
        this.smvTextStyle = textStyle;
        if (factory != null) {
            for (TextView textView : factory.getMarqueeViews()) {
                textView.setTypeface(Typeface.create(Typeface.DEFAULT, smvTextStyle));
            }
        }
    }


    public void setTextSingleLine(boolean singleLine) {
        this.smvTextSingleLine = singleLine;
        if (factory != null) {
            for (TextView textView : factory.getMarqueeViews()) {
                textView.setSingleLine(smvTextSingleLine);
            }
        }
    }

    public void setTextEllipsize(TextUtils.TruncateAt where) {
        if (where == TextUtils.TruncateAt.MARQUEE) {
            throw new UnsupportedOperationException("The type MARQUEE is not supported!");
        }
        this.smvTextEllipsize = where;
        if (factory != null) {
            for (TextView textView : factory.getMarqueeViews()) {
                textView.setEllipsize(where);
            }
        }
    }
}
