package com.example.administrator.zhapp.adapter;

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

import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.entity.PublicInfo;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * 货架列表适配器
 */
public class PublicAdapter extends BaseAdapter implements View.OnClickListener {

    private List<PublicInfo> infos;
    private Context context;
    private Callback mCallback;

    public interface Callback {
        void cbClick(View view);
    }

    public PublicAdapter(List<PublicInfo> infos, Context context, Callback mCallback) {
        this.infos = infos;
        this.context = context;
        this.mCallback = mCallback;
    }

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

    @Override
    public Object getItem(int i) {
        return infos.get(i);
    }

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

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.list_item1,viewGroup, false);
            holder.tv_ms = (TextView) view.findViewById(R.id.tv_ms);
            holder.tv_sl = (TextView) view.findViewById(R.id.tv_sl);
            holder.tv_wl = (TextView) view.findViewById(R.id.tv_wl);
            holder.tv_hg = (TextView) view.findViewById(R.id.tv_hg);
            holder.cb = (CheckBox) view.findViewById(R.id.cb_isprint);
            holder.tv_pdsl = (TextView) view.findViewById(R.id.tv_pdsl);
            holder.tv_pdzt = (TextView) view.findViewById(R.id.tv_pdzt);
            view.setTag(holder);
            AutoUtils.autoSize(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tv_ms.setText(infos.get(i).getDescribe());
        holder.tv_wl.setText(infos.get(i).getPorject());
        holder.tv_sl.setText(infos.get(i).getCount() + "");
        holder.tv_hg.setText(infos.get(i).getHg());
        holder.cb.setChecked(infos.get(i).isFalg());
        holder.tv_pdzt.setText(infos.get(i).getPdzt());
        holder.tv_pdsl.setText(infos.get(i).getPdsl());

        holder.cb.setOnClickListener(this);
        holder.cb.setTag(i);

        return view;
    }

    @Override
    public void onClick(View view) {
        mCallback.cbClick(view);
    }


    static class ViewHolder {
        TextView tv_sl;
        TextView tv_ms;
        TextView tv_wl;
        TextView tv_hg;
        CheckBox cb;
        TextView tv_pdsl;
        TextView tv_pdzt;
    }
}
