File size: 6,714 Bytes
eef08ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
document.addEventListener('DOMContentLoaded', () => {
    // --- CONFIGURATION ---
    const config = {
        // IMPORTANT: Paste your full and correct Cloudflare Worker URL here
        backendUrl: 'https://ar-city-explorer-backend.<your-subdomain>.workers.dev'
    };

    // --- GLOBAL STATE ---
    let poisData = []; // This will hold all our landmark data from the backend
    let arActive = false;

    // --- DOM ELEMENT SELECTORS ---
    const arToggle = document.getElementById('arToggle');
    const arViewport = document.getElementById('arViewport');
    const normalView = document.getElementById('normalView');
    const aiAssistant = document.getElementById('aiAssistant');
    const userInput = document.getElementById('userInput');
    const sendBtn = document.getElementById('sendBtn');
    const objectModal = document.getElementById('objectModal');
    const objectTitle = document.getElementById('objectTitle');
    const objectDescription = document.getElementById('objectDescription');
    const objectImage = document.getElementById('objectImage');
    const closeObjectModal = document.getElementById('closeObjectModal');

    // --- CORE FUNCTIONS ---

    function toggleARView(showAR) {
        arActive = showAR;
        if (arActive) {
            normalView.classList.add('hidden');
            arViewport.classList.remove('hidden');
            arToggle.innerHTML = '<i class="fas fa-times mr-1"></i> Exit AR';
            // Fetch data only if it hasn't been fetched yet
            if (poisData.length === 0) {
                fetchPoisAndCreateAREntities();
            }
        } else {
            arViewport.classList.add('hidden');
            normalView.classList.remove('hidden');
            arToggle.innerHTML = '<i class="fas fa-vr-cardboard mr-1"></i> AR Mode';
        }
    }

    function fetchPoisAndCreateAREntities() {
        console.log('Fetching POIs from backend...');
        fetch(`${config.backendUrl}/api/pois`)
            .then(response => {
                if (!response.ok) throw new Error(`Network error: ${response.statusText}`);
                return response.json();
            })
            .then(pois => {
                console.log('POIs received:', pois);
                poisData = pois; // Store the data globally
                const scene = document.querySelector('a-scene');
                if (!scene) return console.error('A-Frame scene not found!');

                pois.forEach(poi => {
                    const entity = document.createElement('a-entity');
                    entity.setAttribute('gps-new-entity-place', {
                        latitude: poi.latitude,
                        longitude: poi.longitude
                    });

                    const box = document.createElement('a-box');
                    box.setAttribute('material', 'color: red; opacity: 0.7;');
                    box.setAttribute('scale', '10 10 10');
                    box.setAttribute('position', '0 5 0');
                    box.setAttribute('data-poi-id', poi.id); // Set ID for click detection
                    entity.appendChild(box);

                    const text = document.createElement('a-text');
                    text.setAttribute('value', poi.name);
                    text.setAttribute('look-at', '[gps-new-camera]');
                    text.setAttribute('scale', '50 50 50');
                    text.setAttribute('position', '0 15 0');
                    entity.appendChild(text);
                    
                    scene.appendChild(entity);
                });
            })
            .catch(error => {
                console.error('Failed to load POIs:', error);
                alert('Could not load city data. Check your connection and the backend URL.');
                toggleARView(false);
            });
    }

    // --- MODAL AND UI FUNCTIONS ---
    
    function showObjectInfo(poiId) {
        const poi = poisData.find(p => p.id === poiId);
        if (!poi) return;
        objectTitle.textContent = poi.name;
        objectDescription.textContent = poi.description || "No description available.";
        objectImage.src = `https://via.placeholder.com/300x200?text=${encodeURIComponent(poi.name)}`;
        objectModal.classList.remove('hidden');
    }

    function addUserMessage(message) {
        const chatContainer = aiAssistant.querySelector('.flex-col');
        const msg = `<div class="ai-message user-message"><p>${message}</p></div>`;
        chatContainer.insertAdjacentHTML('beforeend', msg);
        aiAssistant.scrollTop = aiAssistant.scrollHeight;
    }

    function addAIMessage(message) {
        const chatContainer = aiAssistant.querySelector('.flex-col');
        const msg = `<div class="ai-message assistant-message"><div class="font-bold text-indigo-800 mb-1">AR Guide</div><p>${message}</p></div>`;
        chatContainer.insertAdjacentHTML('beforeend', msg);
        aiAssistant.scrollTop = aiAssistant.scrollHeight;
    }

    function handleSearch() {
        const searchTerm = userInput.value.trim();
        if (!searchTerm) return;
        addUserMessage(searchTerm);
        userInput.value = '';

        if (poisData.length === 0) {
            addAIMessage("I'm still loading location data. Please try again in a moment.");
            return;
        }

        const results = poisData.filter(poi =>
            poi.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
            (poi.description && poi.description.toLowerCase().includes(searchTerm.toLowerCase()))
        );

        if (results.length > 0) {
            let response = `I found ${results.length} result(s):<ul>`;
            results.forEach(poi => { response += `<li class="mt-2 list-disc list-inside">${poi.name}</li>`; });
            response += "</ul>";
            addAIMessage(response);
        } else {
            addAIMessage(`Sorry, I couldn't find anything matching "${searchTerm}".`);
        }
    }
    
    // --- EVENT LISTENERS ---
    
    arToggle.addEventListener('click', () => toggleARView(!arActive));

    document.querySelector('a-scene').addEventListener('click', (event) => {
        if (event.target.hasAttribute('data-poi-id')) {
            const poiId = parseInt(event.target.getAttribute('data-poi-id'), 10);
            showObjectInfo(poiId);
        }
    });

    closeObjectModal.addEventListener('click', () => objectModal.classList.add('hidden'));
    
    sendBtn.addEventListener('click', handleSearch);
    userInput.addEventListener('keypress', (e) => {
        if (e.key === 'Enter') handleSearch();
    });

    // Initial fetch of data so the search bar works immediately
    fetchPoisAndCreateAREntities();
});