package com.makeeasy.dailyinterest;

import static android.widget.Toast.LENGTH_SHORT;
import static android.widget.Toast.makeText;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

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

public class DepositBox extends BottomSheetDialogFragment {
    private final Activity ctx;
    private String option = "";
    public DepositBox(Activity ctx) {
        this.ctx = ctx;
    }
    @SuppressLint("SetTextI18n")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.deposit,container,false);
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL,R.style.CustomSheet);
        EditText amount = v.findViewById(R.id.am);
        v.findViewById(R.id.r20).setOnClickListener(view -> amount.setText("20"));
        v.findViewById(R.id.r50).setOnClickListener(view -> amount.setText("50"));
        v.findViewById(R.id.r100).setOnClickListener(view -> amount.setText("100"));
        v.findViewById(R.id.r200).setOnClickListener(view -> amount.setText("200"));
        v.findViewById(R.id.r500).setOnClickListener(view -> amount.setText("500"));
        Spinner spinner = v.findViewById(R.id.spinner);
        List<String> cates = new ArrayList<>();
        if(Utils.CashFreeUpi != null && !Utils.CashFreeUpi.equals("")) {
            cates.add("UPI Gateway");
        }
        if(Utils.CashFreeId != null && !Utils.CashFreeId.equals("")) {
            cates.add("Cashfree Gateway");
        }
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(ctx, android.R.layout.simple_spinner_item, cates);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter); option = (String) spinner.getItemAtPosition(0);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                option = (String) adapterView.getItemAtPosition(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        v.findViewById(R.id.btn).setOnClickListener(view -> {
            String amnt = amount.getText().toString();
            if(amnt.equals("")) {
                makeText(ctx,"Fill Amount",LENGTH_SHORT).show();
            } else if(Integer.parseInt(amnt) < 10) {
                makeText(ctx,"Min Amount Rs.10",LENGTH_SHORT).show();
            } else if(option.equals("")) {
                makeText(ctx,"Choose payment option",LENGTH_SHORT).show();
            } else {
                MainActivity.amount = amnt; dismiss();
                switch (option) {
                    case "Cashfree Gateway":
                        MainActivity.generateToken(ctx,false);
                        break;
                    case "UPI Gateway":
                        MainActivity.generateToken(ctx,true);
                        break;
                }
            }
        });
        setCancelable(true);
        return v;
    }

}
