<template>
    <transition name="toast">
        <div
            v-if="visible"
            :class="['toast', `toast--${type}`]"
            @click="close"
        >
            <span class="toast__icon">
                <template v-if="type === 'success'">&#10003;</template>
                <template v-else-if="type === 'error'">&#10005;</template>
                <template v-else-if="type === 'warning'">&#9888;</template>
                <template v-else>&#8505;</template>
            </span>
            <span class="toast__message">{{ message }}</span>
            <button class="toast__close" @click.stop="close">&times;</button>
        </div>
    </transition>
</template>

<script>
export default {
    name: 'Toast',
    props: {
        message: {
            type: String,
            default: ''
        },
        type: {
            type: String,
            default: 'success',
            validator: value => ['success', 'error', 'warning', 'info'].includes(value)
        },
        duration: {
            type: Number,
            default: 3000
        }
    },
    data() {
        return {
            visible: false,
            timer: null
        };
    },
    methods: {
        show() {
            this.visible = true;
            this.startTimer();
        },
        close() {
            this.visible = false;
            this.clearTimer();
            this.$emit('closed');
        },
        startTimer() {
            this.clearTimer();
            if (this.duration > 0) {
                this.timer = setTimeout(() => {
                    this.close();
                }, this.duration);
            }
        },
        clearTimer() {
            if (this.timer) {
                clearTimeout(this.timer);
                this.timer = null;
            }
        }
    },
    beforeDestroy() {
        this.clearTimer();
    }
};
</script>

<style lang="less" scoped>
/* CSS Variables */
.toast {
    --toast-bg-success: #10b981;
    --toast-bg-error: #ef4444;
    --toast-bg-warning: #f59e0b;
    --toast-bg-info: #3b82f6;
    --toast-text-color: #ffffff;
    --toast-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    --toast-border-radius: 0.5rem;
    --toast-padding-x: 1rem;
    --toast-padding-y: 0.75rem;
    --toast-font-size: 0.875rem;
    --toast-icon-size: 1.25rem;
    --toast-z-index: 9999;
}

/* Toast Block */
.toast {
    position: fixed;
    top: 1.5rem;
    right: 1.5rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: var(--toast-padding-y) var(--toast-padding-x);
    border-radius: var(--toast-border-radius);
    box-shadow: var(--toast-shadow);
    color: var(--toast-text-color);
    font-size: var(--toast-font-size);
    z-index: var(--toast-z-index);
    cursor: pointer;
    max-width: 400px;

    /* Modifiers */
    &--success {
        background-color: var(--toast-bg-success);
    }

    &--error {
        background-color: var(--toast-bg-error);
    }

    &--warning {
        background-color: var(--toast-bg-warning);
    }

    &--info {
        background-color: var(--toast-bg-info);
    }

    /* Elements */
    &__icon {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: var(--toast-icon-size);
        flex-shrink: 0;
    }

    &__message {
        flex: 1;
        line-height: 1.4;
    }

    &__close {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 1.5rem;
        height: 1.5rem;
        padding: 0;
        border: none;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 50%;
        color: var(--toast-text-color);
        font-size: 1rem;
        cursor: pointer;
        flex-shrink: 0;
        transition: background-color 0.2s;

        &:hover {
            background: rgba(255, 255, 255, 0.3);
        }
    }
}

/* Transition */
.toast-enter-active,
.toast-leave-active {
    transition: all 0.3s ease;
}

.toast-enter,
.toast-leave-to {
    opacity: 0;
    transform: translateX(100%);
}
</style>
