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 androidx.lifecycle.MutableLiveData;

import com.dspread.pos.common.manager.QPOSCallbackManager;
import com.dspread.pos.printerAPI.PrinterHelper;
import com.dspread.pos.utils.LogFileConfig;
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<>("$");
    // Add Face ID events
    public MutableLiveData<Boolean> startFaceID = new MutableLiveData<>();
    public MutableLiveData<Boolean> faceIDSuccess = new MutableLiveData<>();
    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, 300, 300);
            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());
        }
    }

    // Add Face ID methods
    public void onFaceIDClicked() {
        startFaceID.setValue(true);
    }

    public void simulateFaceIDSuccess() {
        // This will be called when face is detected
        faceIDSuccess.setValue(true);
    }

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

    @Override
    public void onDestroy() {
        Log.d("WaitingCardVM", "onDestroy: ");
//        super.onDestroy();
//        LogFileConfig.getInstance(this).readLog();
//        QPOSCallbackManager.getInstance().unregisterPaymentCallback();
//        PrinterHelper.getInstance().close();
    }

}