package com.qhclh.ytzh.work.breedpoultry;

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.CompoundButton;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.bean.FactryBean;

import java.util.List;

/**
 * Created by 青花瓷 on 2017/11/30.
 */
///< 养殖场
public class FactryAdapter extends BaseAdapter {

    private Context context;
    private List<FactryBean> list;
    private LayoutInflater layoutInflater;
    private List<Boolean> isCheck;

    public FactryAdapter(Context context, List<FactryBean> list, List<Boolean> isCheck) {
        this.context = context;
        this.list = list;
        this.isCheck = isCheck;
        layoutInflater = LayoutInflater.from(context);
    }

    public FactryAdapter(Context context, List<FactryBean> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

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

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

    @Override
    public long getItemId(int i) {
        return list.get(i).getId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        FactryBean message = list.get(i);
        final ViewHolder viewHolder;
        if (view == null){
            viewHolder = new ViewHolder();
            view = layoutInflater.inflate(R.layout.item_factry,viewGroup,false);
            viewHolder.factry_name = view.findViewById(R.id.factry_name);

            view.setTag(viewHolder);

        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.factry_name.setTag(i);
        viewHolder.factry_name.setChecked(isCheck.get(i));
        viewHolder.factry_name.setText(message.getName());
        viewHolder.factry_name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                int pos = (int) compoundButton.getTag();
                isCheck.set(pos, b);
                list.get(pos).setCheck(b);
                if (b){
                    viewHolder.factry_name.setTextColor(context.getResources().getColor(R.color.green029737));
                }else {
                    viewHolder.factry_name.setTextColor(context.getResources().getColor(R.color.black303030));
                }
            }
        });

        return view;
    }

    private class ViewHolder {
        public CheckBox factry_name;
    }
}
