package ir.almahdi.sandogh_bsa_aza.Tarakonesh_ozv;

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.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import ir.almahdi.sandogh_bsa_aza.R;

public class tarakoesh_adapter extends RecyclerView.Adapter<tarakoesh_adapter.MyViewHolder> {

    private tarako_karbar_frg myContext;
    private List<md_tarakonesh_ozv> myVamList;
    private static  int currentposition=0,num_expand=0;;

    tarakoesh_adapter(tarako_karbar_frg context, List<md_tarakonesh_ozv> myVamList) {
        this.myContext = context;
        this.myVamList = myVamList;    }



    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cdv_show_tarakonesh_ozv, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {

        md_tarakonesh_ozv tarakonesh_ozv = myVamList.get(position);
        if(tarakonesh_ozv.getId_ozviat() == null){
            holder.id_ozviat.setText( "-");
            holder.order_id.setText("-");
            holder.track_id.setText("-");
            holder.amount.setText("-");
            holder.date.setText("-");


        }else{holder.id_ozviat.setText( tarakonesh_ozv.getId_ozviat());
            holder.order_id.setText(tarakonesh_ozv.getId_order());
            holder.track_id.setText(tarakonesh_ozv.getTrack_id());
            holder.amount.setText(tarakonesh_ozv.getAmount());
            holder.date.setText(tarakonesh_ozv.getDate());}



        //expand recyclview
        if (currentposition==position){
            holder.linear_tarak_ozv.setVisibility(View.VISIBLE);
            num_expand=1;

        }

        holder.cdv_show_tarakonesh_ozv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (num_expand==1){
                    holder.linear_tarak_ozv.setVisibility(View.GONE);
                    num_expand=0;}
                else{
                    currentposition=position;
                    notifyDataSetChanged();}

            }
        });
        // end expand




    }

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

    // ViewHoldr Class

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView id_ozviat,amount,track_id,
                order_id,date;

CardView cdv_show_tarakonesh_ozv;
LinearLayout linear_tarak_ozv;

        MyViewHolder(View itemView) {
            super(itemView);
            amount =itemView.findViewById(R.id.txt_mab_tarak);
            id_ozviat = itemView.findViewById(R.id.txt_idozviat_trak);
           track_id= itemView.findViewById(R.id.txt_track_id);
            order_id = itemView.findViewById(R.id.txt_order_id);
            date = itemView.findViewById(R.id.txt_date);
            /////cardview
            cdv_show_tarakonesh_ozv=itemView.findViewById(R.id.cdv_show_tarak_ozv);
            linear_tarak_ozv=itemView.findViewById(R.id.linear_tarak_ozv);
        }

    }
}

