package com.cygnusa.hyperm.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.wrappers.ComparisonCompareModel;

import java.util.List;

public class ComparisonCompareModelLayoutAdapter extends RecyclerView.Adapter<ComparisonCompareModelLayoutAdapter.ViewHolder> {
    List<ComparisonCompareModel> body;
    Context activity;
    int totalSize = 0, comparePositionModel;

    public ComparisonCompareModelLayoutAdapter(Context activity, List<ComparisonCompareModel> body, int comparePositionModel) {
        this.activity = activity;
        this.body = body;
        this.comparePositionModel = comparePositionModel;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_drag_comparison_list, parent, false);

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
//        viewHolder.textView_parentName.setText(body.get(comparePositionModel).getCategory().get(i).getTitle());
//        viewHolder.textView_parentName.setTextColor(ContextCompat.getColor(activity,R.color.whitecolor));
//        Log.e("noOfChildTextViews", body.get(comparePositionModel).getCategory().get(i).getTitle() + String.valueOf(i));

        int noOfChildTextViews = viewHolder.linearLayout_childItems.getChildCount();
        int noOfChild = body.get(comparePositionModel).getCategory().get(i).getFields().size();

        if (noOfChild < noOfChildTextViews) {
            for (int index = noOfChild; index < noOfChildTextViews; index++) {
                TextView currentTextView = (TextView) viewHolder.linearLayout_childItems.getChildAt(index);
                TextView valueTextView = (TextView) viewHolder.linearLayout_child_values.getChildAt(index);
                valueTextView.setVisibility(View.GONE);
                currentTextView.setVisibility(View.GONE);
            }
        }
        for (int textViewIndex = 0; textViewIndex < noOfChild; textViewIndex++) {
            TextView currentTextView = (TextView) viewHolder.linearLayout_childItems.getChildAt(textViewIndex);
            TextView valueTextView = (TextView) viewHolder.linearLayout_child_values.getChildAt(textViewIndex);

            //currentTextView.setText(body.get(comparePositionModel).getCategory().get(i).getFields().get(textViewIndex).getTitle());
            valueTextView.setText(body.get(comparePositionModel).getCategory().get(i).getFields().get(textViewIndex).getValue());
        }
    }


    @Override
    public int getItemCount() {
        Log.e("return size", String.valueOf(body.get(comparePositionModel).getCategory().size()));
        return body.get(comparePositionModel).getCategory().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView textView_parentName;
        LinearLayout linearLayout_childItems, linearLayout_child_values/*, linearLayout_colan*/;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView_parentName = itemView.findViewById(R.id.tv_parentName);
            linearLayout_childItems = itemView.findViewById(R.id.ll_child_items);
            linearLayout_child_values = itemView.findViewById(R.id.ll_child_values);

            linearLayout_childItems.setVisibility(View.GONE);
            linearLayout_child_values.setVisibility(View.VISIBLE);
            int intMaxNoOfChild = 0;
            for (int index = 0; index < body.get(comparePositionModel).getCategory().size(); index++) {
                int intMaxSizeTemp = body.get(comparePositionModel).getCategory().get(index).getFields().size();
                if (intMaxSizeTemp > intMaxNoOfChild) intMaxNoOfChild = intMaxSizeTemp;
            }
            for (int indexView = 0; indexView < intMaxNoOfChild; indexView++) {
                TextView textView = new TextView(activity);
                textView.setId(indexView);
                textView.setPadding(20, 20, 20, 20);
                textView.setGravity(Gravity.START | Gravity.START);
                //textView.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_sub_module_text));
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                textView.setOnClickListener(this);
                textView.setSingleLine(true);
                linearLayout_childItems.addView(textView, layoutParams);

                TextView textViewValues = new TextView(activity);
                textViewValues.setId(indexView);
                textViewValues.setPadding(20, 20, 20, 20);
                textViewValues.setGravity(Gravity.START | Gravity.START);
                //textViewValues.setBackground(ContextCompat.getDrawable(activity, R.drawable.background_sub_module_text));
                LinearLayout.LayoutParams layoutParamsValues = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                textView.setOnClickListener(this);
                textViewValues.setSingleLine(true);
                linearLayout_child_values.addView(textViewValues, layoutParamsValues);

            }
            textView_parentName.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.tv_parentName) {
                if (linearLayout_child_values.getVisibility() == View.VISIBLE) {
                    linearLayout_childItems.setVisibility(View.GONE);
                    linearLayout_child_values.setVisibility(View.VISIBLE);
                } else {
                    linearLayout_childItems.setVisibility(View.GONE);
                    linearLayout_child_values.setVisibility(View.VISIBLE);
                }
            } else {
                TextView textViewClicked = (TextView) v;
                Toast.makeText(activity, "" + textViewClicked.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}
