package com.team.app.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.team.app.MyBottomSheetDialog;
import com.team.app.R;
import com.team.app.activities.GameDetailActivity;
import com.team.app.adapter.ContestCodeAdapter;
import com.team.app.models.Code;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.team.app.Constant.APP_URL_MAIN;

public class CodesFragment extends Fragment {

    ProgressBar progressBar;
    RecyclerView recyclerView;
    TextView txt_no_codes_available;
    private List<Object> codeList;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_codes, viewGroup, false);
        inflate.findViewById(R.id.add_your_contest_btn).setOnClickListener(view -> new MyBottomSheetDialog()
                .show(CodesFragment.this.requireActivity().getSupportFragmentManager(),"My Bottom Sheet Dialog"));
        txt_no_codes_available = inflate.findViewById(R.id.txt_no_codes_available);
        progressBar = inflate.findViewById(R.id.progressBar);
        recyclerView = inflate.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        loadCodes();
        return inflate;
    }

    private void loadCodes() {
        String JSON_URL = APP_URL_MAIN + "api.php?contest=true&c_id=" + GameDetailActivity.cricket.getId();
        codeList = new ArrayList<>();
        JsonArrayRequest request = new JsonArrayRequest(JSON_URL, response -> {
            if(response.length()>0) {
                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject object = response.getJSONObject(i);
                        String id = object.getString("id");
                        String mid = object.getString("matchid");
                        String app = object.getString("appname");
                        String code = object.getString("code");
                        String fee = object.getString("fees");
                        String wamt = object.getString("winning");
                        String team = object.getString("totalteam");
                        String win = object.getString("totalwin");
                        Code code1 = new Code(id, mid, app, fee, code, wamt, team, win);
                        codeList.add(code1);
                    } catch (JSONException exception) {
                        Toast.makeText(getContext(), "Error While Fatching Data", Toast.LENGTH_SHORT).show();
                    }
                    ContestCodeAdapter adapter = new ContestCodeAdapter(getContext(), progressBar, codeList);
                    recyclerView.setAdapter(adapter);
                }
            } else {
                progressBar.setVisibility(View.GONE);
                Toast.makeText(getContext(), "No Contest Found", Toast.LENGTH_SHORT).show();
            }
        }, error -> Toast.makeText(getContext(), "No Valid Response Found", Toast.LENGTH_SHORT).show());
        RequestQueue requestQueue = Volley.newRequestQueue(requireContext());
        requestQueue.add(request);
    }
}
