package ir.almahdi.sandogh_bsa_aza.Add_OtherOzv_pay;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

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

import java.util.ArrayList;
import java.util.List;

import ir.almahdi.sandogh_bsa_aza.Add_OtherOzv_pay.model.md_kol_aza;
import ir.almahdi.sandogh_bsa_aza.Global_var;
import ir.almahdi.sandogh_bsa_aza.R;

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

    private Context myContext;


    public List<md_kol_aza> myVamList_other,filterList;
    private CustomFilter_kolAza filter;



    public List<md_kol_aza> getvam_Other_list (){
        return myVamList_other;
    }
    public KolAza_adapter(Context context, ArrayList<md_kol_aza> myList_ozv) {
        this.myContext =  context;
        this.myVamList_other = myList_ozv;
        this.filterList = myList_ozv;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView id_ozviat,id_vam,nfamil ,
                mab_vam,tedad_aghsat,mande_vam,tedad_gest_par,mab_gh_vam;
        ImageView img_add_tolist_pard;
        TableLayout tablayout_mosh_ozv;
        LinearLayout linerlayout_Other;
        public CheckBox chkSelected;

        MyViewHolder(View itemView) {
            super(itemView);
            //linear expand
            linerlayout_Other=itemView.findViewById(R.id.linerlayout_Other);
            //dimention=new LinearLayout.LayoutParams(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.WRAP_CONTENT);
            //   linerlayout_Other.setLayoutParams(dimention);
            nfamil =itemView.findViewById(R.id.txt_nfamil);
            id_ozviat = itemView.findViewById(R.id.idvam);
            chkSelected=itemView.findViewById(R.id.chkSelected);
            mab_gh_vam=itemView.findViewById(R.id.txt_mab_gest_sabad);
            mab_vam=itemView.findViewById(R.id.mablagh_vam_sabad);
            mande_vam=itemView.findViewById(R.id.txtmande_vam_sabad);
//img_add_tolist_pard
             img_add_tolist_pard = itemView.findViewById(R.id.img_add_tolist_pard);
            tablayout_mosh_ozv=itemView.findViewById(R.id.tablayout_mosh_ozv);
        }

        @SuppressLint("SetTextI18n")
        void setvamInfo(md_kol_aza set_karbar_torecy) {
            nfamil.setText(set_karbar_torecy.getName_ozv()+" "+set_karbar_torecy.getLastname_ozv());
            id_ozviat.setText(set_karbar_torecy.getId_ozviat());
            // فعلا کوئری اقساط پرداختی ومانده وام را ننوشتیم
            //mande_vam.setText(set_karbar_torecy..toString());
            mab_gh_vam.setText(Global_var.splitDigits(set_karbar_torecy.getMablagh_gh_vam_nob()));
        }
    }

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

        return new MyViewHolder(view);
    }

    @SuppressLint("Assert")
    @Override
    public void onBindViewHolder(KolAza_adapter.MyViewHolder holder, int position) {
        final int pos = position;

        md_kol_aza entiremd_kolaza = myVamList_other.get(position);
        holder.setvamInfo(entiremd_kolaza);
      //  holder.chkSelected.setChecked(myVamList_other.get(position).isSelected());

        holder.chkSelected.setTag(entiremd_kolaza);

             // checbox select
            holder.chkSelected.setOnClickListener(v -> {
            CheckBox cb = (CheckBox) v;
                    md_kol_aza vam_other = (md_kol_aza) cb.getTag();

                        if(!vam_other.isSelected()){
                            vam_other.setSelected(cb.isChecked());
                            entiremd_kolaza.setSelected(cb.isChecked());
                            holder.img_add_tolist_pard.setVisibility(View.VISIBLE);}
//                        else {
////                            vam_other.setSelected(cb.isChecked());
//
//                           holder.img_add_tolist_pard.setVisibility(View.GONE);
//                        }
//
//                        vam_other.setSelected(false);
//                        img_add_tolist_pard.setVisibility(View.GONE);}

        });
        // checbox select
        // add checked to list pardakhti
        holder.img_add_tolist_pard.setOnClickListener(v -> {
            if (Global_var.num_array_gkol_id_ozviat<10) {
//                Global_var.gkol_id_ozviat[0]=myVamList_other.get(pos).getId_ozviat() ;
//                Toast.makeText(this.myContext,myVamList_other.get(pos).getLastname_ozv()+" به سبد پرداخت شما اضافه شد",Toast.LENGTH_SHORT).show();
//                Toast.makeText(this.myContext, Global_var.num_array_gkol_id_ozviat+1 + "عضو در سبد دارید!",Toast.LENGTH_SHORT).show();
//                Global_var.num_array_gkol_id_ozviat+=1;
//            }else{
                Global_var.gkol_id_ozviat[Global_var.num_array_gkol_id_ozviat] = entiremd_kolaza.getId_ozviat();
                //holder.tablayout_mosh_ozv.setVisibility(View.GONE);
                Global_var.num_array_gkol_id_ozviat += 1;
                Toast.makeText(this.myContext, entiremd_kolaza.getLastname_ozv() + " به سبد پرداخت شما اضافه شد", Toast.LENGTH_SHORT).show();
                Toast.makeText(this.myContext, Global_var.num_array_gkol_id_ozviat + "عضو در سبد دارید!", Toast.LENGTH_SHORT).show();
                //   }
                Global_var.sum_ozv_sabadpar += 1;


                holder.chkSelected.setVisibility(View.INVISIBLE);
                holder.img_add_tolist_pard.setVisibility(View.INVISIBLE);
                notifyDataSetChanged();
            }else
            {
                Toast.makeText(this.myContext,  " عضو گرامی شما فقط میتوانید ده نفر را در سبد پرداخت داشته باشید!", Toast.LENGTH_SHORT).show();

            }
            //    holder.nfamil.setVisibility(View.GONE);
         //  Toast.makeText(this.myContext,myVamList_other.get(pos).getLastname_ozv()+" به سبد پرداخت شما اضافه شد",Toast.LENGTH_SHORT).show();
         //   Toast.makeText(this.myContext, Global_var.num_array_gkol_id_ozviat+1 + "عضو در سبد دارید!",Toast.LENGTH_SHORT).show();

            //  Global_var.sabad_pardakht.add(Global_var.sum_ozv_sabadpar,add_karbar);

//            Global_var.sabad_pardakht.add(new md_kol_aza(
//                    add_karbar.getName_ozv(),
//                    add_karbar.getLastname_ozv(),
//                    add_karbar.getId_ozviat(),
//                    add_karbar.getMojodi_ozv(),add_karbar.getVazeyat_ozv(),
//                    add_karbar.getTarikh_ozviat(),add_karbar.isSelected()));
//                // Toast.makeText(getApplicationContext(), one_karbar.getId_ozviat(), Toast.LENGTH_LONG).show();

                //  s+=vam_karbars[i].toString();

        });

        // end add checked to list pardakhti
    }
    @Override
    public int getItemCount() {
        return myVamList_other.size();
    }

    // ViewHoldr Class



    public Filter getFilter() {
        if(filter==null)
        {
            filter=new CustomFilter_kolAza(filterList,this);
        }

        return filter;
    }
}

