package com.keyboard.themes.key.daapter;

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

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

import com.keyboard.themes.key.R;
import com.keyboard.themes.key.data.Parent;

import java.util.List;

public class AdapterTabName extends RecyclerView.Adapter<AdapterTabName.ViewAllTabViewHolder> {

    private Context mContext;
    private List<Parent> viewAllList;

    private OnChangListener mOnChangListener;

    private int selectedPos = 0;

    private int oldSelectedPos = -1;

    public AdapterTabName(Context context) {
        mContext = context;
    }

    public void setSelectedPos(int selectedPosition) {
        oldSelectedPos = selectedPos;
        selectedPos = selectedPosition;
        notifyItemChanged(oldSelectedPos);
        notifyItemChanged(selectedPos);
    }


    public void setChangeListener(OnChangListener onChangListener) {
        mOnChangListener = onChangListener;
    }


    @NonNull
    @Override
    public AdapterTabName.ViewAllTabViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.theme_item_preview, parent, false);
        return new AdapterTabName.ViewAllTabViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterTabName.ViewAllTabViewHolder holder, int pos) {
        Parent aParent = viewAllList.get(pos);

        holder.tabTitle.setText(aParent.getParentName().toUpperCase());
        if (pos == selectedPos) {
            holder.tabTitle.setSelected(true);
            holder.indicator.setVisibility(View.VISIBLE);
        } else {
            holder.tabTitle.setSelected(false);
            holder.indicator.setVisibility(View.GONE);

        }
        holder.itemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oldSelectedPos = selectedPos;
                selectedPos = holder.getAdapterPosition();

                notifyItemChanged(oldSelectedPos);
                notifyItemChanged(selectedPos);
                mOnChangListener.onChange(selectedPos);
            }
        });
    }

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

    public void tabTitleName(List<Parent> list) {
        viewAllList = list;
        notifyDataSetChanged();
    }

    static class ViewAllTabViewHolder extends RecyclerView.ViewHolder {


        private TextView tabTitle;
        private View indicator;
        private LinearLayout itemLayout;

        public ViewAllTabViewHolder(@NonNull View itemView) {
            super(itemView);
            tabTitle = itemView.findViewById(R.id.text_tab_view);
            indicator = itemView.findViewById(R.id.indicator);
            itemLayout = itemView.findViewById(R.id.item_layout);
        }
    }

    public interface OnChangListener {
        void onChange(int posit);
    }
}
