package com.dspread.pos.ui.cashier;

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

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.dspread.pos.common.base.BaseAppViewModel;
import com.dspread.pos.data.local.PreferencesManager;

import org.json.JSONException;
import org.json.JSONObject;

public class CashierViewModel extends BaseAppViewModel {

    // Enum to represent payment status
    public enum PaymentStatus {
        WAITING, SUCCESS, FAILED
    }
    private static final String TAG = "CashierVM";


    // LiveData to hold payment information
    private final MutableLiveData<String> paymentInfo = new MutableLiveData<>();
    public LiveData<String> getPaymentInfo() {
        return paymentInfo;
    }

    public ObservableField<String> amount = new ObservableField<>("0.00");
    public ObservableField<String> currencyCode = new ObservableField<>("RUB");

    // LiveData to track payment status
    private final MutableLiveData<PaymentStatus> paymentStatus = new MutableLiveData<>(PaymentStatus.WAITING);
    public LiveData<PaymentStatus> getPaymentStatus() {
        return paymentStatus;
    }

    public final ObservableField<Integer> mainLogo = new ObservableField<>();
    public final ObservableField<Bitmap> mainLogoBitmap = new ObservableField<>();

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

    public void printReceipt() {
        String amountValue = amount.get();
        String currencyCodeValue = currencyCode.get();
        if (amountValue != null && currencyCodeValue != null) {
            Log.d(TAG, "Printing Receipt: " + amountValue + " " + currencyCodeValue);
            // Simulate printing logic here
            // For example, send the payment info to a printer service
        } else {
            Log.d(TAG, "No payment info to print");
        }
    }


    @Override
    protected void onPaymentRedirected(String jsonString) {
        Log.d(TAG, "onPaymentRedirected - processing redirect payment");
        Log.d(TAG, "jsonString" + jsonString);

        try {
            JSONObject results = new JSONObject(jsonString);
            PreferencesManager prefs = PreferencesManager.getInstance(getApplication());
            prefs.saveString("lastPaymentRedirectResults", jsonString);

            // Update amount and currency
            amount.set(results.optString("amount", "0.00"));
            currencyCode.set(results.optString("currencySymbol", "RUB"));;

            String status = results.optString("status", "");
            switch (status) {
                case "success":
                    Log.d(TAG, "Redirect Payment Success");
                    paymentStatus.setValue(PaymentStatus.SUCCESS);
                    break;
                case "failed":
                    Log.d(TAG, "Redirect Payment Failed");
                    paymentStatus.setValue(PaymentStatus.FAILED);
                    break;
                default:
                    Log.w(TAG, "Unknown JSON type: " + status);
                    paymentStatus.setValue(PaymentStatus.WAITING);
            }
        } catch (JSONException e) {
            Log.e(TAG, "Error parsing JSON", e);
//            paymentStatus.setValue(PaymentStatus.FAILED);
        }

    }





}
