package com.tjzhxx.union.public_store.fragment;

/**
 * Created by Administrator on 2016/2/22.
 */

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tjzhxx.union.R;


/**
 * TAB页
 * */
public class FragmentTab extends LinearLayout {
    private ImageView ivIcon;
    private TextView tvText;
    private TextView tvTips;

    private ColorStateList colorFocus=null;
    private ColorStateList colorBlur=null;

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

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

    public FragmentTab(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.FragmentTab);
        LayoutInflater.from(context).inflate(R.layout.layout_majortab, this);
        ivIcon=(ImageView)findViewById(R.id.iv_tabicon);
        tvText=(TextView)findViewById(R.id.tv_tabtips);
        tvTips =(TextView)findViewById(R.id.tv_tabtext);

        Drawable icon=a.getDrawable(R.styleable.FragmentTab_tab_icon); //图标
        CharSequence text=a.getText(R.styleable.FragmentTab_tab_text);//文字
        colorFocus=a.getColorStateList(R.styleable.FragmentTab_tab_text_focuscolor);
        colorBlur=a.getColorStateList(R.styleable.FragmentTab_tab_text_blurcolor);
        ivIcon.setBackgroundDrawable(icon);
        tvText.setText(text);
        a.recycle();
    }

    /**
     *
     * 显示提示
     * @param count 数目，count>0才显示
     * */
    public void setTips(int count){
        if(tvTips!=null){
            if (count > 0)
            {
                String res = count > 99 ? "99+" : String.valueOf(count);
                tvTips.setText(res);
            }
            tvTips.setVisibility(count > 0 ? View.VISIBLE : View.GONE);
        }
    }

    public String getTabCaption(){
        if(tvText!=null){
            return tvText.getText().toString();
        }
        return "";
    }

    /**
     *
     * 设置tab状态
     * */
    public void setTabFoucus(boolean focus){
        if(tvText!=null){
            if(focus){
                tvText.setTextColor(colorFocus != null ? colorFocus : ColorStateList.valueOf(0xFF000000));
            }
            else{
                tvText.setTextColor(colorBlur != null ? colorBlur : ColorStateList.valueOf(0xFFe8e8e8));
            }
        }
        if(ivIcon!=null){
            ivIcon.setSelected(focus);
        }
    }

}
