package com.stu.assistant.adapter;

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

import com.stu.assistant.R;
import com.stu.assistant.bean.TableBean;
import com.stu.assistant.constants.ConstansTableItemManager;

import java.util.ArrayList;

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

public class TableListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    ArrayList<TableBean> list1 = new ArrayList<>();
    ArrayList<TableBean> list2 = new ArrayList<>();
    ArrayList<TableBean> list3 = new ArrayList<>();
    ArrayList<TableBean> list4 = new ArrayList<>();
    ArrayList<TableBean> list5 = new ArrayList<>();
    Context mContext;
    private String userLevel;
    private int itemType;

    public TableListAdapter(Context mContext) {
        this.mContext = mContext;
    }


    public void setList(ArrayList<TableBean> list1, ArrayList<TableBean> list2, ArrayList<TableBean> list3, ArrayList<TableBean> list4, ArrayList<TableBean> list5) {
        this.list1 = list1;
        this.list2 = list2;
        this.list3 = list3;
        this.list4 = list4;
        this.list5 = list5;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemViewHodler sectionViewHodler = null;
        sectionViewHodler = new ItemViewHodler(LayoutInflater.from(mContext).inflate(R.layout.table_item_layout, parent, false), viewType);
        return sectionViewHodler;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ItemViewHodler itemViewHodler = (ItemViewHodler) holder;
        itemViewHodler.time1Tv.setText(list1.get(position).getTableNameMsg() + "[" +list1.get(position).getTeacherName()+","+ list1.get(position).getAddress()+"]");
        itemViewHodler.time2Tv.setText(list2.get(position).getTableNameMsg() + "[" + list2.get(position).getTeacherName()+","+list2.get(position).getAddress()+"]");
        itemViewHodler.time3Tv.setText(list3.get(position).getTableNameMsg() + "[" + list3.get(position).getTeacherName()+","+list3.get(position).getAddress()+"]");
        itemViewHodler.time4Tv.setText(list4.get(position).getTableNameMsg() + "[" + list4.get(position).getTeacherName()+","+list4.get(position).getAddress()+"]");
        itemViewHodler.time5Tv.setText(list5.get(position).getTableNameMsg() + "[" + list5.get(position).getTeacherName()+","+list5.get(position).getAddress()+"]");
        itemViewHodler.timeTv.setText("" + (position + 1));

        itemViewHodler.time1Tv.setText(itemViewHodler.time1Tv.getText().toString().replace("[,]",""));
        itemViewHodler.time2Tv.setText(itemViewHodler.time2Tv.getText().toString().replace("[,]",""));
        itemViewHodler.time3Tv.setText(itemViewHodler.time3Tv.getText().toString().replace("[,]",""));
        itemViewHodler.time4Tv.setText(itemViewHodler.time4Tv.getText().toString().replace("[,]",""));
        itemViewHodler.time5Tv.setText(itemViewHodler.time5Tv.getText().toString().replace("[,]",""));
        setOnItem(itemViewHodler.time1Tv, position,list1.get(position), ConstansTableItemManager.item_1);
        setOnItem(itemViewHodler.time2Tv, position,list2.get(position), ConstansTableItemManager.item_2);
        setOnItem(itemViewHodler.time3Tv, position,list3.get(position), ConstansTableItemManager.item_3);
        setOnItem(itemViewHodler.time4Tv, position,list4.get(position), ConstansTableItemManager.item_4);
        setOnItem(itemViewHodler.time5Tv, position, list5.get(position),ConstansTableItemManager.item_5);
        setOnItem(itemViewHodler.timeTv, position,list1.get(position), ConstansTableItemManager.item_99);

    }

    private void setOnItem(TextView time1Tv, int position,TableBean tableBean, int item_1) {
        time1Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(item_1 != ConstansTableItemManager.item_99){
                    listeners.itemBean(position,tableBean, item_1);
                }
            }
        });
//        if (position % 2 == 0) {
//            time1Tv.setBackgroundResource(R.color.mistyrose);
//        } else {
//            time1Tv.setBackgroundResource(R.color.light_blue_50);
//        }
    }

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

    public class ItemViewHodler extends RecyclerView.ViewHolder {

        public TextView time1Tv, time2Tv, time3Tv, time4Tv, time5Tv, timeTv;
        public int viewType;

        public ItemViewHodler(@NonNull View itemView, int viewType) {
            super(itemView);
            this.viewType = viewType;
            time1Tv = itemView.findViewById(R.id.tv_time1);
            time2Tv = itemView.findViewById(R.id.tv_time2);
            time3Tv = itemView.findViewById(R.id.tv_time3);
            time4Tv = itemView.findViewById(R.id.tv_time4);
            time5Tv = itemView.findViewById(R.id.tv_time5);
            timeTv = itemView.findViewById(R.id.tv_time);
        }

        public int getViewType() {
            return viewType;
        }
    }

    public interface OnItemClickListeners {
        void itemBean(int pos,TableBean tableBean, int day);
    }

    public OnItemClickListeners listeners;

    public void setOnItemClickListeners(OnItemClickListeners listener) {
        this.listeners = listener;
    }
}
