<template>
    <div v-if="visible" :class="['dialog-overlay', { 'non-modal': false }]">
        <div class="dialog" :class="{ minimized: minimized }" :style="{ width: dialogWidth, height: dialogHeight }">
            <div class="dialog-header">
                <span class="dialog-title">{{ getHeadLine() }}</span>
                <span class="dialog-close" @click="close">✖</span>
            </div>
            <div class="dialog-body">
                {{ message }}
            </div>
            <div class="dialog-footer">
                <button @click="close()" class="dialog-button">Ok</button>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'MessageboxWidget',
    props: {
        type: {
            type: String,
            required: true
        },
        width: {
            type: String,
            default: '300px'
        },
        height: {
            type: String,
            default: '200px'
        },
        message: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            visible: false,
            minimized: false
        };
    },
    computed: {
        dialogWidth() {
            return this.width || '70%';
        },
        dialogHeight() {
            return this.height || '60%';
        }
    },
    watch: {
        visible(newValue) {
            if (!newValue) {
                this.minimized = false;  
            }
        }
    },
    methods: {
        open() {
            this.visible = true;
            if (this.modal === false) {
                this.$store.dispatch('dialogs/addOpenDialog', {
                    status: 'open',
                    dialog: this
                });
            }
        },
        close() {
            this.visible = false;
            this.$store.dispatch('dialogs/removeOpenDialog', this.name);
        },
        getHeadLine() {
            switch (this.type) {
                case 'error':
                    return this.$t('error-title');
                case 'warning':
                    return this.$t('warning-title');
                case 'info':
                    return this.$t('info-title');
                default:
                    return this.$t('info-title');
            }
        }
    },
    mounted() {
        this.$store.subscribe((mutation) => {
            if (mutation.type === 'dialogs/toggleDialogMinimize' && mutation.payload === this.name) {
                this.minimized = !this.minimized;
            }
        });
    }
};
</script>

<style scoped>
.dialog-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    background: rgba(24, 18, 11, 0.44);
    backdrop-filter: blur(10px);
}

.dialog-overlay.non-modal {
    background: transparent;
    pointer-events: none;
}

.dialog {
    background: linear-gradient(180deg, rgba(255, 252, 247, 0.98) 0%, rgba(249, 242, 232, 0.98) 100%);
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-medium);
    border-radius: var(--radius-lg);
    border: 1px solid rgba(93, 64, 55, 0.12);
    pointer-events: all;
    overflow: hidden;
}

.dialog.minimized {
    height: auto;
    overflow: hidden;
}

.dialog-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid rgba(93, 64, 55, 0.1);
    background: linear-gradient(180deg, rgba(248, 162, 43, 0.16) 0%, rgba(255, 255, 255, 0.56) 100%);
}

.dialog-icon {
    margin-right: 10px;
}

.dialog-title {
    flex-grow: 1;
    font-size: 1.08rem;
    font-weight: 800;
    color: var(--color-text-primary);
}

.dialog-close,
.dialog-minimize {
    cursor: pointer;
    font-size: 1.1rem;
    margin-left: 0;
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    color: var(--color-text-secondary);
}

.dialog-body {
    flex-grow: 1;
    padding: 18px 20px;
    overflow-y: auto;
    color: var(--color-text-primary);
}

.dialog-footer {
    display: flex;
    justify-content: flex-end;
    padding: 14px 20px 18px;
    border-top: 1px solid rgba(93, 64, 55, 0.08);
    background: rgba(255, 255, 255, 0.46);
}

.dialog-button {
    margin-left: 0;
    min-height: 38px;
}

.dialog-button:hover {
    color: #2b1f14;
}
</style>
