/**
 * Cellframe Wallet iOS Bridge Polyfill
 * 
 * Intercepts fetch() and XMLHttpRequest calls to localhost:8045
 * and routes them through WKScriptMessageHandler bridge.
 * 
 * This allows dApps in WKWebView to communicate with the wallet's
 * HTTP API without direct localhost access (which iOS blocks).
 */

(function() {
    'use strict';
    
    // Check if already injected
    if (window.__cellframeBridgeInjected) {
        console.log('[CellframeBridge] Already injected, skipping');
        return;
    }
    window.__cellframeBridgeInjected = true;
    
    const LOCALHOST_PATTERN = /^https?:\/\/(localhost|127\.0\.0\.1):8045/i;
    const BRIDGE_NAME = 'cellframeBridge';
    
    // Pending requests storage
    let requestId = 0;
    const pendingRequests = new Map();
    
    // Check if bridge is available
    function hasBridge() {
        return window.webkit && 
               window.webkit.messageHandlers && 
               window.webkit.messageHandlers[BRIDGE_NAME];
    }
    
    // Check if URL should be intercepted
    function shouldIntercept(url) {
        if (!url) return false;
        const urlStr = url.toString();
        return LOCALHOST_PATTERN.test(urlStr);
    }
    
    // Send request through bridge
    function sendBridgeRequest(method, url, headers, body) {
        return new Promise((resolve, reject) => {
            if (!hasBridge()) {
                reject(new Error('Cellframe bridge not available'));
                return;
            }
            
            const id = ++requestId;
            
            // Store promise callbacks
            pendingRequests.set(id, { resolve, reject });
            
            // Parse URL to get query string
            let urlObj;
            try {
                urlObj = new URL(url);
            } catch (e) {
                urlObj = new URL(url, window.location.origin);
            }
            
            const message = {
                id: id,
                method: method,
                path: urlObj.pathname,
                query: urlObj.search,
                headers: headers || {},
                body: body || null,
                // Include page origin for dApp identification
                pageUrl: window.location.href,
                pageOrigin: window.location.origin,
                pageHost: window.location.host
            };
            
            console.log('[CellframeBridge] Sending request:', id, method, urlObj.pathname + urlObj.search);
            
            try {
                window.webkit.messageHandlers[BRIDGE_NAME].postMessage(JSON.stringify(message));
            } catch (e) {
                pendingRequests.delete(id);
                reject(new Error('Bridge communication failed: ' + e.message));
            }
            
            // Timeout after 30 seconds
            setTimeout(() => {
                if (pendingRequests.has(id)) {
                    pendingRequests.delete(id);
                    reject(new Error('Bridge request timeout'));
                }
            }, 30000);
        });
    }
    
    // Handle response from native side
    window.__cellframeBridgeCallback = function(responseJson) {
        try {
            const response = JSON.parse(responseJson);
            const id = response.id;
            const pending = pendingRequests.get(id);
            
            if (pending) {
                pendingRequests.delete(id);
                
                if (response.error) {
                    pending.reject(new Error(response.error));
                } else {
                    pending.resolve({
                        status: response.status || 200,
                        statusText: response.statusText || 'OK',
                        headers: response.headers || {},
                        body: response.body || ''
                    });
                }
            }
        } catch (e) {
            console.error('[CellframeBridge] Callback parse error:', e);
        }
    };
    
    // ==================== Fetch Polyfill ====================
    
    const originalFetch = window.fetch;
    
    window.fetch = function(input, init) {
        let url = input;
        if (input instanceof Request) {
            url = input.url;
        }
        
        if (!shouldIntercept(url)) {
            return originalFetch.apply(this, arguments);
        }
        
        console.log('[CellframeBridge] Intercepting fetch:', url);
        
        const method = (init && init.method) || 'GET';
        const headers = {};
        
        if (init && init.headers) {
            if (init.headers instanceof Headers) {
                init.headers.forEach((value, key) => { headers[key] = value; });
            } else if (typeof init.headers === 'object') {
                Object.assign(headers, init.headers);
            }
        }
        
        const body = init ? init.body : null;
        
        return sendBridgeRequest(method, url, headers, body)
            .then(result => {
                // Create Response object
                const responseInit = {
                    status: result.status,
                    statusText: result.statusText,
                    headers: new Headers(result.headers)
                };
                return new Response(result.body, responseInit);
            });
    };
    
    // ==================== XMLHttpRequest Polyfill ====================
    
    const OriginalXHR = window.XMLHttpRequest;
    
    function PatchedXHR() {
        const xhr = new OriginalXHR();
        const self = this;
        
        // Internal state
        let intercepted = false;
        let method = 'GET';
        let url = '';
        let requestHeaders = {};
        let responseHeaders = {};
        let responseText = '';
        let responseStatus = 0;
        let responseStatusText = '';
        let readyState = 0;
        let async = true;
        
        // Event handlers
        this.onreadystatechange = null;
        this.onload = null;
        this.onerror = null;
        this.onloadend = null;
        this.ontimeout = null;
        this.onprogress = null;
        
        // Properties
        Object.defineProperty(this, 'readyState', { get: () => intercepted ? readyState : xhr.readyState });
        Object.defineProperty(this, 'status', { get: () => intercepted ? responseStatus : xhr.status });
        Object.defineProperty(this, 'statusText', { get: () => intercepted ? responseStatusText : xhr.statusText });
        Object.defineProperty(this, 'responseText', { get: () => intercepted ? responseText : xhr.responseText });
        Object.defineProperty(this, 'response', { get: () => intercepted ? responseText : xhr.response });
        Object.defineProperty(this, 'responseType', { 
            get: () => xhr.responseType,
            set: (v) => { xhr.responseType = v; }
        });
        Object.defineProperty(this, 'timeout', { 
            get: () => xhr.timeout,
            set: (v) => { xhr.timeout = v; }
        });
        Object.defineProperty(this, 'withCredentials', { 
            get: () => xhr.withCredentials,
            set: (v) => { xhr.withCredentials = v; }
        });
        
        // Methods
        this.open = function(m, u, a) {
            method = m;
            url = u;
            async = a !== false;
            requestHeaders = {};
            
            if (shouldIntercept(u)) {
                intercepted = true;
                readyState = 1; // OPENED
                console.log('[CellframeBridge] Intercepting XHR:', method, url);
            } else {
                intercepted = false;
                xhr.open.apply(xhr, arguments);
            }
        };
        
        this.setRequestHeader = function(name, value) {
            if (intercepted) {
                requestHeaders[name] = value;
            } else {
                xhr.setRequestHeader(name, value);
            }
        };
        
        this.getResponseHeader = function(name) {
            if (intercepted) {
                return responseHeaders[name.toLowerCase()] || null;
            }
            return xhr.getResponseHeader(name);
        };
        
        this.getAllResponseHeaders = function() {
            if (intercepted) {
                return Object.entries(responseHeaders)
                    .map(([k, v]) => k + ': ' + v)
                    .join('\r\n');
            }
            return xhr.getAllResponseHeaders();
        };
        
        this.abort = function() {
            if (!intercepted) {
                xhr.abort();
            }
        };
        
        this.send = function(body) {
            if (!intercepted) {
                // Forward events for non-intercepted requests
                xhr.onreadystatechange = () => {
                    if (self.onreadystatechange) self.onreadystatechange();
                };
                xhr.onload = (e) => { if (self.onload) self.onload(e); };
                xhr.onerror = (e) => { if (self.onerror) self.onerror(e); };
                xhr.onloadend = (e) => { if (self.onloadend) self.onloadend(e); };
                xhr.ontimeout = (e) => { if (self.ontimeout) self.ontimeout(e); };
                xhr.onprogress = (e) => { if (self.onprogress) self.onprogress(e); };
                
                xhr.send(body);
                return;
            }
            
            // Intercepted request - use bridge
            sendBridgeRequest(method, url, requestHeaders, body)
                .then(result => {
                    responseStatus = result.status;
                    responseStatusText = result.statusText;
                    responseHeaders = {};
                    
                    // Normalize header names to lowercase
                    if (result.headers) {
                        Object.entries(result.headers).forEach(([k, v]) => {
                            responseHeaders[k.toLowerCase()] = v;
                        });
                    }
                    
                    responseText = result.body;
                    
                    // Fire events
                    readyState = 2; // HEADERS_RECEIVED
                    if (self.onreadystatechange) self.onreadystatechange();
                    
                    readyState = 3; // LOADING
                    if (self.onreadystatechange) self.onreadystatechange();
                    
                    readyState = 4; // DONE
                    if (self.onreadystatechange) self.onreadystatechange();
                    if (self.onload) self.onload({});
                    if (self.onloadend) self.onloadend({});
                })
                .catch(error => {
                    console.error('[CellframeBridge] XHR error:', error);
                    readyState = 4;
                    responseStatus = 0;
                    if (self.onreadystatechange) self.onreadystatechange();
                    if (self.onerror) self.onerror({ error: error.message });
                    if (self.onloadend) self.onloadend({});
                });
        };
        
        // EventTarget methods
        this.addEventListener = function(type, listener) {
            if (!intercepted) {
                xhr.addEventListener(type, listener);
            }
        };
        this.removeEventListener = function(type, listener) {
            if (!intercepted) {
                xhr.removeEventListener(type, listener);
            }
        };
    }
    
    // Replace XMLHttpRequest
    window.XMLHttpRequest = PatchedXHR;
    
    // Keep reference to original
    window.XMLHttpRequest.UNSENT = 0;
    window.XMLHttpRequest.OPENED = 1;
    window.XMLHttpRequest.HEADERS_RECEIVED = 2;
    window.XMLHttpRequest.LOADING = 3;
    window.XMLHttpRequest.DONE = 4;
    
    console.log('[CellframeBridge] iOS Bridge polyfill injected successfully');
    console.log('[CellframeBridge] Bridge available:', hasBridge());
})();

