package ir.almahdi.sandogh_bsa_aza.Add_OtherOzv_pay;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;

import ir.almahdi.sandogh_bsa_aza.Add_OtherOzv_pay.model.md_kol_aza;
import ir.almahdi.sandogh_bsa_aza.Class.MySingleton;
import ir.almahdi.sandogh_bsa_aza.Global_var;
import ir.almahdi.sandogh_bsa_aza.R;
import ir.almahdi.sandogh_bsa_aza.display_sabad_ozv.Display_sabad_ozv;
import ir.almahdi.sandogh_bsa_aza.karbar_activity;

//import ir.almahdi.sandogh_bsa_aza.display_sabad_ozv.Mos_vam_pas_ozv_adapter;

public class Add_OtherOzv_payment extends AppCompatActivity {
ImageView img_hadith,img_add_tolist_pard;
    TextView txt_idozvia_in_kolaza;
    Button btn_goto_recy_details_vam,btn_list_aza_pay;
    //    private FrameLayout frameLayout;
    public RecyclerView recyclerView;
    public RelativeLayout layout;
    private SearchView sv;
    ProgressDialog progressDialog;
    LinearLayoutManager linear;
    TableLayout tablayout_mosh_ozv;
    final  ArrayList<md_kol_aza> myVamList = new ArrayList<>();
    KolAza_adapter adapter ;

    //Mos_vam_pas_ozv_adapter adapter_mos_vam_pas;
    public Add_OtherOzv_payment() {
    }

    @SuppressLint({"WrongViewCast", "SetTextI18n", "ResourceAsColor"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_other_ozv_payment);
        //**************
        img_hadith=findViewById(R.id.img_hadith);
        Glide
                .with(this)
                .load(R.raw.hadith)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(img_hadith);
    //***************
      //  adapter_mos_vam_pas = new Mos_vam_pas_ozv_adapter(getApplicationContext(), myVamList);
        //a list to store all the products
        btn_goto_recy_details_vam=findViewById(R.id.btn_goto_recy_details_vam);
        //set recyclerview to Layout manager
        // Toast.makeText(getContext(),"fargment vam hastim_oncreateview",Toast.LENGTH_LONG).show();
        adapter= new KolAza_adapter(getApplicationContext(), myVamList);
        recyclerView = findViewById(R.id.recylcerview_add_karbar);//set the Layout manager
        linear = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linear);
        recyclerView.setAdapter(adapter);
        //set the Layout manager
        sv=findViewById(R.id.srch_kol_aza);
        txt_idozvia_in_kolaza=(TextView) findViewById(R.id.txt_idozvia_in_kolaza);
        btn_list_aza_pay=findViewById(R.id.btn_list_aza_pay);
        //
        img_add_tolist_pard =findViewById(R.id.img_add_tolist_pard);
//        String txtidozv="شماره عضویت :";
//       txt_idozvia_in_kolaza.setText(txtidozv);
        if (Global_var.sum_ozv_sabadpar==0){get_Mosh_kol_aza();}
        else
            {myVamList.clear();}

