package ir.almahdi.sandogh_bsa_aza.vam_other_ozv;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.AuthFailureError;
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.List;
import java.util.Map;

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.vam_other_ozv.model.md_vam_other;


/**
 * A simple {@link Fragment} subclass.
 */
public class vam_other_frg extends Fragment {
    TextView tarikhvam,idvam,mablaghvam,tedad_aghsat,bazpard_vam_mah;
    Button img_sabad_pard;
//    private FrameLayout frameLayout;
    private RecyclerView recyclerView;
    public RelativeLayout layout;
    private SearchView sv;
    private List <md_vam_other> myVamList = new ArrayList<>();
    private ImageView img_pay;
    private ImageView imgSelection;
    public vam_other_frg() {
    }
    //the recyclerview

//    private String mande_vam;
//    private String tedad_gest_par;
    //md_otheraza vam_karbar=new md_otheraza(tarikhvam,idvam,mablaghvam,tedad_aghsat, mande_vam, tedad_gest_par);

    @SuppressLint("ResourceType")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.frg_vam_other, container, false);
        init(view);
        //a list to store all the products
        img_sabad_pard=view.findViewById(R.id.img_pard_ghest_other);

       // Toast.makeText(getContext(),"fargment vam hastim_oncreateview",Toast.LENGTH_LONG).show();
        recyclerView =  view.findViewById(R.id.recylcerview_other_karbar);
        //set the Layout manager

        mablaghvam=view.findViewById(R.id.txt_mab_gest);
