package com.friendvilla.slotmachine;

import static android.widget.Toast.LENGTH_SHORT;
import static android.widget.Toast.makeText;
import static com.friendvilla.slotmachine.MainActivity.edit;
import static com.friendvilla.slotmachine.MainActivity.sp;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
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.google.android.material.bottomsheet.BottomSheetDialogFragment;

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 -> {
            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) > sp.getInt("bal",0)) {
                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 {
                pb.setVisibility(View.VISIBLE);
                new Handler().postDelayed(() -> {
                    int bal = sp.getInt("bal",0) - Integer.parseInt(amnt);
                    edit.putInt("bal",bal);edit.apply();pb.setVisibility(View.GONE);
                    makeText(ctx,"Request is successful",LENGTH_SHORT).show();
                },3000);
            }
        });
        setCancelable(true);
        return v;
    }
}
