package ir.almahdi.sandogh_bsa_aza.display_sabad_ozv;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
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.google.gson.Gson;

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

import ir.almahdi.sandogh_bsa_aza.Add_OtherOzv_pay.Add_OtherOzv_payment;
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.model.md_vam_pas_ozv;
import ir.almahdi.sandogh_bsa_aza.idpay.Main2Activity;

public class Display_sabad_ozv extends AppCompatActivity {
    ProgressDialog progressDialog;
    final  ArrayList<md_vam_pas_ozv> myVamList = new ArrayList<>();
    public RecyclerView rc_res_add_otherozv;
    test_adapter adapter_mos;
    LinearLayoutManager linear;
    Button  btn_vam_pas_sabad_mnext,btn_vam_pas_sabad_mpre;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_sabad_ozv);
        Global_var.sabad_pardakht=new ArrayList<>();
        rc_res_add_otherozv = findViewById(R.id.rc_res_add_otherozv);//set the Layout manager
        adapter_mos = new test_adapter(Display_sabad_ozv.this, Global_var.sabad_pardakht);
        rc_res_add_otherozv.setLayoutManager(new LinearLayoutManager(Display_sabad_ozv.this));
        rc_res_add_otherozv.setHasFixedSize(true);
        rc_res_add_otherozv.setAdapter(adapter_mos);

        //
        btn_vam_pas_sabad_mnext = findViewById(R.id.btn_vam_pas_sabad_mnext);
        btn_vam_pas_sabad_mpre = findViewById(R.id.btn_vam_pas_sabad_mpre);
       // Toast.makeText(getApplicationContext(), String.valueOf(Global_var.sum_ozv_sabadpar).trim() + "تعداد اعضائ موجود در سبد :", Toast.LENGTH_LONG).show();
       // لیست انتخابی کاربر را برای اضافه کردن به تابع دریافت اطلاعاتشون فراخوانی میکند
        if (Global_var.sum_ozv_sabadpar > 0) {
            for (byte i = 0; i <= Global_var.num_array_gkol_id_ozviat-1; i++) {
                if (!Global_var.gkol_id_ozviat[i].equals("empty")){

                    get_mosh_vam_hozv_ozv(String.valueOf(Global_var.gkol_id_ozviat[i]).trim());

                }
                else {i+=1;}
                }

            }

//        else{
//          //  Toast.makeText(getApplicationContext(), "شماره عضویت شما:"+Global_var.gid_ozviat.trim().trim(), Toast.LENGTH_LONG).show();
////            get_mosh_vam_hozv_ozv(Global_var.gid_ozviat.trim());
//            get_mosh_vam_hozv_ozv(String.valueOf(Global_var.gid_ozviat).trim());
//
//        }
        //رفتن به مرحله مشاهده مبلغ سبد پرداخت
        btn_vam_pas_sabad_mnext.setOnClickListener(v ->{
            byte index_idozv=0,sum_idozviat_sabad_ozv=0;
            while(!Global_var.gkol_id_ozviat[index_idozv].equals("empty")) {

                for (byte i = 0; i < Global_var.sabad_pardakht.size(); i++) {
                    if (Global_var.sabad_pardakht.get(i).getId_ozviat().equals(Global_var.gkol_id_ozviat[index_idozv])) {
                        Global_var.sum_vams_list_select_ozv += Global_var.sabad_pardakht.get(i).getMablagh_gh_vam_nob();
                        Toast.makeText(getApplicationContext(), "[جمع وام ها]:" + Global_var.sum_vams_list_select_ozv.toString(), Toast.LENGTH_LONG).show();
                    }
                    //                System.out.print(i);
//                System.out.print("\n");
                }
                index_idozv += 1;
                sum_idozviat_sabad_ozv += 1;
            }
            Global_var.sum_pas_list_select_ozv=Global_var.gmab_hagh_ozv * sum_idozviat_sabad_ozv ;
            Toast.makeText(getApplicationContext(), "[جمع پسانداز]:"+ Global_var.sum_pas_list_select_ozv.toString(), Toast.LENGTH_LONG).show();
            Global_var.sum_pas_vam_list_select_ozv=Global_var.sum_pas_list_select_ozv+Global_var.sum_vams_list_select_ozv;
            startActivity(new Intent(getApplicationContext(), Main2Activity.class));

        });
        btn_vam_pas_sabad_mpre.setOnClickListener(v ->{
        rc_res_add_otherozv.removeAllViewsInLayout();
            Global_var.sum_ozv_sabadpar=0;
            Global_var.sabad_pardakht.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;
            startActivity(new Intent(getApplicationContext(), Add_OtherOzv_payment.class));
        });

    }
    // غیرفعال کردن دکمه بک
    @Override
    public void onBackPressed() {
    }



   public void get_mosh_vam_hozv_ozv(final String id_ozviat) {
        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();
       // Toast.makeText(getApplicationContext(), id_ozviat+"ورودب به تابع ", Toast.LENGTH_LONG).show();


        Response.Listener<String> listener = new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                //  daryaft etelaat
               // Toast.makeText(getApplicationContext(), "دریافت ازسرور"+response, Toast.LENGTH_LONG).show();

               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) {
                    Global_var.sabad_pardakht.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(), Global_var.gkol_id_ozviat.get(i[0]), Toast.LENGTH_LONG).show();
//                //  Toast.makeText(getContext(), Global_var.sabad_pardakht.toString(), Toast.LENGTH_LONG).show();
//                rc_res_add_otherozv.setHasFixedSize(true);
                adapter_mos.notifyDataSetChanged();

                   //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(getApplicationContext(), id_ozviat, Toast.LENGTH_SHORT).show();
        //adding our stringrequest to queue
       // Toast.makeText(getApplicationContext(), String.valueOf(Global_var.gkol_id_ozviat.get(finalI)), Toast.LENGTH_SHORT).show();

        MySingleton.getInstance(getApplicationContext()).addToRequestQueue(request);

        //   i<=Global_var.sum_ozv_sabadpar






        // RecyclerView recyclerView;
//            List<md_kol_aza> Global_var.sabad_pardakht=new List<md_kol_aza>();

    }




}
