package com.keyboard.bubble.skin.keyboard.myada;

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

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

import com.keyboard.bubble.skin.keyboard.R;
import com.keyboard.bubble.skin.keyboard.bean.ParentBean;

import java.util.List;

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

    private Context mContext;
    private List<ParentBean> viewAllList;

    private OnChangListener mOnChangListener;

    private int selectedPos = 0;

    private int oldSelectedPos = -1;

    public AdapterTab(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 AdapterTab.ViewAllTabViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_preview, parent, false);
        return new AdapterTab.ViewAllTabViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterTab.ViewAllTabViewHolder holder, int pos) {
        ParentBean aParentBean = viewAllList.get(pos);

        holder.tabTitle.setText(aParentBean.getParentName().toUpperCase());
        if (pos == selectedPos) {
            holder.tabTitle.setSelected(true);
//            holder.tabTitle.setBackground(ContextCompat.getDrawable(App.appInstance,R.drawable.selected_tab_bg));
//            holder.tabTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,17);
//            holder.tabIndicator.setVisibility(View.VISIBLE);
        } else {
            holder.tabTitle.setSelected(false);
//            holder.tabTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
//            holder.tabIndicator.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<ParentBean> list) {
        viewAllList = list;
        notifyDataSetChanged();
    }

    static class ViewAllTabViewHolder extends RecyclerView.ViewHolder {


        private TextView tabTitle;
//        private LinearLayout tabIndicator;
        private RelativeLayout itemLayout;

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

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