package com.sjkj.base_lib.tree.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sjkj.base_lib.R;
import com.sjkj.base_lib.weight.tree.Node;
import com.sjkj.base_lib.weight.tree.TreeListViewAdapter;

import java.util.List;

/**
 * Created by zhangke on 2017-1-14.
 */
public class SelectTreeAdapter extends TreeListViewAdapter {
    public SelectTreeAdapter(ListView mTree, Context context, List<Node> datas, int defaultExpandLevel, int iconExpand, int iconNoExpand) {
        super(mTree, context, datas, defaultExpandLevel, iconExpand, iconNoExpand);
    }

    public SelectTreeAdapter(ListView mTree, Context context, List<Node> datas,
                             int defaultExpandLevel) {
        super(mTree, context, datas, defaultExpandLevel);
    }

    @Override
    public View getConvertView(final Node node, int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item_tree_select, parent, false);
            viewHolder = new ViewHolder();
//            viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb_select_tree);
//            viewHolder.cb.setVisibility(View.GONE);
            viewHolder.label = (TextView) convertView.findViewById(R.id.id_treenode_label);
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
//        viewHolder.cb.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                setChecked(node, viewHolder.cb.isChecked());
//            }
//        });

//        if (node.isChecked()) {
//            viewHolder.tvBtn.setChecked(true);
//        } else {
//            viewHolder.tvBtn.setChecked(false);
//        }

        if (node.getIcon() == -1) {
            viewHolder.icon.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.icon.setVisibility(View.VISIBLE);
            viewHolder.icon.setImageResource(node.getIcon());
        }

        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (mNodes != null && mNodes.size() > 0) {
//                    for (int i = 0; i < mNodes.size(); i++) {
//                        mNodes.get(i).setChecked(false);
//                    }
//                    mNodes.get(position).setChecked(true);
//                }
//                setChecked(node, viewHolder.tvBtn.isChecked());

                if (detailClickListener != null)
                    detailClickListener.click(node, position);
            }
        });

        viewHolder.label.setText(node.getName());

        return convertView;
    }

    private final class ViewHolder {
        ImageView icon;
        //        CheckBox cb;
        TextView label;
        ImageView imageView;
    }

    public interface OnDetailClickListener {
        void click(Node node, int position);
    }

    private OnDetailClickListener detailClickListener;

    public void setDetailClickListener(OnDetailClickListener detailClickListener) {
        this.detailClickListener = detailClickListener;
    }
}
