package com.friendvilla.bucksbuddy.controls;

import static android.util.Base64.DEFAULT;
import static android.util.Base64.decode;
import static android.util.Base64.encode;
import static android.widget.Toast.LENGTH_SHORT;
import static android.widget.Toast.makeText;
import static com.friendvilla.bucksbuddy.MainActivity.USER;
import static com.friendvilla.bucksbuddy.Method.getUser;
import static com.friendvilla.bucksbuddy.controls.Constant.CHECK_REFER;
import static com.friendvilla.bucksbuddy.controls.Constant.REFER;
import static com.friendvilla.bucksbuddy.controls.Constant.USER_ID;

import android.app.Activity;
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.TextView;

import androidx.annotation.Nullable;

import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.friendvilla.bucksbuddy.BuildConfig;
import com.friendvilla.bucksbuddy.R;
import com.friendvilla.bucksbuddy.model.User;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

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

public class ReferBox extends BottomSheetDialogFragment {

    private ProgressBar pb;
    private final TextView tv;
    private final Activity ctx;
    public ReferBox(Activity ctx, TextView tv) {
        this.ctx = ctx;
        this.tv = tv;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.refer,container,false);
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL,R.style.CustomSheet);
        pb = v.findViewById(R.id.load);
        EditText code = v.findViewById(R.id.tt);
        v.findViewById(R.id.btn).setOnClickListener(view -> {
            User user = USER.getValue(); assert user != null;
            String cod = code.getText().toString();
            if(cod.equals("")) {
                makeText(ctx,"Enter Referral Code",LENGTH_SHORT).show();
            } else if(cod.equals(user.getRefercode())) {
                makeText(ctx,"You can't use your own referral code",LENGTH_SHORT).show();
            } else {
                String BASE = new String(decode(BuildConfig.BASE_URL,DEFAULT));
                String referData = USER_ID + "@" + cod + "@" + REFER;
                String HASH = new String(encode(referData.getBytes(),DEFAULT));
                String URL = BASE + CHECK_REFER + HASH; pb.setVisibility(View.VISIBLE);
                Volley.newRequestQueue(ctx).add(new JsonObjectRequest(URL,response -> {
                    try {
                        JSONArray res = response.getJSONArray("result");
                        JSONObject obj = res.getJSONObject(0);
                        pb.setVisibility(View.GONE);dismiss();
                        if(obj.getString("success").equals("1")) {
                            getUser(ctx);tv.setEnabled(false);tv.setClickable(false);
                        }
                        makeText(ctx,obj.getString("msg"),LENGTH_SHORT).show();
                    } catch (JSONException e) {
                        pb.setVisibility(View.GONE);dismiss();
                        makeText(ctx,e.getMessage(),LENGTH_SHORT).show();
                    }
                }, error -> {
                    pb.setVisibility(View.GONE);dismiss();
                    makeText(ctx, error.getMessage(), LENGTH_SHORT).show();
                }));
            }
        });
        setCancelable(true);
        return v;
    }
}
