package hf.lskj.com.carstockcount.adapter;

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

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import hf.lskj.com.carstockcount.R;
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/1.
 */

public class CountRecordAdapter extends BaseAdapter {
    private Context mContext;
    private List<Record> mRecords;
    private LayoutInflater inflater;

    private Drawable drawableIn, drawableOut, drawableMove;

    public CountRecordAdapter(Context context, List<Record> records) {
        this.mContext = context;
        this.mRecords = records;
        inflater = LayoutInflater.from(mContext);
        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 mRecords.size();
    }

    @Override
    public Record getItem(int i) {
        return mRecords.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 = inflater.inflate(R.layout.item_record, viewGroup, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        Record record = getItem(i);
        viewHolder.tvVin.setText(record.getVin());
        viewHolder.tvRfid.setText(record.getRfid());
        viewHolder.tvTime.setText(DateUtils.format(record.getCreateTime()));
        viewHolder.tvStatus.setText(record.getStatus());
        switch (record.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;
        }
        switch (record.getType()){
            case Record.TYPE_BANGDING:
                viewHolder.tvTimeTitle.setText("绑定时间:");
                break;
            case Record.TYPE_JIEBANG:
                viewHolder.tvTimeTitle.setText("解绑时间:");
                break;
            default:
                viewHolder.tvTimeTitle.setText("盘点时间:");
                break;
        }
        return view;
    }

    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_title)
        TextView tvTimeTitle;
        @Bind(R.id.tv_time)
        TextView tvTime;

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