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.updateUser;
import static com.friendvilla.bucksbuddy.controls.Constant.POST_TXN;
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 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;

import java.math.BigDecimal;

public class WithdrawBox extends BottomSheetDialogFragment {
    private ProgressBar pb;
    private final Activity ctx;
    public WithdrawBox(Activity ctx) {
        this.ctx = ctx;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.withdraw,container,false);
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL,R.style.CustomSheet);
        pb = v.findViewById(R.id.load);
        EditText amount = v.findViewById(R.id.nn);
        EditText upiid = v.findViewById(R.id.tt);
        v.findViewById(R.id.btn).setOnClickListener(view -> {
            User user = USER.getValue(); assert user != null;
            String amnt = amount.getText().toString();
            String upi = upiid.getText().toString();
            if(amnt.equals("")) {
                makeText(ctx,"Fill Amount",LENGTH_SHORT).show();
            } else if(Integer.parseInt(amnt) < 100) {
                makeText(ctx,"Min Amount Rs.100",LENGTH_SHORT).show();
            } else if(Integer.parseInt(amnt) > new BigDecimal(user.getWithdraw()).intValue()) {
                makeText(ctx,"You have not enough withdraw balance",LENGTH_SHORT).show();
            } else if(upi.equals("")) {
                makeText(ctx,"Fill UPI Id",LENGTH_SHORT).show();
            } else if(!upi.contains("@")) {
                makeText(ctx,"Invalid UPI",LENGTH_SHORT).show();
            } else {
                BigDecimal bd = new BigDecimal(user.getWithdraw()).subtract(new BigDecimal(amnt));
                String w = bd.toString(); pb.setVisibility(View.VISIBLE);
                updateUser(ctx,user.getDeposit(),w,"0");
                String txnDetails = "Withdraw," + amnt + "," + upi;
                String BASE = new String(decode(BuildConfig.BASE_URL,DEFAULT));
                String txnData = USER_ID + "=" + "0" + "=" + txnDetails;
                String HASH = new String(encode(txnData.getBytes(),DEFAULT));
                String URL = BASE + POST_TXN + HASH;
                Volley.newRequestQueue(ctx).add(new JsonObjectRequest(URL,response -> {
                    try {
                        JSONArray res = response.getJSONArray("result");
                        JSONObject obj = res.getJSONObject(0);
                        dismiss(); pb.setVisibility(View.GONE);
                        if(obj.getString("success").equals("1")) {
                            makeText(ctx,"Request Successful!",LENGTH_SHORT).show();
                        } else {
                            makeText(ctx, obj.getString("msg"), LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        dismiss(); pb.setVisibility(View.GONE);
                        makeText(ctx,e.getMessage(), LENGTH_SHORT).show();
                    }
                }, error -> {
                    dismiss(); pb.setVisibility(View.GONE);
                    makeText(ctx,error.getMessage(), LENGTH_SHORT).show();
                }));
            }
        });
        setCancelable(true);
        return v;
    }
}
