package com.input.board.appskey.keyboard.recyAdapter;

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

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

import com.input.board.appskey.keyboard.R;
import com.input.board.appskey.keyboard.jsonBean.MainBean;

import java.util.List;

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

    private Context mContext;
    private List<MainBean> viewAllList;

    private OnChangListener mOnChangListener;

    private int selectedPos = 0;

    private int oldSelectedPos = -1;

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

    @Override
    public void onBindViewHolder(@NonNull ViewAllTabAdapter.ViewAllTabViewHolder holder, int pos) {
        MainBean mainBean = viewAllList.get(pos);

        holder.tabTitle.setText(mainBean.getParentName().toUpperCase());
        if (pos == selectedPos) {
            holder.tabTitle.setTextColor(mContext.getColor(R.color.white));
            holder.tabTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
            holder.tabIndicator.setVisibility(View.VISIBLE);
        } else {
            holder.tabTitle.setTextColor(mContext.getColor(R.color.view_all_text_color));
            holder.tabTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,13);
            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<MainBean> 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);
    }
}
