package com.tofly.ghrq_route.common.ui.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.esri.arcgisruntime.data.Feature;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.common.ui.view.PipeAnalyzeResultBottomSheet;
import com.tofly.ghrq_route.module.pipeAnalyze.domain.PipeAnalysisResultBean;
import com.tofly.ghrq_route.module.pipeAnalyze.domain.PipeAnalysisResultBean.ResultBean.SummaryBean.LayersummaryBean;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 重要注释
 * <p>
 * 二级listview的适配器 在调用notifyDataSetChanged()后，
 * 使用listview
 * resultsListView.collapseGroup(i); //收起
 * resultsListView.expandGroup(i); //展开
 * 两个方法后，数据才能在界面上正常刷新
 */

public class ListLayerSummaryAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<PipeAnalysisResultBean.ResultBean.SummaryBean.LayersummaryBean> mSummaryList;
    private Map<Integer, List> integerListMap;
    private static final String NULL_STR = "null";

    public ListLayerSummaryAdapter(Context context, List<PipeAnalysisResultBean.ResultBean.SummaryBean.LayersummaryBean> summaryList) {
        mContext = context;
        mSummaryList = summaryList;
        initIntegerListMap(summaryList);

    }

    public ListLayerSummaryAdapter(Context context, List<LayersummaryBean> summaryList, int groupPosition, List featrueList) {
        mContext = context;
        mSummaryList = summaryList;
        initIntegerListMap(summaryList);
        integerListMap.put(groupPosition, featrueList);
    }

    public ListLayerSummaryAdapter(Context context, List<LayersummaryBean> summaryList, String jobId) {
        mContext = context;
        mSummaryList = summaryList;
        initIntegerListMap(summaryList);
    }

    private void initIntegerListMap(List<LayersummaryBean> summaryList) {
        if (integerListMap == null) {
            integerListMap = new HashMap<>();
        } else {
            integerListMap.clear();
        }
        for (int i = 0; i < summaryList.size(); i++) {
            integerListMap.put(i, null);
        }
    }

    public void updateGroupData(List<LayersummaryBean> layersummaryBeen) {
        this.mSummaryList = layersummaryBeen;
        initIntegerListMap(layersummaryBeen);
        notifyDataSetChanged();
    }

    public void updateChildData(int childPostion, List<Feature> childList) {
        integerListMap.put(childPostion, childList);
        notifyDataSetChanged();

    }

    public void clearAllData(){
        mSummaryList.clear();
        integerListMap.clear();
        notifyDataSetChanged();
    }


    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getGroupCount() {
        return mSummaryList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (integerListMap != null) {
            List list = integerListMap.get(groupPosition);
            if (list != null)
                return list.size();
            else
                return 0;
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        Object object = null;
        if (groupPosition < mSummaryList.size()){
            object = mSummaryList.get(groupPosition);
        }
        return object;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (integerListMap != null) {
            int size = integerListMap.size();
            int i = groupPosition % size;
            List list = integerListMap.get(i);
            Object o = list.get(childPosition);
            return o;

        }

        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        if (integerListMap != null) {
            return childPosition;
        }
        return 0;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        PipeDetailViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pipe_analyze_result, null);
            viewHolder = new PipeDetailViewHolder();
            viewHolder.collapseLayout = convertView.findViewById(R.id.pipeanasis_layout_pipenet_collapse);
            viewHolder.nameText = convertView.findViewById(R.id.result_item_name);
            viewHolder.valueText = convertView.findViewById(R.id.result_item_attr);
            viewHolder.resultAreaLayout = convertView.findViewById(R.id.layout_result_result);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (PipeDetailViewHolder) convertView.getTag();
        }

        LayersummaryBean summary = (LayersummaryBean) getGroup(groupPosition);
        if (summary != null) {
            String layerName = summary.getLayername();
            viewHolder.nameText.setText(layerName);
            if ("esriGeometryPoint".equals(summary.getGeometrytype()))
                viewHolder.valueText.setText(String.valueOf(summary.getCount()));
            else if ("esriGeometryPolyline".equals(summary.getGeometrytype()))
                viewHolder.valueText.setText(String.valueOf(summary.getLength()));

        }

        //viewHolder.resultAreaLayout.setVisibility(View.GONE);

        return convertView;

    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_fragment_pipeanalyze, null);
            holder = new ViewHolder();
            holder.layoutHeader = convertView.findViewById(R.id.layout_header_row);
            holder.text_num = convertView.findViewById(R.id.text_num);
            holder.text_sid = convertView.findViewById(R.id.text_sid);
            holder.text_radius = convertView.findViewById(R.id.text_radius);
            /*holder.designPress = convertView.findViewById(R.id.pipenet_design_press);
            holder.material = convertView.findViewById(R.id.pipenet_material);
            holder.radius = convertView.findViewById(R.id.pipenet_radius);
            holder.address = convertView.findViewById(R.id.pipenet_address);
            holder.projectName = convertView.findViewById(R.id.pipenet_project_name);

            holder.num_header = convertView.findViewById(R.id.header_pipenet_num);
            holder.designPress_header = convertView.findViewById(R.id.header_pipenet_design_press);
            holder.material_header = convertView.findViewById(R.id.header_pipenet_material);
            holder.radius_header = convertView.findViewById(R.id.header_pipenet_radius);
            holder.address_header = convertView.findViewById(R.id.header_pipenet_address);
            holder.projectName_header = convertView.findViewById(R.id.header_pipenet_project_name);*/

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        boolean isShutValve = false;
        LayersummaryBean layerSummary = mSummaryList.get(groupPosition);
        if (layerSummary.getLayerdbname().equals(PipeAnalyzeResultBottomSheet.MUST_SHUT_VALVE_DBLAYER_DBNAME)){
            isShutValve = true;
        }

        if (childPosition == 0){
            holder.layoutHeader.setVisibility(View.VISIBLE);
        }else {
            holder.layoutHeader.setVisibility(View.GONE);
        }

        int visible = isShutValve ? View.GONE : View.VISIBLE;
        /*holder.designPress.setVisibility(visible);
        holder.material.setVisibility(visible);
        holder.radius.setVisibility(visible);
        holder.address.setVisibility(visible);
        holder.projectName.setVisibility(visible);*/

        if (childPosition % 2 == 1) {
            convertView.setBackgroundResource(R.color.scada_shadow);
        } else {
            convertView.setBackgroundResource(R.color.white);
        }

        int colorText = mContext.getResources().getColor(R.color.text);
        holder.text_num.setTextColor(colorText);
