package com.cygnusa.hyperm.adapter;

import android.content.Context;
import androidx.core.content.ContextCompat;
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.cygnusa.hyperm.R;
import com.cygnusa.hyperm.wrappers.ComparisonCategory;
import com.cygnusa.hyperm.wrappers.ComparisonCompareDataCategory;
import com.cygnusa.hyperm.wrappers.ComparisonCompareDataField;

import java.util.List;

public class ComparisionChildAdapter extends BaseExpandableListAdapter {


    private Context _context;
    private List<ComparisonCompareDataCategory> comparisonCategories;
    List<ComparisonCategory> checkColorList;

    public ComparisionChildAdapter(Context context, List<ComparisonCompareDataCategory> comparisonCategories, List<ComparisonCategory> comparisonCategoryList) {
        this._context = context;
        this.comparisonCategories = comparisonCategories;
        this.checkColorList = comparisonCategoryList;

    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return comparisonCategories.get(groupPosition).getFields().size();
    }

    @Override
    public ComparisonCompareDataCategory getGroup(int groupPosition) {
        return comparisonCategories.get(groupPosition);
    }

    @Override
    public ComparisonCompareDataField getChild(int groupPosition, int childPosition) {
        return comparisonCategories.get(groupPosition).getFields().get(childPosition);
    }

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

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ComparisonCompareDataCategory category = getGroup(groupPosition);
        ParentViewHolder holder;
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_header, null);
            holder = new ParentViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ParentViewHolder) convertView.getTag();
        }
        holder.list_header.setText(category.getTitle());
        holder.list_header.setTextColor(ContextCompat.getColor(_context, R.color.material_bluegrey800));

        holder.list_header.setCompoundDrawablesWithIntrinsicBounds(0, 0, android.R.drawable.arrow_down_float, 0);
        return convertView;
    }

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

        ComparisonCompareDataField field = getChild(groupPosition, childPosition);
        ChildViewHolder holder;
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            assert infalInflater != null;
            convertView = infalInflater.inflate(R.layout.child_list_item_new, null);
            holder = new ChildViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        holder.list_item_value.setText(field.getValue());

        String fieldValue = field.getValue().trim();
        String childValue = checkColorList.get(groupPosition).getFields().get(childPosition).getValue().trim();


        if(fieldValue.equalsIgnoreCase(childValue)){
            holder.list_item_color.setVisibility(View.GONE);
            holder.list_item_value.setVisibility(View.VISIBLE);
            holder.list_item_value.setText(childValue+"");
        }
//        if (fieldValue.equalsIgnoreCase(childValue)) {
//            holder.list_item_color.setVisibility(View.VISIBLE);
//            holder.list_item_value.setVisibility(View.GONE);
//            holder.list_item_color.setImageDrawable(ContextCompat.getDrawable(_context, R.mipmap.right));
//        } else if (TextUtils.isEmpty(fieldValue) || fieldValue.equalsIgnoreCase("") || fieldValue.equalsIgnoreCase("NA")) {
//            holder.list_item_color.setVisibility(View.VISIBLE);
//            holder.list_item_value.setVisibility(View.GONE);
//            holder.list_item_color.setImageDrawable(ContextCompat.getDrawable(_context, R.mipmap.cancel));
//        } else {
//            holder.list_item_color.setVisibility(View.GONE);
//            holder.list_item_value.setVisibility(View.VISIBLE);
//        }

        return convertView;
    }

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

    static class ParentViewHolder {
        TextView list_header;

        ParentViewHolder(View view) {
            list_header = view.findViewById(R.id.list_header);
        }
    }

    static class ChildViewHolder {
        TextView list_item_value;
        ImageView list_item_color;

        ChildViewHolder(View view) {
            list_item_value = view.findViewById(R.id.list_item_value);
            list_item_color = view.findViewById(R.id.list_item_color);
        }
    }
}
