package com.dspread.pos.ui.payment;

import android.app.Application;
import android.graphics.Bitmap;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;

import com.dspread.pos.utils.QRCodeGenerator;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.SPUtils;

public class WaitingForCardViewModel extends BaseViewModel {
    public ObservableBoolean showQRCode = new ObservableBoolean(true);
    public ObservableField<Bitmap> qrCodeBitmap = new ObservableField<>();

    public ObservableField<String> amount = new ObservableField<>("0.00");
    public ObservableField<String> currencySymbol = new ObservableField<>("$");

    public WaitingForCardViewModel(@NonNull Application application) {
        super(application);
    }


    public void setAmount(String amount) {
        Double amountDouble = convertCentsToDollars(amount);
        this.amount.set(String.valueOf(amountDouble));
    }
    public void setCurrencySymbol(String symbol) {
        this.currencySymbol.set(symbol);
    }
    public static Double convertCentsToDollars(String amountValue) {
        if (amountValue == null || amountValue.isEmpty()) {
            return 0.0; // Default value if the input is null or empty
        }
        try {
            return Double.parseDouble(amountValue) / 100.0; // Convert cents to dollars
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0.0; // Default value if parsing fails
        }
    }
    public void generateQRCode(String paymentData) {
        try {
            Bitmap qrCode = QRCodeGenerator.generateQRCode(paymentData, 500, 500);
            if (qrCode != null) {
                qrCodeBitmap.set(qrCode);
                Log.d("QRCode", "QR code generated successfully");
            } else {
                Log.e("QRCode", "Failed to generate QR code");
            }
        } catch (Exception e) {
            Log.e("QRCode", "Error generating QR code: " + e.getMessage());
        }
    }

    public void onCancel() {
        // Handle cancel action
        Log.d("WaitingForCardViewModel", "onCancel: ");
    }
}