package com.team.app;

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

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.team.app.activities.GameDetailActivity;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

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

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

public class MyBottomSheetDialog extends BottomSheetDialogFragment {

    EditText application_name;
    EditText contest_code;
    EditText entry_fees;
    ProgressBar progressBar;
    EditText total_team;
    EditText total_winners;
    EditText winning_amount;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.add_contest, viewGroup, false);
        this.progressBar = inflate.findViewById(R.id.progressBar);
        this.application_name = inflate.findViewById(R.id.application_name);
        this.contest_code = inflate.findViewById(R.id.contest_code);
        this.entry_fees = inflate.findViewById(R.id.entry_fees);
        this.winning_amount = inflate.findViewById(R.id.winning_amount);
        this.total_team = inflate.findViewById(R.id.total_team);
        this.total_winners = inflate.findViewById(R.id.total_winners);
        inflate.findViewById(R.id.close).setOnClickListener(view -> MyBottomSheetDialog.this.dismiss());
        inflate.findViewById(R.id.submit_btn).setOnClickListener(view -> {
            MyBottomSheetDialog.this.progressBar.setVisibility(View.VISIBLE);
            String mid = GameDetailActivity.cricket.getId();
            String app = application_name.getText().toString();
            String code = contest_code.getText().toString();
            String fee = entry_fees.getText().toString();
            String wAmt = winning_amount.getText().toString();
            String team = total_team.getText().toString();
            String twin = total_winners.getText().toString();
            application_name.setText("");contest_code.setText("");entry_fees.setText("");
            winning_amount.setText("");total_team.setText("");total_winners.setText("");
            if (!app.equals("") && !code.equals("") && !fee.equals("") && !wAmt.equals("") && !team.equals("") && !twin.equals("")) {
                String JSON_URL = APP_URL_MAIN + "api.php?add_contest=true&mid="+mid+"&app="+app+"&code="+code+
                        "&fee="+fee+"&win="+wAmt+"&team="+team+"&twin="+twin;
                JsonArrayRequest request = new JsonArrayRequest(JSON_URL, response -> {
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject object = response.getJSONObject(i);
                            String status = object.getString("status");
                            String msg = object.getString("msg");
                            MyBottomSheetDialog.this.progressBar.setVisibility(View.GONE);
                            MyBottomSheetDialog.this.dismiss();
                            Toast.makeText(inflate.getContext(),msg, Toast.LENGTH_SHORT).show();
                        } catch (JSONException exception) {
                            MyBottomSheetDialog.this.progressBar.setVisibility(View.GONE);
                            Toast.makeText(inflate.getContext(), "Error While Fatching Data", Toast.LENGTH_SHORT).show();
                        }
                    }
                }, error -> {
                    MyBottomSheetDialog.this.progressBar.setVisibility(View.GONE);
                    Toast.makeText(inflate.getContext(), "No Valid Response Found", Toast.LENGTH_SHORT).show();
                });
                RequestQueue requestQueue = Volley.newRequestQueue(inflate.getContext());
                requestQueue.add(request);
            }
        });
        return inflate;
    }
}
