package hf.lskj.com.carstockcount.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import hf.lskj.com.carstockcount.R;
import hf.lskj.com.carstockcount.db.CarInfo;
import hf.lskj.com.carstockcount.db.Record;
import hf.lskj.com.carstockcount.utils.DateUtils;

import static hf.lskj.com.carstockcount.db.CarInfo.STATUS_MOVE;
import static hf.lskj.com.carstockcount.db.CarInfo.STATUS_WEIPANDAO;

/**
 * Created by yaoju on 2017/8/2.
 */

public class CarListAdapter extends BaseAdapter implements Filterable {
    private Context mContext;
    private List<CarInfo> mCarList;//这个数据是会改变的，所以要有个变量来备份一下原始数据
    private List<CarInfo> backData;//用来备份原始数据
    private LayoutInflater mInflater;
    private MyFilter mFilter;
    private Drawable drawableIn, drawableOut, drawableMove;

    public CarListAdapter(Context context, List<CarInfo> mCarList) {
        this.mContext = context;
        this.mCarList = mCarList;
        backData = mCarList;
        mInflater = LayoutInflater.from(context);
        drawableIn = context.getResources().getDrawable(R.mipmap.zaiku_icon);
        drawableOut = context.getResources().getDrawable(R.mipmap.chuku_icon);
        drawableMove = context.getResources().getDrawable(R.mipmap.yidong_icon);
        drawableIn.setBounds(0, 0, drawableIn.getMinimumWidth(), drawableIn.getMinimumHeight());
        drawableOut.setBounds(0, 0, drawableOut.getMinimumWidth(), drawableOut.getMinimumHeight());
        drawableMove.setBounds(0, 0, drawableMove.getMinimumWidth(), drawableMove.getMinimumHeight());
    }

    @Override
    public int getCount() {
        return mCarList.size();
    }

    @Override
    public CarInfo getItem(int i) {
        return mCarList.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = mInflater.inflate(R.layout.item_car, viewGroup, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        CarInfo carInfo = getItem(i);
        viewHolder.tvVin.setText(carInfo.getVin());
        viewHolder.tvRfid.setText(carInfo.getRfid());
        viewHolder.tvTime.setText(DateUtils.format(carInfo.getStockCountTime()));
        viewHolder.tvStatus.setText(carInfo.getStatus());
        switch (carInfo.getStatus()) {
            case STATUS_MOVE:
                viewHolder.tvStatus.setCompoundDrawables(null, drawableMove, null, null);
                viewHolder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.car_move));
                break;
            case STATUS_WEIPANDAO:
                viewHolder.tvStatus.setCompoundDrawables(null, drawableOut, null, null);
                viewHolder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.car_out));
                break;
            default://在库
                viewHolder.tvStatus.setCompoundDrawables(null, drawableIn, null, null);
                viewHolder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.car_in));
                break;
        }
        if (Record.TYPE_PANDIAN.equals(carInfo.getStockCountType())){
            view.setBackgroundColor(Color.parseColor("#E6EFFF"));
        }else{
            view.setBackgroundResource(android.R.color.transparent);
        }
        return view;
    }

    //当ListView调用setTextFilter()方法的时候，便会调用该方法
    @Override
    public Filter getFilter() {
        if (null == mFilter) {
            mFilter = new MyFilter();
        }
        return mFilter;
    }

    // 自定义Filter类
    private class MyFilter extends Filter {
        @Override
        // 该方法在子线程中执行
        // 自定义过滤规则
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            List<CarInfo> newValues = new ArrayList<>();
            // 如果搜索框内容为空，就恢复原始数据
            if (TextUtils.isEmpty(constraint)) {
                newValues = backData;
            } else {
                String filterString = constraint.toString().trim()
                        .toLowerCase();
                if (filterString.equals("")) {
                    newValues = backData;
                } else {
                    // 过滤出新数据
                    for (CarInfo str : backData) {
                        if (str.getVin().contains(constraint)) {
                            newValues.add(str);
                        }
                    }
                }
            }

            results.values = newValues;
            results.count = newValues.size();

            return results;
        }

        //在publishResults方法中告诉适配器更新界面
        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            mCarList = (List<CarInfo>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();//通知数据发生了改变
            } else {
                notifyDataSetInvalidated();//通知数据失效
            }
        }
    }

    static class ViewHolder {
        @Bind(R.id.tv_status)
        TextView tvStatus;
        @Bind(R.id.tv_vin)
        TextView tvVin;
        @Bind(R.id.tv_rfid)
        TextView tvRfid;
        @Bind(R.id.tv_time)
        TextView tvTime;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
