package com.hjx.miaohongentry.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hjx.miaohongentry.R;
import com.hjx.miaohongentry.bean.BookInfo;

import java.util.ArrayList;

/**
 * Created by l on 2018/2/2.
 */

public class BookListAdapter  extends BaseAdapter {

    private Context mContext;
    private ArrayList<BookInfo> mList = null;
    private int selectItem = 0;

    public BookListAdapter(Context mContext, ArrayList<BookInfo> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder mViewHolder = null;
        int textColor = R.color.colorFontBlack;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.choose_book_list_item, null);

            mViewHolder = new ViewHolder();
            mViewHolder.tv_grade = (TextView) convertView
                    .findViewById(R.id.choose_book_list_item_grade);
            mViewHolder.tv_reactos = (TextView) convertView
                    .findViewById(R.id.choose_book_list_item_reactos);
            mViewHolder.tv_press = (TextView) convertView
                    .findViewById(R.id.choose_book_list_item_press);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        BookInfo detailInfo = (BookInfo) getItem(position);

        if (detailInfo != null) {
            mViewHolder.tv_grade.setText(detailInfo.getGrade());
            mViewHolder.tv_reactos.setText(detailInfo.getReactOS());
            mViewHolder.tv_press.setText(detailInfo.getPress());
        }

        if (position == selectItem) {
            textColor = R.color.colorFontGreen;
        }
        mViewHolder.tv_grade.setTextColor(mContext.getResources().getColor(textColor));
        mViewHolder.tv_reactos.setTextColor(mContext.getResources().getColor(textColor));
        mViewHolder.tv_press.setTextColor(mContext.getResources().getColor(textColor));
        return convertView;
    }

    public static class ViewHolder {
        public TextView tv_grade;
        public TextView tv_reactos;
        public TextView tv_press;
    }

    public void setSelectItem(int selectItem) {
        this.selectItem = selectItem;
        notifyDataSetChanged();
    }

    public void setSelectItem(BookInfo bookInfo){
        if (bookInfo == null) {
            return;
        }

        for(int i = 0; i < mList.size(); i++){
            BookInfo kBookInfo = mList.get(i);
            if(bookInfo.get_id().equals(kBookInfo.get_id())){
                selectItem = i;
                notifyDataSetChanged();
                break;
            }
        }
    }

    public int getSelectItem() {
        return this.selectItem;
    }

}
