package com.genlot.ninglottery.widget.Diaolog;

import android.graphics.drawable.Drawable;
import android.support.annotation.IdRes;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * 类描述：Dialog ViewHolder
 * 创建人：songjing
 * 创建时间：2019/7/1 09:46
 */
public class ViewHolder {
    private SparseArray<View> views;
    private View convertView;

    private ViewHolder(View view) {
        convertView = view;
        views = new SparseArray<>();
    }

    public static ViewHolder create(View view) {
        return new ViewHolder(view);
    }

    /**
     * 获取View
     *
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(@IdRes int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = convertView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 设置文本
     *
     * @param viewId
     * @param text
     */
    public void setText(int viewId, CharSequence text) {
        TextView textView = getView(viewId);
        textView.setText(text);
    }


    /**
     * 设置字体颜色
     *
     * @param viewId
     * @param colorId
     */
    public void setTextColor(int viewId, int colorId) {
        TextView textView = getView(viewId);
        textView.setTextColor(colorId);
    }

    /**
     * 设置背景图片
     *
     * @param viewId
     * @param resId
     */
    public void setBackgroundResource(int viewId, Drawable resId) {
        View view = getView(viewId);
        view.setBackground(resId);
    }

    /**
     * 设置背景颜色
     *
     * @param viewId
     * @param colorId
     */
    public void setBackgroundColor(int viewId, int colorId) {
        View view = getView(viewId);
        view.setBackgroundColor(colorId);
    }

    /**
     * @param viewId
     * @param isV
     */
    public void setViewVisibility(int viewId, boolean isV) {
        View view = getView(viewId);
        if (isV) {
            if (view.getVisibility() == View.GONE)
                view.setVisibility(View.VISIBLE);
        } else {
            if (view.getVisibility() == View.VISIBLE)
                view.setVisibility(View.GONE);
        }
    }

    /**
     * 设置点击事件
     *
     * @param viewId
     * @param listener
     */
    public void setOnClickListener(int viewId, String str, View.OnClickListener listener) {
        Button view = getView(viewId);
        view.setText(str);
        view.setOnClickListener(listener);
    }

    public void setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        view.setOnClickListener(listener);
    }
}