        tablayout_mosh_ozv= findViewById(R.id.tablayout_mosh_ozv);
        //بازگشت به صفحه صورت وضهیت کاربر -صفحه اصلی
        Button btn_dis_mosh_karbar=findViewById(R.id.btn_dis_mosh_karbar);
        btn_dis_mosh_karbar.setOnClickListener(v -> {
            Global_var.sum_ozv_sabadpar=0;
            myVamList.clear();
            Global_var.numLoad_Vamozv=0;
            for (byte i=0;i<10;i++){
                Global_var.gkol_id_ozviat[i]="empty";
            }
            Global_var.num_array_gkol_id_ozviat=0;
            Global_var.sum_vams_list_select_ozv= 0L;
            Global_var.sum_pas_list_select_ozv=0L;
            Global_var.sum_pas_vam_list_select_ozv=0L;
            isDestroyed();
            finish();
            startActivity(new Intent(getApplicationContext(), karbar_activity.class));


        });// پایان انتقال به صفحه اصلی
    }
    // غیرفعال کردن دکمه بک
    @Override
    public void onBackPressed() {
    }

    public void get_Mosh_kol_aza() {
//        final String url = "http://sandogh-bs.makkh.ir/vam_karbar/read_vam_ozv.php";

        final String url_kol_aza = "http://sandogh-bs.makkh.ir/md_kol_aza/read_mos_kol_aza_tbaza.php";
        progressDialog = new ProgressDialog(this);
        progressDialog.show();

        // RecyclerView recyclerView;
//            List<md_kol_aza> myVamList=new List<md_kol_aza>();
        Response.Listener<String> listener = new Response.Listener<String>() {

            @SuppressLint("Assert")
            @Override
            public void onResponse(String response) {
                //ArrayList<md_kol_aza> myVamList = new ArrayList<>();// Toast.makeText(getApplicationContext(), response+"111", Toast.LENGTH_LONG).show()
                if (Global_var.numLoad_Vamozv==0){
                    Gson gson = new Gson();
                    md_kol_aza[] kol_karbars = gson.fromJson(response, md_kol_aza[].class);

                    for (md_kol_aza one_karbar : kol_karbars) {
                        myVamList.add(new md_kol_aza(
                                one_karbar.getName_ozv(),
                                one_karbar.getLastname_ozv(),
                                one_karbar.getId_ozviat(),
                                one_karbar.getMojodi_ozv(), one_karbar.getVazeyat_ozv(),
                                one_karbar.getTarikh_ozviat(), one_karbar.isSelected()));
                        // Toast.makeText(getApplicationContext(), one_karbar.getId_ozviat(), Toast.LENGTH_LONG).show();

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

                    progressDialog.dismiss();
                    // Toast.makeText(getApplicationContext(), kol_karbars[5].getId_ozviat().toString(), Toast.LENGTH_LONG).show();
                    //  Toast.makeText(getContext(), myVamList.toString(), Toast.LENGTH_LONG).show();

                    //set the Layout manager
                    //setLayoutManger(new LinearLayoutManager(getActivity()));
                    //ArrayList<md_kol_aza> filterList = null;

                    recyclerView.setHasFixedSize(true);
                    recyclerView.setAdapter(adapter);
                    Global_var.numLoad_Vamozv+=1;
                }
                //SEARCH
                sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        return false;
                    }

                    @Override
                    public boolean onQueryTextChange(String query) {
                        //FILTER AS YOU TYPE
                        adapter.getFilter().filter(query);
                        return false;
                    }
                });
                //اضافه کردن به لیست کاربران;
                btn_goto_recy_details_vam.setOnClickListener(v -> {
                    if (Global_var.num_array_gkol_id_ozviat==0){
                        Toast.makeText(Add_OtherOzv_payment.this,"لطفا عضوی را از لیست بالا انتخاب نمایید!",Toast.LENGTH_LONG).show();
                    }else{
                        startActivity(new Intent(getApplicationContext(), Display_sabad_ozv.class));
                    }
                });
            }

        };
        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }
        };
        StringRequest request = new StringRequest(Request.Method.POST, url_kol_aza, listener, errorListener) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> map = new Hashtable<>();
                map.put("kolaza","all");
                map.put("sal_mali", Global_var.gsal_mali);

                return map;
            }
        };
//       Toast.makeText(getContext(), myVamList.get(1).getMablagh_gh_vam_nob(), Toast.LENGTH_SHORT).show();
        //adding our stringrequest to queue
        MySingleton.getInstance(getApplicationContext()).addToRequestQueue(request);
        //
    }
