/**
 * Technician Location Services
 *
 * 1. Background logger -- logs GPS every 5 minutes while the tech is clocked in.
 * 2. getLocation() -- returns a Promise that resolves to {latitude, longitude, accuracy}.
 *    If the user denies permission or the request times out a blocking modal is shown
 *    and the promise is rejected.
 * 3. Blocking modal -- cannot be dismissed; forces the technician to grant permission.
 */
(function () {
    'use strict';

    var INTERVAL_MS = 5 * 60 * 1000;
    var CLOCK_CHECK_MS = 60 * 1000; // check clock status every 60s
    var locationTimer = null;
    var clockCheckTimer = null;
    var isClockedIn = false;
    var permissionDenied = false;

    // =====================================================================
    // BLOCKING MODAL
    // =====================================================================

    var modalEl = null;

    function ensureModal() {
        if (modalEl) return;
        var div = document.createElement('div');
        div.id = 'fusionLocationModal';
        div.innerHTML =
            '<div style="position:fixed;inset:0;background:rgba(0,0,0,.7);z-index:99999;display:flex;align-items:center;justify-content:center;">' +
                '<div style="background:#fff;border-radius:16px;max-width:400px;width:90%;padding:2rem;text-align:center;box-shadow:0 8px 32px rgba(0,0,0,.3);">' +
                    '<div style="font-size:3rem;color:#dc3545;margin-bottom:1rem;"><i class="fa fa-map-marker"></i></div>' +
                    '<h4 style="margin-bottom:0.5rem;">Location Required</h4>' +
                    '<p style="color:#666;font-size:0.95rem;">Your GPS location is mandatory to perform this action. ' +
                    'Please allow location access in your browser settings and try again.</p>' +
                    '<p style="color:#999;font-size:0.85rem;">If you previously denied access, open your browser settings ' +
                    'and reset the location permission for this site.</p>' +
                    '<button id="fusionLocationRetryBtn" style="background:#0d6efd;color:#fff;border:none;border-radius:12px;padding:0.75rem 2rem;font-size:1rem;cursor:pointer;margin-top:0.5rem;width:100%;">' +
                        '<i class="fa fa-refresh" style="margin-right:6px;"></i>Try Again' +
                    '</button>' +
                '</div>' +
            '</div>';
        document.body.appendChild(div);
        modalEl = div;
        document.getElementById('fusionLocationRetryBtn').addEventListener('click', function () {
            hideModal();
            window.fusionGetLocation().catch(function () {
                showModal();
            });
        });
    }

    function showModal() {
        ensureModal();
        modalEl.style.display = '';
    }

    function hideModal() {
        if (modalEl) modalEl.style.display = 'none';
    }

    // =====================================================================
    // PERMISSION-DENIED BANNER (persistent warning for background logger)
    // =====================================================================

    var bannerEl = null;

    function showDeniedBanner() {
        if (bannerEl) return;
        bannerEl = document.createElement('div');
        bannerEl.id = 'fusionLocationBanner';
        bannerEl.style.cssText =
            'position:fixed;top:0;left:0;right:0;z-index:9999;background:#dc3545;color:#fff;' +
            'padding:10px 16px;text-align:center;font-size:0.9rem;font-weight:600;box-shadow:0 2px 8px rgba(0,0,0,.2);';
        bannerEl.innerHTML =
            '<i class="fa fa-exclamation-triangle" style="margin-right:6px;"></i>' +
            'Location access is denied. Your location is not being tracked. ' +
            'Please enable location in browser settings.';
        document.body.appendChild(bannerEl);
    }

    // =====================================================================
    // getLocation() -- public API
    // =====================================================================

    function getLocation() {
        return new Promise(function (resolve, reject) {
            if (!navigator.geolocation) {
                reject(new Error('Geolocation is not supported by this browser.'));
                return;
            }
            navigator.geolocation.getCurrentPosition(
                function (position) {
                    permissionDenied = false;
                    if (bannerEl) { bannerEl.remove(); bannerEl = null; }
                    resolve({
                        latitude: position.coords.latitude,
                        longitude: position.coords.longitude,
                        accuracy: position.coords.accuracy || 0,
                    });
                },
                function (error) {
                    permissionDenied = true;
                    showDeniedBanner();
                    console.error('Fusion Location: GPS error', error.code, error.message);
                    reject(error);
                },
                { enableHighAccuracy: true, timeout: 15000, maximumAge: 30000 }
            );
        });
    }

    window.fusionGetLocation = getLocation;

    // =====================================================================
    // NAVIGATE -- opens Google Maps app on iOS/Android, browser fallback
    // =====================================================================

    function openGoogleMapsNav(el) {
        var addr = (el.dataset.navAddr || '').trim();
        var fallbackUrl = el.dataset.navUrl || '';
        if (!addr && !fallbackUrl) return;

        var dest = encodeURIComponent(addr) || fallbackUrl.split('destination=')[1];
        var isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
        var isAndroid = /Android/i.test(navigator.userAgent);

        if (isIOS) {
            window.location.href = 'comgooglemaps://?daddr=' + dest + '&directionsmode=driving';
        } else if (isAndroid) {
            window.location.href = 'google.navigation:q=' + dest;
        } else {
            window.open(fallbackUrl, '_blank');
        }
    }

    window.openGoogleMapsNav = openGoogleMapsNav;

    // =====================================================================
    // BACKGROUND LOGGER (tied to clock-in / clock-out status)
    // =====================================================================

    function isTechnicianPortal() {
        return window.location.pathname.indexOf('/my/technician') !== -1;
    }

    function checkClockStatus() {
        fetch('/my/technician/clock-status', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ jsonrpc: '2.0', method: 'call', params: {} }),
        })
        .then(function (r) { return r.json(); })
        .then(function (data) {
            var wasClocked = isClockedIn;
            isClockedIn = !!(data.result && data.result.clocked_in);
            if (isClockedIn && !wasClocked) {
                // Just clocked in — start tracking immediately
                startLocationTimer();
            } else if (!isClockedIn && wasClocked) {
                // Just clocked out — stop tracking
                stopLocationTimer();
            }
        })
        .catch(function () {
            /* network error: keep current state */
        });
    }

    function logLocation() {
        if (!isClockedIn || document.hidden || !navigator.geolocation) return;

        getLocation().then(function (coords) {
            fetch('/my/technician/location/log', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    jsonrpc: '2.0',
                    method: 'call',
                    params: {
                        latitude: coords.latitude,
                        longitude: coords.longitude,
                        accuracy: coords.accuracy,
                    }
                }),
            })
            .then(function (r) { return r.json(); })
            .then(function (data) {
                if (data.result && !data.result.success) {
                    console.warn('Fusion Location: server rejected log', data.result);
                }
            })
            .catch(function (err) {
                console.warn('Fusion Location: network error', err);
            });
        }).catch(function () {
            /* permission denied -- banner already shown */
        });
    }

    function startLocationTimer() {
        if (locationTimer) return; // already running
        logLocation(); // immediate first log
        locationTimer = setInterval(logLocation, INTERVAL_MS);
    }

    function stopLocationTimer() {
        if (locationTimer) {
            clearInterval(locationTimer);
            locationTimer = null;
        }
    }

    function startLocationLogging() {
        if (!isTechnicianPortal()) return;

        // Check clock status immediately, then every 60s
        checkClockStatus();
        clockCheckTimer = setInterval(checkClockStatus, CLOCK_CHECK_MS);

        // Pause/resume on tab visibility
        document.addEventListener('visibilitychange', function () {
            if (document.hidden) {
                stopLocationTimer();
            } else if (isClockedIn) {
                startLocationTimer();
            }
        });
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', startLocationLogging);
    } else {
        startLocationLogging();
    }
})();
