package com.cpm.lorealbaautomation.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.constant.AlertandMessages;
import com.cpm.lorealbaautomation.constant.CommonFunctions;
import com.cpm.lorealbaautomation.gsonGetterSetter.ProductMaster;

import java.util.List;

public class SubAxeValueAdapter extends RecyclerView.Adapter<SubAxeValueAdapter.MyViewHolder> {
    LayoutInflater inflator;
    List<ProductMaster> masterList;
    private String selectedSubAxe;
    boolean disable, network_check;
    Onclick onclick;

    public SubAxeValueAdapter(Context context, List<ProductMaster> data, String selectedSubAxe, boolean disable, boolean network_check, Onclick onclick) {
        inflator = LayoutInflater.from(context);
        this.selectedSubAxe = selectedSubAxe;
        this.network_check = network_check;
        this.onclick = onclick;
        this.masterList = data;
        this.disable = disable;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = inflator.inflate(R.layout.item_filter, parent, false);
        return new MyViewHolder(view);
    }

    @SuppressLint({"NotifyDataSetChanged"})
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final ProductMaster current = masterList.get(position);
        holder.tv_item.setText(current.getSubAxeName());
        holder.tv_item.setId(position);

        if (!disable) {
            holder.tv_item.setOnClickListener(v -> {
                if (network_check && !CommonFunctions.checkNetIsAvailable(inflator.getContext())) {
                    AlertandMessages.showToastMsg(inflator.getContext(), inflator.getContext().getString(R.string.nonetwork));
                    return;
                }
                String placeHolder = "Axe Name";
                selectedSubAxe = current.getSubAxeName();
                switch (current.getSubAxeName()) {
                    case "Product":
                        placeHolder = "Product Name";
                        break;
                    case "Axe":
                        placeHolder = "Axe Name";
                        break;
                    case "Sub Axe":
                        placeHolder = "Sub Axe Name";
                        break;
                    case "Signature":
                        placeHolder = "Signature Name";
                        break;
                    case "Sub Brand":
                        placeHolder = "Sub Brand Name";
                        break;
                    case "Brand":
                        placeHolder = "Brand Name";
                        break;

                    default:
                        break;
                }

                onclick.onClick(current.getSubAxeName(), placeHolder);
                current.setSelected(true);
                notifyDataSetChanged();
            });
        }

        if (!disable) {
            holder.tv_item.setTextColor(inflator.getContext().getResources().getColor(R.color.white));
            holder.tv_item.setId(position);
            if (current.getSubAxeName().equalsIgnoreCase(selectedSubAxe)) {
                holder.card.setCardBackgroundColor(inflator.getContext().getResources().getColor(R.color.dashboard_ach));
                holder.card.setId(position);
            } else {
                holder.card.setCardBackgroundColor(inflator.getContext().getResources().getColor(R.color.grey_dark_background));
                holder.card.setId(position);
            }
        } else {
            holder.card.setCardBackgroundColor(inflator.getContext().getResources().getColor(R.color.circular_progress_default_background));
            holder.card.setId(position);

            holder.tv_item.setTextColor(inflator.getContext().getResources().getColor(R.color.disable_txt_color));
            holder.tv_item.setId(position);
        }
    }

    @Override
    public int getItemCount() {
        return masterList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_item;
        CardView card;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_item = (TextView) itemView.findViewById(R.id.tv_item);
            card = (CardView) itemView.findViewById(R.id.item_card);

        }
    }

    public interface Onclick {
        void onClick(String filterText, String placeHolder);
    }
}