//       btn_list_aza_pay.setOnClickListener(v -> {
//           if (Global_var.sum_ozv_sabadpar > 0) {
//               get_mosh_vam_hozv_ozv(Global_var.gid_ozviat, (byte) -10);
//               for (byte i=0;i<=Global_var.gkol_id_ozviat.size();i++){
//               //   i<=Global_var.sum_ozv_sabadpar
//                   Toast.makeText(getApplicationContext(),String.valueOf(Global_var.gkol_id_ozviat.get(i)),Toast.LENGTH_LONG).show();
//                  get_mosh_vam_hozv_ozv(String.valueOf(Global_var.gkol_id_ozviat.get(i)),i);
//               }
//          //     recylcerview_res_add_otherozv.setHasFixedSize(true);
////               recylcerview_res_add_otherozv.setHasFixedSize(true);
////               recylcerview_res_add_otherozv.setAdapter(adapter_mos);
//                adapter_mos_vam_pas.notifyDataSetChanged();         //  recylcerview_res_add_otherozv.setAdapter(adapter_mos_vam_pas);
//////                String[] adapter2 = (getApplicationContext(), Global_var.gkol_id_ozviat);
//////                recylcerview_res_add_otherozv.setHasFixedSize(true);
//////                recylcerview_res_add_otherozv.setAdapter(adapter2);
////
//         }
//        });
//    public void get_mosh_vam_hozv_ozv(String id_ozviat,byte sum_ozv_sel_karbar) {
//
//
//        final String url_Mosh_vam_pas_ozv = "http://sandogh-bs.makkh.ir/md_kol_aza/read_kolmosh_ozv_sabad.php";
//            progressDialog = new ProgressDialog(this);
//            progressDialog.show();
//
//            // RecyclerView recyclerView;
////            List<md_kol_aza> myVamList=new List<md_kol_aza>();
//            Response.Listener<String> listener = new Response.Listener<String>() {
//
//                @Override
//                public void onResponse(String response) {
//                    // if (Global_var.numLoad_Vamozv==0){
//                    ArrayList<md_vam_pas_ozv> md_vam_pas_ozv = new ArrayList<>();// Toast.makeText(getApplicationContext(), response+"111", Toast.LENGTH_LONG).show()
//                    Mos_vam_pas_ozv_adapter adapter_mos = new Mos_vam_pas_ozv_adapter(getApplicationContext(), md_vam_pas_ozv);
//
//                    Gson gson = new Gson();
//                        md_vam_pas_ozv[] mos_karbar = gson.fromJson(response, md_vam_pas_ozv[].class);
//                         Toast.makeText(getApplicationContext(), response+"111", Toast.LENGTH_LONG).show();
//                         for (md_vam_pas_ozv one_karbar : mos_karbar) {
//                            myVamList.add(new md_vam_pas_ozv(
//                                    one_karbar.getId_ozviat(),one_karbar.getName_ozv(),one_karbar.getLastname_ozv(),
//                                    one_karbar.getMojodi_ozv(), one_karbar.getVazeyat_ozv(),
//                                    one_karbar.getTarikh_ozviat(),one_karbar.getId_par_vam_nbt(),
//                                    one_karbar.getMande_vam(),one_karbar.getMablagh_vam_par(),one_karbar.getBazpard_vam_mah(),
//                                    one_karbar.getTarikh_par_vam(),one_karbar.isSelected(),one_karbar.getMablagh_gh_vam_nob()));
//                            // Toast.makeText(getApplicationContext(), one_karbar.getId_ozviat(), Toast.LENGTH_LONG).show();
//
//                            //  s+=vam_karbars[i].toString();
//                        }
//
//                        progressDialog.dismiss();
//                        // Toast.makeText(getApplicationContext(), kol_karbars[5].getId_ozviat().toString(), Toast.LENGTH_LONG).show();
//                        //  Toast.makeText(getContext(), myVamList.toString(), Toast.LENGTH_LONG).show();
//
//
//                    recylcerview_res_add_otherozv.setHasFixedSize(true);
//                    recylcerview_res_add_otherozv.setAdapter(adapter_mos);
//
////                        Global_var.numLoad_Vamozv+=1;
//                  //  }
//
//                }
//
//            };
//            Response.ErrorListener errorListener = new Response.ErrorListener() {
//                @Override
//                public void onErrorResponse(VolleyError error) {
//                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
//                    progressDialog.dismiss();
//                }
//            };
//            StringRequest request = new StringRequest(Request.Method.POST, url_Mosh_vam_pas_ozv, listener, errorListener) {
//                @Override
//                protected Map<String, String> getParams() {
//                    Map<String, String> map = new Hashtable<>();
//                    map.put("id_ozviat",id_ozviat);
//                  //  map.put("sal_mali", Global_var.gsal_mali);
//
//                    return map;
//                }
//            };
////       Toast.makeText(getContext(), myVamList.get(1).getMablagh_gh_vam_nob(), Toast.LENGTH_SHORT).show();
//            //adding our stringrequest to queue
//            MySingleton.getInstance(getApplicationContext()).addToRequestQueue(request);
//
//        }



}
