package ir.almahdi.sandogh_bsa_aza.vam_other_ozv;

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.LinearLayout;
import android.widget.TextView;

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

import java.util.List;

import ir.almahdi.sandogh_bsa_aza.R;
import ir.almahdi.sandogh_bsa_aza.vam_other_ozv.model.md_vam_other;

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

    private Context myContext;
private static  int currentposition=0,num_expand=0;;


    public List<md_vam_other> myVamList_other,filterList;
    private CustomFilter filter;

    public List<md_vam_other> getvam_Other_list (){
        return myVamList_other;
    }
    public vam_Other_adapter(Context context, List<md_vam_other> myVamList_other) {
        this.myContext =  context;
        this.myVamList_other = myVamList_other;
        this.filterList = myVamList_other;
    }



    @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);
    }

    @Override
    public void onBindViewHolder(vam_Other_adapter.MyViewHolder holder, int position) {
        final int pos = position;

        md_vam_other entiremd_vam_other = myVamList_other.get(position);
        holder.setvamInfo(entiremd_vam_other);
      //  holder.chkSelected.setChecked(myVamList_other.get(position).isSelected());

        holder.chkSelected.setTag(myVamList_other.get(position));

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

                    }

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

    }
});
        // end expand
        // checbox select
        holder.chkSelected.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                md_vam_other vam_other = (md_vam_other) cb.getTag();
              vam_other.setSelected(cb.isChecked());
               myVamList_other.get(pos).setSelected(cb.isChecked());


            }
            });
        // checbox select
    }
    @Override
    public int getItemCount() {
        return myVamList_other.size();
    }

    public void notifyItemRemoved(md_vam_other md_vam_other) {

            notifyItemRemoved(currentposition);
    }

    // ViewHoldr Class

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tarikh_dar_vam,id_vam,nfamil ,
           mab_vam,tedad_aghsat,mande_vam,tedad_gest_par,mab_gh_vam;
        LinearLayout linerlayout_Other;
     //   LinearLayout.LayoutParams dimention;
        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_vam = itemView.findViewById(R.id.idvam);
            tedad_aghsat = itemView.findViewById(R.id.tedad_aghsat);
            mab_vam = itemView.findViewById(R.id.mablagh_vam);
            mande_vam = itemView.findViewById(R.id.txtmande_vam);
           // tedad_gest_par = itemView.findViewById(R.id.txt_tedad_gest_par);
            tarikh_dar_vam = itemView.findViewById(R.id.txt_tarikh_vam);
            mab_gh_vam=itemView.findViewById(R.id.txt_mab_gest);
            chkSelected=itemView.findViewById(R.id.chkSelected);


        }

        @SuppressLint("SetTextI18n")
        void setvamInfo(md_vam_other vam_karbar) {
            nfamil.setText(vam_karbar.getName_ozv()+" "+vam_karbar.getLastname_ozv());
            id_vam.setText(vam_karbar.getId_par_vam_nbt());
            tedad_aghsat.setText(vam_karbar.getBazpard_vam_mah().toString());
            mab_vam.setText(vam_karbar.getMablagh_vam_par().toString());
            // فعلا کوئری اقساط پرداختی ومانده وام را ننوشتیم
            mande_vam.setText(vam_karbar.getMande_vam().toString());
            mab_gh_vam.setText(vam_karbar.getMablagh_gh_vam_nob().toString());
            tarikh_dar_vam.setText(vam_karbar.getTarikh_par_vam());



        }
    }

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

        return filter;
    }
}

