package ir.almahdi.sandogh_bsa_aza.display_sabad_ozv;

import android.annotation.SuppressLint;
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.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;
import ir.almahdi.sandogh_bsa_aza.display_sabad_ozv.model.md_vam_pas_ozv;

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

    private Context myContext;
    private List<md_vam_pas_ozv> myVamList;
    private static  int currentposition=0,num_expand=0;;

    test_adapter(Context context, List<md_vam_pas_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.crd_md_vam_pas_ozv, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
        md_vam_pas_ozv vam_pas_ozv = myVamList.get(position);
      //  holder.setvamInfo(Entiremd_vam_karbar);
       // holder.id_ozviat.setText();=vam_pas_ozv.getId_ozviat();
        if(vam_pas_ozv.getId_par_vam_nbt() == null){
         holder.id_vam.setText("0");
         holder. tedad_aghsat.setText("0");
          holder.  mab_vam.setText("-");
           holder. mande_vam.setText("-");
          holder. mab_gh_vam.setText("-");
           holder. tarikh_dar_vam.setText("0");
          //holder.nfamil.setText(vam_pas_ozv.getLastname_ozv());
            holder.mojodi_ozv.setText(String.valueOf(vam_pas_ozv.getMojodi_ozv()) );
            holder. nfamil.setText(String.format("%s %s", vam_pas_ozv.getName_ozv(), vam_pas_ozv.getLastname_ozv()));
         holder.id_ozviat.setText(vam_pas_ozv.getId_ozviat());


        }else{
            holder.mojodi_ozv.setText(String.valueOf((int) vam_pas_ozv.getMojodi_ozv()));
//            holder.tedad_aghsat.setText(String.valueOf(vam_pas_ozv.getBazpard_vam_mah()));
//            holder. mab_vam.setText(String.valueOf((int) vam_pas_ozv.getMablagh_vam_par()));

            holder.id_vam.setText(vam_pas_ozv.getId_par_vam_nbt());
            holder.tedad_aghsat.setText(String.valueOf(vam_pas_ozv.getBazpard_vam_mah()));
            holder. mab_vam.setText(String.valueOf((int) vam_pas_ozv.getMablagh_vam_par()));
            // فعلا کوئری اقساط پرداختی ومانده وام را ننوشتیم
            holder.mande_vam.setText(String.valueOf((int) vam_pas_ozv.getMande_vam()));
            holder.mab_gh_vam.setText(String.valueOf((int) vam_pas_ozv.getMablagh_gh_vam_nob()));
            holder.tarikh_dar_vam.setText(vam_pas_ozv.getTarikh_par_vam());
            //String name=vam_pas_ozv.getName_ozv();
            // String fam=;
            holder.mojodi_ozv.setText(String.valueOf(vam_pas_ozv.getMojodi_ozv()) );
            holder. nfamil.setText(String.format("%s %s", vam_pas_ozv.getName_ozv(), vam_pas_ozv.getLastname_ozv()));
            // nfamil.setText(vam_pas_ozv.getName_ozv()+" "+vam_pas_ozv.getLastname_ozv());
            holder.id_ozviat.setText(vam_pas_ozv.getId_ozviat());

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

        }
//        holder. img_del_aslist_pard2.setOnClickListener(new View.OnClickListener() {
//           @Override
//           public void onClick(View v) {
//               if (myVamList.size()>1){
//                   myVamList.remove(position);
//                   notifyItemRemoved(position);
//                   Global_var.gkol_id_ozviat[position]="empty";
//               }
//                else if (myVamList.size()==1){
//
//                   Toast.makeText(v.getContext(),"لیست پرداخت باید حداقل یک عضو داشته باشد!",Toast.LENGTH_LONG).show();
//               }
//
//         //   notifyDataSetChanged();
//           }
//        });
        holder.crdview_sabad_karbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (num_expand==1){
                    holder.linearLayout_vam_pas.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 tarikh_dar_vam,nfamil,id_ozviat,mojodi_ozv,vazeyat_ozv,
                tarikh_ozviat,id_vam,mande_vam,mab_vam,tedad_aghsat
                ,tedad_gest_par,mab_gh_vam;

CardView crdview_sabad_karbar;
LinearLayout linearLayout_vam_pas;
        private Boolean checkbox;
        private long mablagh_gh_vam_nob;
        ImageView img_del_aslist_pard2;

        MyViewHolder(View itemView) {
            super(itemView);
            nfamil =itemView.findViewById(R.id.txt_nfamil_vam_pas);
            id_ozviat = itemView.findViewById(R.id.idozviat_vam_pas);
           mojodi_ozv= itemView.findViewById(R.id.sum_pas_vam_vam_pas);
//           vazeyat_ozv= itemView.findViewById(R.id.idozviat);
//           tarikh_ozviat= itemView.findViewById(R.id.idozviat);

            id_vam = itemView.findViewById(R.id.idvam_vam_pas);
            tedad_aghsat = itemView.findViewById(R.id.tedad_aghsat_vam_pas);
            mab_vam = itemView.findViewById(R.id.mablagh_vam_vam_pas);
            mande_vam = itemView.findViewById(R.id.txtmande_vam_vam_pas);
           // tedad_gest_par = itemView.findViewById(R.id.txt_tedad_gest_par);
            tarikh_dar_vam = itemView.findViewById(R.id.txt_tarikh_vam_vam_pas);
            mab_gh_vam=itemView.findViewById(R.id.txt_mab_gest_vam_pas);
            /////cardview
            crdview_sabad_karbar=itemView.findViewById(R.id.crdview_sabad_karbar);
            linearLayout_vam_pas=itemView.findViewById(R.id.linerlayout_vam_pas);
            img_del_aslist_pard2=itemView.findViewById(R.id.img_del_aslist_pard2);
        }

        @SuppressLint("SetTextI18n")
        public void setvamInfo(md_vam_pas_ozv vam_pas_ozv) {
            if(vam_pas_ozv.getId_par_vam_nbt()==null  ){
                id_vam.setText("0");
                tedad_aghsat.setText("0");
                mab_vam.setText("0");
                mande_vam.setText("0");
                mab_gh_vam.setText("0");
                tarikh_dar_vam.setText("0");
                nfamil.setText(vam_pas_ozv.getLastname_ozv());
                // nfamil.setText(vam_pas_ozv.getName_ozv()+" "+vam_pas_ozv.getLastname_ozv());
                id_ozviat.setText(vam_pas_ozv.getId_ozviat());

            }else{
                id_vam.setText(vam_pas_ozv.getId_par_vam_nbt());
                tedad_aghsat.setText(String.valueOf(vam_pas_ozv.getBazpard_vam_mah()));
                mab_vam.setText(String.valueOf((int) vam_pas_ozv.getMablagh_vam_par()));
                // فعلا کوئری اقساط پرداختی ومانده وام را ننوشتیم
                mande_vam.setText(String.valueOf((int) vam_pas_ozv.getMande_vam()));
                mab_gh_vam.setText(String.valueOf((int) vam_pas_ozv.getMablagh_gh_vam_nob()));
                tarikh_dar_vam.setText(vam_pas_ozv.getTarikh_par_vam());
                //String name=vam_pas_ozv.getName_ozv();
                // String fam=;
                nfamil.setText(vam_pas_ozv.getLastname_ozv());
                // nfamil.setText(vam_pas_ozv.getName_ozv()+" "+vam_pas_ozv.getLastname_ozv());
                id_ozviat.setText(vam_pas_ozv.getId_ozviat());

            }



        }
    }
}
///////////////////////*******************/////////////////////
//package com.example.novindevelopers.recyclerview_project;
//
//        import android.content.Context;
//        import android.support.v7.widget.CardView;
//        import android.support.v7.widget.RecyclerView;
//        import android.view.LayoutInflater;
//        import android.view.View;
//        import android.view.ViewGroup;
//        import android.widget.ImageView;
//        import android.widget.TextView;
//        import android.widget.Toast;
//
//        import java.util.List;
//
///**
// * Created by NovinDevelopers on 1/26/2018.
// */
//
//public class AdapterRecy extends RecyclerView.Adapter<AdapterRecy.ViewHolder>{
//
//    List<ModelItems> items;
//    Context context;
//
//    public AdapterRecy(List<ModelItems> items, Context context) {
//        this.items = items;
//        this.context = context;
//    }
//
//    @Override
//    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recy,parent,false);
//
//        return new ViewHolder(view);
//    }
//
//    @Override
//    public void onBindViewHolder(final ViewHolder holder, final int position) {
//
//        ModelItems modelItems = items.get(position);
//        holder.imageViewShow.setImageResource(modelItems.getImage());
//        holder.textViewName.setText(modelItems.getName());
//
//        holder.cardView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(context, holder.textViewName.getText().toString(), Toast.LENGTH_SHORT).show();
//            }
//        });
//        holder.imageViewRemove.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                items.remove(position);
//                notifyItemRemoved(position);
//                notifyItemRangeRemoved(position,items.size());
//            }
//        });
//    }
//
//    @Override
//    public int getItemCount() {
//        return items.size();
//    }
//
//    public class ViewHolder extends RecyclerView.ViewHolder {
//        ImageView imageViewShow,imageViewRemove;
//        CardView cardView;
//        TextView textViewName;
//        public ViewHolder(View itemView) {
//            super(itemView);
//
//            imageViewShow = (ImageView)itemView.findViewById(R.id.imageShow);
//            imageViewRemove = (ImageView)itemView.findViewById(R.id.imageRemove);
//            textViewName = (TextView)itemView.findViewById(R.id.textName);
//            cardView = (CardView)itemView.findViewById(R.id.cardView);
//
//
//        }
//    }
//}
//
