package ir.almahdi.sandogh_bsa_aza.vam_ha_ozv;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
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.idpay.Main2Activity;
import ir.almahdi.sandogh_bsa_aza.vam_ha_ozv.model.md_vam_karbar;
import ir.almahdi.sandogh_bsa_aza.zarinpal.zarinpal;


/**
 * A simple {@link Fragment} subclass.
 */
public class vam_karbar_frg extends Fragment {
    TextView tarikhvam,idvam,mablaghvam,tedad_aghsat,bazpard_vam_mah;
//    private FrameLayout frameLayout;
    private static RecyclerView recyclerView;
    public RelativeLayout layout;
    //a list to store all the products
    private  List<md_vam_karbar> myVamList ;
    private  Button txt_pay_kol,txt_addtosabad_vamozv;
    private ImageView img_par_ozviat;
    TextView txtnovam,help_barname;
    public vam_karbar_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.fragment_frg_vam, container, false);
        init(view);
        // افزایش و کاهش دادن اندازه کمک برنامه در قسمت پرداخت و سبد پرداخت
        help_barname=view.findViewById(R.id.help);
        Button btn_help;
        btn_help=view.findViewById(R.id.btn_help);
        this.help_barname.getLayoutParams().height = 3;
        btn_help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if (help_barname.getLayoutParams().height == 3){
                  help_barname.setTextSize(16);

                  slideView(help_barname, help_barname.getLayoutParams().height,350);
                         }  else{slideView(help_barname, help_barname.getLayoutParams().height,3);
              }

            }
        });
        // افزایش و کاهش دادن اندازه کمک برنامه در قسمت پرداخت و سبد پرداختend
        txt_addtosabad_vamozv=view.findViewById(R.id.txt_addtosabad_vamozv);
        txtnovam=view.findViewById(R.id.no_vam_ozv);
        txt_pay_kol=view.findViewById(R.id.txt_pard_ghest_ozv_or_kol);
        myVamList= new ArrayList<>();
       // Toast.makeText(getContext(),"fargment vam hastim_oncreateview",Toast.LENGTH_LONG).show();
        recyclerView =  view.findViewById(R.id.recylcerview);
        //set the Layout manager
        LinearLayoutManager linear = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linear);
        get_vam_karbar();
        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();


        return view; }

    private void init(View view) {
        recyclerView =  view.findViewById(R.id.recylcerview);
        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_karbar() {
        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";

        Response.Listener<String> listener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
            // Toast.makeText(getContext(), response, Toast.LENGTH_LONG).show();
             if (!response.equals("no") && Global_var.numLoad_Vamozv==0) {
                 txtnovam.setVisibility(View.GONE);
                 recyclerView.setVisibility(View.VISIBLE);

                 Gson gson=new Gson();
                md_vam_karbar[] vam_karbars=gson.fromJson(response,md_vam_karbar[].class);
                  for (md_vam_karbar vam_karbar : vam_karbars) {
                    myVamList.add(new md_vam_karbar(
                    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()));
                    Global_var.mablagh_pard_ozv = (Global_var.mablagh_pard_ozv + Integer.parseInt( vam_karbar.getMablagh_gh_vam_nob()));


        //  s+=vam_karbars[i].toString();
    }
           // Toast.makeText(getContext(), vam_karbars[0].getMande_vam(), Toast.LENGTH_LONG).show();
                 vam_adapter adapter = new vam_adapter(getContext(), myVamList);
            recyclerView.setHasFixedSize(true);
            recyclerView.setAdapter(adapter);
            Global_var.numLoad_Vamozv=1;
                 txt_pay_kol.setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
                        Global_var.pard_one_karbar=0;
                         if(Global_var.num_click_pay_ozv==0){
                             Global_var.sum_hozv_vam_ozv = (Global_var.mablagh_pard_ozv)+Global_var.gmab_hagh_ozv;
                             Toast.makeText(getContext(), " جمع سبد پرداخت شمامبلغ  :"+ Global_var.sum_hozv_vam_ozv+" می باشد."+" ", Toast.LENGTH_LONG).show();

                             startActivity(new Intent(getContext(),  Main2Activity.class));
                         }
                         else{
                             Toast.makeText(getContext(),  "شماعضوی را به سبد پردختی اضافه نکردید لطفا بر روی دکمه اضافه کردن به سبد پرداخت ضربه بزنید!", Toast.LENGTH_LONG).show();
                         }
                     }


                 });
                 txt_addtosabad_vamozv.setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
                         if(Global_var.num_click_pay_ozv==0){
                             Global_var.sum_hozv_vam_ozv = (Global_var.mablagh_pard_ozv)+Global_var.gmab_hagh_ozv;
                             Toast.makeText(getContext(), " جمع سبد پرداخت شمامبلغ  :"+ Global_var.sum_hozv_vam_ozv+" می باشد."+" ", Toast.LENGTH_LONG).show();

                             //  Toast.makeText(getApplicationContext(), String.valueOf(Global_var.mablagh_pard_ozv), Toast.LENGTH_LONG).show();
                             Global_var.num_click_pay_ozv= 0L;
                         }
                         else{
                             Toast.makeText(getContext(), "شمامبالغ مربوط به حق عضویت و وام(ها)خودتان را به سبد پرداخت اضافه کرده اید!", Toast.LENGTH_LONG).show();
                         }

                     }
                 });

        }else{
            // Toast.makeText(getContext(), "تسهیلاتی پیدا نشد!", Toast.LENGTH_LONG).show();
             myVamList.add(new md_vam_karbar("0","0","0","0","0","0","0"));
            vam_adapter adapter = new vam_adapter( getContext(), myVamList);
            recyclerView.setVisibility(View.INVISIBLE);
            recyclerView.setHasFixedSize(true);
            recyclerView.setAdapter(adapter);
           txtnovam.setVisibility(View.VISIBLE);
           txt_pay_kol.setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
//
                         if(Global_var.num_click_pay_ozv==0){
                             Global_var.sum_hozv_vam_ozv = (Global_var.mablagh_pard_ozv)+Global_var.gmab_hagh_ozv;
                             Toast.makeText(getContext(), " جمع سبد پرداخت شمامبلغ  :"+ Global_var.sum_hozv_vam_ozv+" می باشد."+" ", Toast.LENGTH_LONG).show();

                             //Toast.makeText(getContext(),  Global_var.sum_hozv_vam_ozv+"مجموع مبلغ سبد پرداختی شما:", Toast.LENGTH_LONG).show();
                             startActivity(new Intent(getContext(),  zarinpal.class));
                         }
                         else{
                             Toast.makeText(getContext(),  "ابتدامبلغی را به سبد پردختی اضافه کنید!", Toast.LENGTH_LONG).show();
                         }

                     }


                 });
                 txt_addtosabad_vamozv.setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
                         if(Global_var.num_click_pay_ozv==0){
                             // عضو وامی ندارد و فقط حق عضویت به سبد پرداخت اضافه میگردد
                             Global_var.sum_hozv_vam_ozv = Global_var.gmab_hagh_ozv;
                             Toast.makeText(getContext(), " جمع سبد پرداخت شمامبلغ  :"+ Global_var.sum_hozv_vam_ozv+" می باشد."+" ", Toast.LENGTH_LONG).show();

                             //  Toast.makeText(getApplicationContext(), String.valueOf(Global_var.mablagh_pard_ozv), Toast.LENGTH_LONG).show();
                             Global_var.num_click_pay_ozv+=1;
                         }
                         else{
                             Toast.makeText(getContext(), "شمامبالغ مربوط به حق عضویت و وام(ها) به سبد پرداخت اضافه کرده اید!", Toast.LENGTH_LONG).show();
                         }

                     }
                 });


             }


                //  Toast.makeText(getContext(), myVamList.toString(), Toast.LENGTH_LONG).show();

                //set the Layout manager
                //setLayoutManger(new LinearLayoutManager(getActivity()));


            }
        };
        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        };
        StringRequest request = new StringRequest(Request.Method.POST, url, listener, errorListener) {
            @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;
            }
        };
//       Toast.makeText(getContext(), myVamList.get(1).getMablagh_gh_vam_nob(), Toast.LENGTH_SHORT).show();
        //adding our stringrequest to queue
        MySingleton.getInstance(getContext()).addToRequestQueue(request);

    }
    public static void slideView(View view,
                                 int currentHeight,
                                 int newHeight) {

        ValueAnimator slideAnimator = ValueAnimator
                .ofInt(currentHeight, newHeight)
                .setDuration(500);



        slideAnimator.addUpdateListener(animation1 -> {
            view.getLayoutParams().height = (Integer) animation1.getAnimatedValue();
            view.requestLayout();
        });


        AnimatorSet animationSet = new AnimatorSet();
        animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
        animationSet.play(slideAnimator);
        animationSet.start();
    }

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

