package com.kfzs.duanduan.utils;

import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sheep.jiuyan.samllsheep.R;


/**
 * @ Created by Dlg
 * @ <p>TiTle:  ListViewUtil</p>
 * @ <p>Description:</p>
 * @ date:  2015/12/19 18:09
 * @ QQ:    315096953
 */
public class ListViewUtil {

    private TextView mTxtEmpty;
    private ImageView mImgEmpty;
    private View mViewEmpty;//总布局文件

    public ListViewUtil setEmptyView(Context context, ListView listView) {
        mViewEmpty = View.inflate(context, R.layout.empty_view, null);
        mImgEmpty = mViewEmpty.findViewById(R.id.empty_view_img);
        mTxtEmpty = mViewEmpty.findViewById(R.id.empty_view_msg);
        mViewEmpty.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams
                .MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mViewEmpty.setVisibility(View.GONE);
        ((ViewGroup) listView.getParent()).addView(mViewEmpty);
        listView.setEmptyView(mViewEmpty);
        return this;
    }


    /**
     * 设置用户自己的列表为空时的图片
     *
     * @param view
     */
    public ListViewUtil setCustomEmptyView(ListView listView, @NonNull View view) {
        mViewEmpty = view;
        mViewEmpty.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams
                .MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mViewEmpty.setVisibility(View.GONE);
        mViewEmpty.setVisibility(View.GONE);
        ((ViewGroup) listView.getParent()).addView(mViewEmpty);
        listView.setEmptyView(view);
        return this;
    }


    /**
     * 设置空列表组件的背景
     *
     * @param color
     */
    public ListViewUtil setEmptyViewBackGround(@ColorInt int color) {
        mViewEmpty.setBackgroundColor(color);
        return this;
    }


    /**
     * 设置列表为空时的显示图片
     *
     * @param resId 为0时显示不显示图标
     */
    public ListViewUtil setEmptyImgRes(@DrawableRes int resId) {
        if (mImgEmpty == null) {
            return this;
        } else if (resId == 0) {
            mImgEmpty.setVisibility(View.GONE);
        }
        mImgEmpty.setImageResource(resId);
        mImgEmpty.setVisibility(View.VISIBLE);
        return this;
    }


    /**
     * 设置列表为空时的显示标题
     *
     * @param emptyText
     */
    public ListViewUtil setEmptyText(String emptyText) {
        if (mTxtEmpty == null || emptyText == null || emptyText.trim().length() < 1) {
            return this;
        }
        mTxtEmpty.setText(emptyText);
        return this;
    }

}
