package com.cygnusa.hyperm.adapter;

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

import java.util.List;

public class ComparisionExpandableListAdapterNew extends BaseExpandableListAdapter {


    private Context _context;
    private List<ComparisonCategory> comparisonCategories;
    private List<ComparisonCompareDataCategory> comparisonOne;
    private List<ComparisonCompareDataCategory> comparisonTwo;

    public ComparisionExpandableListAdapterNew(Context context, List<ComparisonCategory> comparisonCategories,
                                               List<ComparisonCompareDataCategory> comparisonOne,
                                               List<ComparisonCompareDataCategory> comparisonTwo) {
        this._context = context;
        this.comparisonCategories = comparisonCategories;
        this.comparisonOne = comparisonOne;
        this.comparisonTwo = comparisonTwo;

    }

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

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

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

    @Override
    public ComparisonField 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) {
        ComparisonCategory 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_new, null);
            holder = new ParentViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ParentViewHolder) convertView.getTag();
        }
        holder.list_header.setText(category.getTitle());

        if (comparisonOne == null) {
            holder.headerOne.setVisibility(View.INVISIBLE);
        } else {
            holder.headerOne.setVisibility(View.VISIBLE);
            holder.list_header_one.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.dropdown_new, 0);
        }

        if (comparisonTwo == null) {
            holder.headerTwo.setVisibility(View.INVISIBLE);
        } else {
            holder.list_header_one.setCompoundDrawables(null, null, null, null);
            holder.headerTwo.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

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

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

        if (comparisonOne == null) {
            holder.itemOne.setVisibility(View.INVISIBLE);
        } else {
            holder.itemOne.setVisibility(View.VISIBLE);
            holder.list_item_value_one.setText(comparisonOne.get(groupPosition).getFields().get(childPosition).getValue());
        }

        if (comparisonTwo == null) {
            holder.itemTwo.setVisibility(View.INVISIBLE);
        } else {
            holder.itemTwo.setVisibility(View.VISIBLE);
            holder.list_item_value_two.setText(comparisonTwo.get(groupPosition).getFields().get(childPosition).getValue());
        }

        return convertView;
    }

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

    public void update(List<ComparisonCompareDataCategory> comparisonOne,
                       List<ComparisonCompareDataCategory> comparisonTwo) {
        this.comparisonOne = comparisonOne;
        this.comparisonTwo = comparisonTwo;
        notifyDataSetChanged();
    }

    static class ParentViewHolder {
        TextView list_header, list_header_one, list_header_two;
        LinearLayout headerOne, headerTwo;

        ParentViewHolder(View view) {
            list_header = view.findViewById(R.id.list_header);
            list_header_one = view.findViewById(R.id.list_header_one);
            list_header_two = view.findViewById(R.id.list_header_two);
            headerTwo = view.findViewById(R.id.headerTwo);
            headerOne = view.findViewById(R.id.headerOne);
        }
    }

    static class ChildViewHolder {
        TextView list_item_type, list_item_value, list_item_value_one, list_item_value_two;
        LinearLayout itemOne, itemTwo;

        ChildViewHolder(View view) {
            list_item_type = view.findViewById(R.id.list_item_type);
            list_item_value = view.findViewById(R.id.list_item_value);
            list_item_value_one = view.findViewById(R.id.list_item_value_one);
            list_item_value_two = view.findViewById(R.id.list_item_value_two);

            itemOne = view.findViewById(R.id.itemOne);
            itemTwo = view.findViewById(R.id.itemTwo);
        }
    }
}