/*        holder.designPress.setTextColor(colorText);
        holder.material.setTextColor(colorText);
        holder.radius.setTextColor(colorText);
        holder.address.setTextColor(colorText);
        holder.projectName.setTextColor(colorText);*/

        holder.text_num.setText(String.valueOf(childPosition + 1));
        List list = integerListMap.get(groupPosition);
        if (list == null)
            return convertView;
        Feature feature = (Feature) list.get(childPosition);
        if (feature != null) {
            Map<String, Object> attributes = feature.getAttributes();

            if (attributes.containsKey("SID")) {
                Object object = attributes.get("SID");
                String sid = "";
                if (object != null && !object.toString().equalsIgnoreCase(NULL_STR)) {
                    sid = object.toString();
                }
                holder.text_sid.setText(sid);
            }

            if (attributes.containsKey("DIAMETER")) {
                Object object = attributes.get("DIAMETER");
                String radius  = "";
                if (object != null && !object.toString().equalsIgnoreCase(NULL_STR)) {
                    radius = object.toString();
                }
                holder.text_radius.setText(radius);
            }

            /*if (attributes.containsKey("DESPRESSURE")) {
                Object object = attributes.get("DESPRESSURE");
                String designPress = "";
                if (object != null && !object.toString().equalsIgnoreCase(NULL_STR)) {
                    designPress = object.toString();
                }
                holder.designPress.setText(designPress);
            }
            if (attributes.containsKey("MATERIAL")) {
                Object object = attributes.get("MATERIAL");
                String material = "";
                if (object != null && !object.toString().equalsIgnoreCase(NULL_STR)) {
                    material = object.toString();
                }
                holder.material.setText(material);
            }
            if (attributes.containsKey("DIAMETER")) {
                Object object = attributes.get("DIAMETER");
                String radius = "";
                if (object != null && !object.toString().equalsIgnoreCase(NULL_STR)) {
                    radius = object.toString();
                }
                holder.radius.setText(radius);
            }
            if (attributes.containsKey("ADDR")) {
                Object object = attributes.get("ADDR");
                String address = "";
                if (object != null && !object.toString().equalsIgnoreCase(NULL_STR)) {
                    address = object.toString();
                }
                holder.address.setText(address);
            }
            if (attributes.containsKey("DRAWNAME")) {
                Object object = attributes.get("DRAWNAME");
                String projectName = "";
                if (object != null && !object.toString().equalsIgnoreCase(NULL_STR)) {
                    projectName = object.toString();
                }
                holder.projectName.setText(projectName);
            }*/
        }

        return convertView;
    }

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

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

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

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }

    public static class PipeDetailViewHolder {
        LinearLayout collapseLayout;
        LinearLayout resultAreaLayout;
        TextView nameText;
        TextView valueText;

        public LinearLayout getResultAreaLayout() {
            return resultAreaLayout;
        }

        public LinearLayout getCollapseLayout() {
            return collapseLayout;
        }
    }


    private static class ViewHolder {
        LinearLayout layoutHeader;
        TextView text_num;
        TextView text_sid;
        TextView text_radius;
/*        TextView designPress;
        TextView material;
        TextView radius;
        TextView address;
        TextView projectName;

        TextView num_header;
        TextView designPress_header;
        TextView material_header;
        TextView radius_header;
        TextView address_header;
        TextView projectName_header;*/
    }

}
