<template>
  <div v-if="isOpen" class="modal-overlay">
    <div class="modal">
        <div class="modal-header">
            <h3>Simulation Settings</h3>
            <button class="close-btn" @click="close">×</button>
        </div>
        
        <div class="modal-content">
            <div class="setting-group">
                <label>Simulation Mode</label>
                <div class="radio-group">
                    <label class="radio-label">
                        <input type="radio" value="simulated" v-model="simulationModeInput" />
                        Simulated (Manual Time)
                    </label>
                    <label class="radio-label">
                        <input type="radio" value="live" v-model="simulationModeInput" />
                        Live (Real Time)
                    </label>
                </div>
            </div>

            <div class="setting-group">
                <label>Brightness: {{ brightnessInput }}</label>
                <div class="description">Global light intensity multiplier.</div>
                <input 
                    type="range" 
                    min="0.1" 
                    max="2.0" 
                    step="0.1" 
                    v-model="brightnessInput" 
                    class="slider"
                />
            </div>

            <div class="setting-group">
                <label>Max Zoom Height: {{ maxZoomInput }}</label>
                <div class="description">Limit how far the camera can zoom out.</div>
                <input 
                    type="range" 
                    min="50" 
                    max="300" 
                    step="10" 
                    v-model="maxZoomInput" 
                    class="slider"
                />
            </div>

            <div class="setting-group">
                <label>Zoom Sensitivity: {{ zoomSensitivityInput }}</label>
                <div class="description">Speed of zooming in/out.</div>
                <input 
                    type="range" 
                    min="1" 
                    max="20" 
                    step="1" 
                    v-model="zoomSensitivityInput" 
                    class="slider"
                />
            </div>

            <div class="setting-group">
                <label>Data Origin (Base URL)</label>
                <div class="description">
                    Prepend this URL to all data file requests. Leave empty for default (local).
                </div>
                <input 
                    v-model="baseUrlInput" 
                    type="text" 
                    placeholder="e.g., http://localhost:8080/" 
                    class="input-field"
                />
            </div>
        </div>

        <div class="modal-actions">
            <button class="btn secondary" @click="close">Cancel</button>
            <button class="btn primary" @click="save">Save Settings</button>
        </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { useSimulation } from '../composables/useSimulation';

const props = defineProps<{
    isOpen: boolean;
}>();

const emit = defineEmits(['close']);

const { state, setDataBaseUrl, setBrightness, setSimulationMode, setMaxZoom, setZoomSensitivity } = useSimulation();
const baseUrlInput = ref('');
const brightnessInput = ref(1.0);
const simulationModeInput = ref<'simulated' | 'live'>('simulated');
const maxZoomInput = ref(120);
const zoomSensitivityInput = ref(5);

// Sync input with state when modal opens
watch(() => props.isOpen, (newVal) => {
    if (newVal) {
        baseUrlInput.value = state.value.dataBaseUrl || '';
        brightnessInput.value = state.value.brightness ?? 1.0;
        simulationModeInput.value = state.value.simulationMode ?? 'simulated';
        maxZoomInput.value = state.value.maxZoom ?? 120;
        zoomSensitivityInput.value = state.value.zoomSensitivity ?? 5;
    }
});

const close = () => {
    emit('close');
};

const save = () => {
    setDataBaseUrl(baseUrlInput.value);
    setBrightness(Number(brightnessInput.value));
    setSimulationMode(simulationModeInput.value);
    setMaxZoom(Number(maxZoomInput.value));
    setZoomSensitivity(Number(zoomSensitivityInput.value));
    close();
};
</script>

<style scoped>
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
}

.modal {
    background: #1a1a1a;
    border: 3px solid #000;
    width: 400px;
    max-width: 90%;
    color: white;
    font-family: 'Courier New', monospace;
    box-shadow: 10px 10px 0px rgba(0,0,0,0.5);
}

.modal-header {
    background: #FFD700;
    color: black;
    padding: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 3px solid #000;
}

.modal-header h3 {
    margin: 0;
    text-transform: uppercase;
    font-weight: 900;
    font-size: 1.1rem;
}

.close-btn {
    background: black;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
    padding: 0 8px;
    font-weight: bold;
}

.close-btn:hover {
    color: #ff4444;
}

.modal-content {
    padding: 20px;
}

.setting-group {
    margin-bottom: 20px;
}

.setting-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: bold;
    text-transform: uppercase;
    color: #ffd700;
}

.description {
    font-size: 0.8rem;
    color: #888;
    margin-bottom: 8px;
}

.input-field {
    width: 100%;
    padding: 8px;
    background: #000;
    border: 2px solid #333;
    color: white;
    font-family: monospace;
    font-size: 0.9rem;
    box-sizing: border-box; /* Fix padding issue */
}

.input-field:focus {
    outline: none;
    border-color: #FFD700;
}

.modal-actions {
    padding: 16px;
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    border-top: 2px solid #333;
    background: #222;
}

.btn {
    padding: 8px 16px;
    font-family: 'Courier New', monospace;
    text-transform: uppercase;
    font-weight: bold;
    cursor: pointer;
    border: 2px solid #000;
    box-shadow: 2px 2px 0px #000;
    transition: all 0.1s;
}

.btn:active {
    transform: translate(2px, 2px);
    box-shadow: none;
}

.btn.primary {
    background: #FFD700;
    color: black;
}

.btn.primary:hover {
    background: #ffea70;
}

.btn.secondary {
    background: #444;
    color: white;
}

.btn.secondary:hover {
    background: #555;
}

.radio-group {
    display: flex;
    gap: 16px;
    margin-top: 8px;
}

.radio-label {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    font-size: 0.9rem;
}

.slider {
    width: 100%;
    cursor: pointer;
    accent-color: #FFD700;
}
</style>
