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.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

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

import java.util.List;

/**
 * Created by Rehome-rjb1 on 2017/7/26.
 */

public class XsTjExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;


    List<XsDqgzTjListBean1.DataBeanX> datas;

    public XsTjExpandableListAdapter(Context context, List<XsDqgzTjListBean1.DataBeanX> datas) {
        this.datas = datas;
        this.context = context;
    }

    /**
     * 父项的数量
     *
     * @return
     */
    @Override
    public int getGroupCount() {
        return datas.size();
    }

    /**
     * 子项的数量
     *
     * @param groupPosition
     * @return
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return datas.get(groupPosition).getData().size();
    }

    /**
     * 获得父项
     *
     * @param groupPosition
     * @return
     */
    @Override
    public Object getGroup(int groupPosition) {
        return datas.get(groupPosition);
    }

    /**
     * 获得子项
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datas.get(groupPosition).getData().get(childPosition);
    }

    /**
     * 获得父项ID
     *
     * @param groupPosition
     * @return
     */
    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }


    /**
     * 获得子项ID
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        GroupViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_parent, null);
            holder = new GroupViewHolder();
            holder.ivLeft = (ImageView) convertView.findViewById(R.id.iv_left);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }

        holder.tvTitle.setText(datas.get(groupPosition).getRq() + "数据");
        if (isExpanded) {
            holder.ivLeft.setImageResource(R.mipmap.unfold);
        } else {
            holder.ivLeft.setImageResource(R.mipmap.packup);
        }

        AutoUtils.auto(convertView);

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ChildViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_dqgz_tj, null);
            holder = new ChildViewHolder();
            holder.tvJhmc = (TextView) convertView.findViewById(R.id.tv_jhmc);
            holder.tvZxsj = (TextView) convertView.findViewById(R.id.tv_zxsj);
            holder.tvZxr = (TextView) convertView.findViewById(R.id.tv_zxr);
            holder.tvZxqk = (TextView) convertView.findViewById(R.id.tv_zxqk);
            holder.tvZxzt = (TextView) convertView.findViewById(R.id.tv_zxzt);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }

        holder.tvJhmc.setText("计划名称：" + datas.get(groupPosition).getData().get(childPosition).getJhmc());
        holder.tvZxsj.setText("执行时间：" + datas.get(groupPosition).getData().get(childPosition).getZxsj());
        holder.tvZxr.setText("执行人：" + datas.get(groupPosition).getData().get(childPosition).getZxr());
        holder.tvZxqk.setText("执行情况：" + datas.get(groupPosition).getData().get(childPosition).getZxqk());

        String status = "";

        switch (datas.get(groupPosition).getData().get(childPosition).getZxzt()) {
            case "0":
                status = "未执行";
                break;
            case "1":
                status = "工作完成";
                break;
            case "2":
                status = "工作取消";
                break;
            case "3":
                status = "延期执行";
                break;
            case "4":
                status = "申请中";
                break;
        }

        holder.tvZxzt.setText(status);
        AutoUtils.auto(convertView);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupViewHolder {
        ImageView ivLeft;
        TextView tvTitle;
    }

    class ChildViewHolder {
        TextView tvJhmc;
        TextView tvZxsj;
        TextView tvZxr;
        TextView tvZxqk;
        TextView tvZxzt;
    }
}