//      Global_var.mablagh_pard_ozv=  Long.valueOf(mablaghvam.getText().toString());
//      Toast.makeText(getContext(),Global_var.mablagh_pard_ozv.toString(),Toast.LENGTH_SHORT).show();
// display vam digaran in recyclerview
//        Objects.requireNonNull(getActivity()).findViewById(R.id.img_exit).setVisibility(View.INVISIBLE);
    LinearLayoutManager linear = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(linear);
        get_vam_other_karbar( view);

         sv = view.findViewById(R.id.srch_vam_other);
         sv.setFocusable(true);
 return view; }

    private void init(View view) {
        recyclerView =  view.findViewById(R.id.recylcerview_other_karbar);
        layout = view.findViewById(R.id.frmlayout_vam);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

//        if (img_pay.isSelected()) {
//            // img_pay.callOnClick();
//            imp_par_zarin(view);
//        }
        }
    public void get_vam_other_karbar(View view) {
//        final String url = "http://sandogh-bs.makkh.ir/vam_karbar/read_vam_ozv.php";

        final String url_vam_jari = "http://sandogh-bs.makkh.ir/vam_karbar/read_vam_ozv_other.php";
        final ProgressDialog progressDialog = new ProgressDialog(view.getContext());
        progressDialog.show();

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

            @Override
            public void onResponse(String response) {

                    //Toast.makeText(view.getContext(), response, Toast.LENGTH_LONG).show();
                Gson gson=new Gson();
                md_vam_other[] vam_karbars=gson.fromJson(response, md_vam_other[].class);
                ArrayList<md_vam_other> myVamList = new ArrayList<>();
                   for (md_vam_other vam_karbar : vam_karbars) {
                       myVamList.add(new md_vam_other(
                               vam_karbar.getName_ozv(),
                               vam_karbar.getLastname_ozv(),
                               vam_karbar.getId_par_vam_nbt(),
                            vam_karbar.getMande_vam(),
                            vam_karbar.getMablagh_vam_par(),
                            vam_karbar.getBazpard_vam_mah(),
                            vam_karbar.getTarikh_par_vam(),
                            vam_karbar.getMablagh_gh_vam_nob(),
                            vam_karbar.getMande_vam(),vam_karbar.isSelected()));
                       Global_var.mablagh_pard_ozv =Global_var.mablagh_pard_ozv + vam_karbar.getMablagh_gh_vam_nob();


                       //  s+=vam_karbars[i].toString();
                }
                progressDialog.dismiss();
               Toast.makeText(view.getContext(), vam_karbars[0].getMande_vam().toString(), Toast.LENGTH_LONG).show();
              //  Toast.makeText(getContext(), myVamList.toString(), Toast.LENGTH_LONG).show();

                //set the Layout manager
                //setLayoutManger(new LinearLayoutManager(getActivity()));
                //ArrayList<md_otheraza> filterList = null;
                vam_Other_adapter adapter = new vam_Other_adapter(view.getContext(), myVamList);
                recyclerView.setHasFixedSize(true);
                recyclerView.setAdapter(adapter);
                //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;
                    }
                });
                //adapter.notifyDataSetChanged();
                img_sabad_pard.setOnClickListener(new View.OnClickListener() {
                    @SuppressLint("Assert")
                    @Override
                    public void onClick(View v) {
//                        List<md_otheraza> myVamList_other = null;
//                        OtherAza_select_adapter adapter = new OtherAza_select_adapter(view.getContext(), myVamList_other);
//                        myVamList_other=adapter.getvam_Other_list();
                          ArrayList <md_vam_other> num_select_aza = null;
                        for (int i = 0; i < myVamList.size(); i++) {
                           md_vam_other singleselkarbar  = myVamList.get(i);

                            if (singleselkarbar.isSelected() ) {
                                //     assert false;
                                //     num_select_aza.add(myVamList.get(i));
                                //    data = data + "\n" + singleselkarbar.getName().toString();
                                singleselkarbar.setId_parchk_san("-100-");
                                // حذف از لیست
//                                myVamList.remove(myVamList.get(i));
//                                adapter.notifyItemRemoved(i);
                                Global_var.sum_vam_other = Global_var.sum_vam_other + Global_var.gmab_hagh_ozv + singleselkarbar.getMablagh_gh_vam_nob();
                                Toast.makeText(getContext(), Global_var.sum_vam_other + "جمع مبالغ پرداختی:", Toast.LENGTH_LONG).show();

                            }
                           else {
                                assert false;
                               if (singleselkarbar.getId_parchk_san().equals("-100-")) {
                                           Global_var.sum_vam_other = Global_var.sum_vam_other - Global_var.gmab_hagh_ozv - singleselkarbar.getMablagh_gh_vam_nob();
                                            Toast.makeText(getContext(), Global_var.sum_vam_other + "جمع مبالغ پرداختی:", Toast.LENGTH_LONG).show();
                                      }
                                  }
                        }

                        //startActivity(new Intent(getApplicationContext(),zarinpal.class));



                    }
                });
            }
        };
        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(view.getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }
        };
        StringRequest request = new StringRequest(Request.Method.POST, url_vam_jari, listener, errorListener) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new Hashtable<>();
                map.put("kol_vam_jari", "1");
                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(view.getContext()).addToRequestQueue(request);

    }

    @SuppressLint("WrongConstant")
    public void img_sabad_pard(View view) {


//       List <md_otheraza> myVamList_other = new ArrayList<>();
            List<md_vam_other> myVamList_other = null;
        vam_Other_adapter adapter = new vam_Other_adapter(view.getContext(), myVamList_other);
            myVamList_other=adapter.getvam_Other_list();
            for (int i = 0; i < myVamList_other.size(); i++) {
                md_vam_other singleselkarbar = myVamList_other.get(i);
                if (singleselkarbar.isSelected()) {

                    //    data = data + "\n" + singleselkarbar.getName().toString();
                    Global_var.mablagh_pard_ozv = (Global_var.mablagh_pard_ozv + singleselkarbar.getMablagh_gh_vam_nob());
                    Toast.makeText(getContext(), singleselkarbar.getId_par_vam_nbt(), Toast.LENGTH_LONG).show();

                }

            }

            //startActivity(new Intent(getApplicationContext(),zarinpal.class));

    }
}
//  get_vam_karbar();
//        OtherAza_select_adapter adapter = new OtherAza_select_adapter(getActivity(), myVamList);
//        recyclerView.setAdapter(adapter);
//        Toast.makeText(getContext(), Arrays.toString(myVamList.toArray()), Toast.LENGTH_LONG).show();
//
//
////                  //set the Layout manager
//         setLayoutManger(new LinearLayoutManager(getActivity()));
//     OtherAza_select_adapter adapter = new OtherAza_select_adapter(getActivity(), myVamList);
////            //Set Adapter
//    recyclerView.setAdapter(adapter);
////       // Toast.makeText(getContext(),"fargment vam hastim_onviewcreated_set recyclerview",Toast.LENGTH_LONG).show();
//      //  Toast.makeText(getContext(), Arrays.toString(myVamList.toArray()), Toast.LENGTH_LONG).show();
//        get_vam_karbar();
////        // Set empty_view
//        setEmptyView(R.layout.empty_view);
//        setEmptyViewAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
//        frameLayout.getLayoutTransition();
//        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
//        ////////////////bind data az server fo recycler view
//        final String URL_moshvam = "http://192.168.101.1/MyApi/Api.php";
//
//
//        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_moshvam,
//                new Response.Listener<String>() {
//                    @Override
//                    public void onResponse(String response) {
//                        try {
//                            //converting the string to json array object
//                            JSONArray array = new JSONArray(response);
//
//                            //traversing through all the object
//                            for (int i = 0; i < array.length(); i++) {
//
//                                //getting product object from json array
//                                JSONObject mosh_vam = array.getJSONObject(i);
//
//                                //adding the mosh_vam to myvam list
//                                myVamList.add(new md_otheraza(
//                                        mosh_vam.getString("id"),
//                                        mosh_vam.getString("title"),
//                                        mosh_vam.getString("shortdesc"),
//                                        mosh_vam.getString("rating"),
//                                        mosh_vam.getString("price"),
//                                        mosh_vam.getString("image")
//                                ));
//                            }
//
//                            //creating adapter object and setting it to recyclerview
//                            OtherAza_select_adapter adapter = new OtherAza_select_adapter(getContext(), myVamList);
//                            recyclerView.setAdapter(adapter);
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                },
//                new Response.ErrorListener() {
//                    @Override
//                    public void onErrorResponse(VolleyError error) {
//
//                    }
//                }){
//            @Override
//            protected Map<String, String> getParams() throws AuthFailureError
//            {
//                Map<String, String> map = new Hashtable<>();
//                map.put("id_ozviat", Global_var.gid_ozviat);
//                map.put("sal_mali",Global_var.gsal_mali);
//
//                return map;
//            }
//        };
//
//        //adding our stringrequest to queue
//        MySingleton.getInstance(getContext()).addToRequestQueue(stringRequest);

