<template>
    <div v-if="show" class="modal-overlay" @click="handleOverlayClick">
        <div class="modal-dialog" @click.stop>
            <!-- Modal Header -->
            <div class="modal-header">
                <h4 class="modal-title">{{ title }}</h4>
                <button type="button" class="modal-close" @click="closeModal" aria-label="Close">
                    <span class="material-icons">close</span>
                </button>
            </div>

            <!-- Modal Body -->
            <div class="modal-body">
                <!-- Loading State -->
                <div v-if="loading" class="loading-container">
                    <div class="loading-spinner"></div>
                    <p>Content wordt geladen...</p>
                </div>

                <!-- Error State -->
                <div v-else-if="error" class="error-container">
                    <div class="error-icon">
                        <span class="material-icons">error_outline</span>
                    </div>
                    <h5>Fout bij het laden van content</h5>
                    <p>{{ errorMessage }}</p>
                    <button type="button" class="btn btn-primary" @click="retryLoad">
                        Opnieuw proberen
                    </button>
                </div>

                <!-- Content Display -->
                <div v-else-if="content" class="content-container">
                    <div class="content-body" v-html="renderedContent"></div>
                    <div v-if="version" class="content-version">
                        Versie: {{ version }}
                    </div>
                </div>

                <!-- Empty State -->
                <div v-else class="empty-container">
                    <p>Geen content beschikbaar.</p>
                </div>
            </div>

            <!-- Modal Footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" @click="closeModal">
                    Sluiten
                </button>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'ContentModal',
    props: {
        show: {
            type: Boolean,
            default: false
        },
        title: {
            type: String,
            default: 'Content'
        },
        content: {
            type: String,
            default: ''
        },
        version: {
            type: String,
            default: ''
        },
        loading: {
            type: Boolean,
            default: false
        },
        error: {
            type: Boolean,
            default: false
        },
        errorMessage: {
            type: String,
            default: 'Er is een fout opgetreden bij het laden van de content.'
        }
    },
    emits: ['close', 'retry'],
    computed: {
        renderedContent() {
            if (!this.content) return '';
            
            // Use marked library if available (same pattern as SideBarExplanation)
            if (typeof window.marked === 'function') {
                return window.marked(this.content);
            } else if (window.marked && typeof window.marked.parse === 'function') {
                return window.marked.parse(this.content);
            } else {
                console.warn('Marked library not available, falling back to basic parsing');
                // Fallback to basic regex-based parsing if marked is not available
                return this.content
                    .replace(/^### (.*$)/gim, '<h3>$1</h3>')
                    .replace(/^## (.*$)/gim, '<h2>$1</h2>')
                    .replace(/^# (.*$)/gim, '<h1>$1</h1>')
                    .replace(/^\* (.*$)/gim, '<li>$1</li>')
                    .replace(/\*\*(.*)\*\*/gim, '<strong>$1</strong>')
                    .replace(/\*(.*)\*/gim, '<em>$1</em>')
                    .replace(/\n\n/gim, '</p><p>')
                    .replace(/^(?!<[h|l|p])/gim, '<p>')
                    .replace(/(?<![h|l|p]>)$/gim, '</p>')
                    .replace(/<p><\/p>/gim, '')
                    .replace(/<p>(<h[1-6]>)/gim, '$1')
                    .replace(/(<\/h[1-6]>)<\/p>/gim, '$1')
                    .replace(/<p>(<li>)/gim, '<ul>$1')
                    .replace(/(<\/li>)<\/p>/gim, '$1</ul>');
            }
        }
    },
    methods: {
        closeModal() {
            this.$emit('close');
        },
        handleOverlayClick() {
            // Close modal when clicking on overlay (outside the dialog)
            this.closeModal();
        },
        retryLoad() {
            this.$emit('retry');
        },
        handleEscapeKey(event) {
            if (event.key === 'Escape' && this.show) {
                this.closeModal();
            }
        }
    },
    mounted() {
        // Add escape key listener
        document.addEventListener('keydown', this.handleEscapeKey);
    },
    beforeUnmount() {
        // Remove escape key listener
        document.removeEventListener('keydown', this.handleEscapeKey);
    }
};
</script>

<style scoped>
/* Modal Overlay */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    padding: 20px;
    box-sizing: border-box;
}

/* Modal Dialog */
.modal-dialog {
    background: white;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    width: 80vw;
    height: min(80vh, calc(100vh - 120px));
    max-height: calc(100vh - 120px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Modal Header */
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid #e0e0e0;
    background-color: #f8f9fa;
}

.modal-title {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: #333;
}

.modal-close {
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #666;
    transition: background-color 0.2s, color 0.2s;
}

.modal-close:hover {
    background-color: #e9ecef;
    color: #333;
}

.modal-close .material-icons {
    font-size: 20px;
}

/* Modal Body */
.modal-body {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    min-height: 200px;
}

/* Loading State */
.loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    text-align: center;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid #007bff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 16px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Error State */
.error-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    text-align: center;
}

.error-icon {
    margin-bottom: 16px;
}

.error-icon .material-icons {
    font-size: 48px;
    color: #dc3545;
}

.error-container h5 {
    margin: 0 0 12px 0;
    color: #dc3545;
    font-weight: 600;
}

.error-container p {
    margin: 0 0 20px 0;
    color: #666;
}

/* Content Display */
.content-container {
    line-height: 1.6;
}

.content-body {
    color: #333;
    font-size: 14px;
}

.content-body h1,
.content-body h2,
.content-body h3 {
    margin-top: 24px;
    margin-bottom: 12px;
    color: #333;
}

.content-body h1 {
    font-size: 24px;
    border-bottom: 2px solid #e0e0e0;
    padding-bottom: 8px;
}

.content-body h2 {
    font-size: 20px;
}

.content-body h3 {
    font-size: 16px;
}

.content-body p {
    margin-bottom: 12px;
}

.content-body ul {
    margin: 12px 0;
    padding-left: 20px;
}

.content-body li {
    margin-bottom: 4px;
}

.content-body strong {
    font-weight: 600;
}

.content-version {
    margin-top: 20px;
    padding-top: 12px;
    border-top: 1px solid #e0e0e0;
    font-size: 12px;
    color: #666;
    text-align: right;
}

/* Empty State */
.empty-container {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 40px 20px;
    text-align: center;
    color: #666;
}

/* Modal Footer */
.modal-footer {
    padding: 16px 20px;
    border-top: 1px solid #e0e0e0;
    background-color: #f8f9fa;
    display: flex;
    justify-content: flex-end;
}

/* Button Styles */
.btn {
    padding: 8px 16px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: background-color 0.2s, border-color 0.2s;
    text-decoration: none;
    display: inline-block;
    text-align: center;
}

.btn-primary {
    background-color: #007bff;
    color: white;
}

.btn-primary:hover {
    background-color: #0056b3;
}

.btn-secondary {
    background-color: #6c757d;
    color: white;
}

.btn-secondary:hover {
    background-color: #545b62;
}

/* Responsive Design */
@media (max-width: 768px) {
    .modal-overlay {
        padding: 10px;
        bottom: 80px;
    }
    
    .modal-dialog {
        width: 95vw;
        height: min(95vh, calc(100vh - 60px));
        max-height: calc(100vh - 60px);
    }
    
    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 12px 16px;
    }
    
    .modal-title {
        font-size: 16px;
    }
}
</style>