/**
 * Native Android Bridge
 * Komunikasi antara Web dan Native Android App
 */

(function() {
    'use strict';

    // Detect if running in native app
    window.isNativeApp = typeof Android !== 'undefined';
    window.hasNativeBluetooth = window.isNativeApp;

    console.log('[Native Bridge] Running in native app:', window.isNativeApp);

    if (!window.isNativeApp) {
        console.log('[Native Bridge] Running in browser, native features disabled');
        return;
    }

    // Native Print Manager
    window.NativePrintManager = {
        /**
         * Print via native Bluetooth
         * @param {Object} data - Receipt data
         * @returns {Promise}
         */
        print: function(data) {
            return new Promise((resolve, reject) => {
                try {
                    // Set callbacks
                    window.onPrintSuccess = function(message) {
                        resolve(message);
                        // Cleanup
                        delete window.onPrintSuccess;
                        delete window.onPrintError;
                    };

                    window.onPrintError = function(message) {
                        reject(new Error(message));
                        // Cleanup
                        delete window.onPrintSuccess;
                        delete window.onPrintError;
                    };

                    // Call native method
                    const jsonData = JSON.stringify(data);
                    Android.printBluetooth(jsonData);

                    // Timeout fallback
                    setTimeout(() => {
                        if (window.onPrintSuccess || window.onPrintError) {
                            reject(new Error('Print timeout'));
                            delete window.onPrintSuccess;
                            delete window.onPrintError;
                        }
                    }, 30000); // 30 seconds timeout

                } catch (error) {
                    reject(error);
                }
            });
        },

        /**
         * Select printer
         */
        selectPrinter: function() {
            try {
                Android.selectPrinter();
            } catch (error) {
                console.error('[Native Bridge] Select printer error:', error);
            }
        },

        /**
         * Get printer status
         * @returns {Object}
         */
        getStatus: function() {
            try {
                const statusJson = Android.getPrinterStatus();
                return JSON.parse(statusJson);
            } catch (error) {
                console.error('[Native Bridge] Get status error:', error);
                return {
                    connected: false,
                    printerAddress: null,
                    bluetoothEnabled: false
                };
            }
        },

        /**
         * Show native toast
         * @param {String} message
         */
        showToast: function(message) {
            try {
                Android.showToast(message);
            } catch (error) {
                console.error('[Native Bridge] Toast error:', error);
            }
        }
    };

    console.log('[Native Bridge] Initialized successfully');

})();
