<template>
    <header 
        v-if="userAgentInfo === 'web'"
        class="header"
        :class="{ 'is-scrolled': isScrolled, '-no-topbar': !topbarData?.length }"
    >
        <div 
            v-if="topBarActive && topbarData?.length"
            class="header__topbar" 
        >
            <div class="container">
            <Transition name="list" mode="out-in">
                <NuxtLink 
                    v-if="topbarData?.[currentEventIndex]"
                    :key="currentEventIndex"
                    :to="topbarData[currentEventIndex]?.link" 
                    :target="topbarData[currentEventIndex]?.is_external ? '_blank' : '_self'"
                >
                    <div class="header__topbar-icon" v-if="topbarData[currentEventIndex]?.highlighted_text">
                        {{ topbarData[currentEventIndex]?.highlighted_text }}
                    </div>
                    <div class="header__topbar-text">
                        <template v-if="topbarData[currentEventIndex]?.description">
                            {{ topbarData[currentEventIndex]?.description }}
                        </template>
                        <span v-if="topbarData[currentEventIndex]?.button_text">
                            {{ topbarData[currentEventIndex]?.button_text }}
                        </span>
                    </div>
                </NuxtLink>
            </Transition>
            </div>
        </div>
        <div class="header__wrap">
            <div class="container">
                <HeaderLogo class="header__logo" />
                <div class="header__right">
                    <NuxtLink class="header__search" :to="searchLink" >
                        <NuxtIcon name="search" />
                    </NuxtLink>

                    <div 
                        class="header__navopen"
                        @click="navOpen = true"
                    >
                        <NuxtIcon name="menu" />
                    </div>
                </div>
                <div 
                    class="header__navigation-bg" 
                    :class="{'-open': navOpen, '-height': menuData?.blocks[1]?.fields?.event_list?.length > 0}" 
                    @click="navOpen = false" 
                />
                <div 
                    v-if="menuData"
                    class="header__navigation"
                    :class="{'-open': navOpen}"
                >
                    <div 
                        class="header__navigation-close"
                        @click="navOpen = false"
                    >
                        <NuxtIcon name="cross" />
                    </div>
                    <HeaderNavigation 
                        :menuData="menuData"
                        @update:navOpen="navOpen = false"
                    />
                </div>
            </div>
        </div>
    </header>
</template>
<script setup>
    const isScrolled = ref(false)
    const navOpen = ref(false)
    const userAgentInfo = useUserAgentInfo()
    const route = useRoute()

    const pathTypeMap = [
        { prefix: '/stock', type: 'stock' },
        { prefix: '/fund',  type: 'fund'  },
        { prefix: '/etf',   type: 'etf'   },
        { prefix: '/bond',  type: 'bond'  },
    ]
    const searchLink = computed(() => {
        const match = pathTypeMap.find(({ prefix }) => route.path.startsWith(prefix))
        return match ? `/search?from=${match.type}` : '/search'
    })
    const topBarActive = useTopBarActive()
    const currentEventIndex = ref(0)
    const topbarData = ref(null)
    const siteInfoData = useSiteInfoData()
    const menuData = siteInfoData?.value?.header
    
    onMounted(() => {
        
        topbarData.value = menuData?.blocks[1]?.fields?.event_list

        topBarActive.value = userAgentInfo.value === 'web'
        
        window.addEventListener('scroll', () => {
            const scrollY = window.scrollY
            if (scrollY > 20) {
                isScrolled.value = true
            } else {
                isScrolled.value = false
            }
        })

        // 設定輪播
        if(topbarData.value?.length > 1) {
            setInterval(() => {
                currentEventIndex.value = (currentEventIndex.value + 1) % topbarData.value.length
            }, 4000)
        }
    })
</script>
<style lang="scss">
$class-name: header;
.#{$class-name} {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 100;
    animation: fadeIn .3s both;

    &__topbar {
        @include size(100%, auto);
        background-color: map-get($colors, black-2);

        .container {
            a {
                @include typo('head', 12);
                font-size: 1.5rem;
                padding-top: 1.5rem;
                padding-bottom: 1.5rem;
                color: #fff;
                display: flex;
                justify-content: flex-start;
                align-items: center;
                gap: 1rem;

                @include media-breakpoint-down(medium) {
                    gap: .4rem;
                    font-size: 1.3rem;
                }
            }
        }

        &-icon {
            @include typo('body', 12);
            background-color: #fff;
            color: #000;
            border-radius: 10rem;
            padding: .4rem 1.2rem .2rem;

            @include media-breakpoint-down(medium) {
                padding: .4rem 1.2rem;
            }
        }

        .nuxt-icon {
            svg {
                @include size(2.2rem);
            }
        }

        &-text {
            @include typo('head', 12);

            > span {
                text-decoration: underline;

                @include media-breakpoint-down(medium) {
                    display: none;
                }
            }
        }
    }

    &__right {
        order: 2;
        display: flex;
        align-items: center;
        gap: 1rem;
    }

    &__logo {
        padding-right: 1rem;
        margin-left: -1rem;
    }

    &__search {
        @include size(3rem);
        font-size: 1.8rem;
        border-radius: 10rem;
        background-color: map-get($colors, gray-3);
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all .2s;
        
        @include media-breakpoint-up(tablet) {
            &:hover {
                color: #fff;
                background-color: map-get($colors, black-1);
            }
        }

        svg {
            margin-bottom: .4rem;
        }
    }

    &__wrap {
        position: relative;

        &::before {
            @include size(100%);
            
            content: '';
            top: 0;
            left: 0;
            position: absolute;
            background: rgba(255, 255, 255, .8);
            backdrop-filter: blur(10px);
            // transition: transform .3s;
            // transform: scaleY(0);
            // transform-origin: 0 0;
        }

        > .container {
            display: flex;
            align-items: center;
            justify-content: space-between;
            position: relative;
        }
    }

    &.is-scrolled {
        // .#{$class-name}{
        //     &__wrap::before {
        //         transform: scaleY(1);
        //     }
        // }
    }

    &__navopen {
        display: none;
        cursor: pointer;
        padding: 1rem 0 1rem 1rem;

        .nuxt-icon {
            svg {
                @include size(2.4rem);
            }
        }

        @include media-breakpoint-down(medium) {
            display: block;
        }
    }

    &__navigation {
        flex-grow: 1;

        @include media-breakpoint-down(medium) {
            @include size(23rem, calc(100% - 5.4rem));

            position: fixed;
            right: 0;
            top: 5.4rem;
            z-index: 999;
            background: #fff;
            border-radius: 2rem 0 0 0;
            padding: 2.4rem;
            transform: translate3d(101%,0,0);
            transition: transform .6s cubic-bezier(0.87, 0, 0.13, 1);

            &.-open {
                transform: translate3d(0,0,0);
            }
        }

        &-close {
            display: none;
            padding: 0;
            margin-bottom: 1rem;

            .nuxt-icon {
                svg {
                    @include size(2.4rem);
                }
            }

            @include media-breakpoint-down(medium) {
                display: block;
            }
        }

        &-bg {
            @include size(100%);

            position: fixed;
            top: 0;
            left: 0;
            z-index: 99;
            background: rgba(0, 0, 0, .5);
            display: none;

            @include media-breakpoint-down(medium) {
                @include size(100%);

                top: 0;
                display: block;
                cursor: pointer;
                pointer-events: none;
                opacity: 0;
                transition: opacity .3s;

                &.-height {
                    @include size(100%, calc(100% - 5rem));

                    top: 5rem;
                }

                &.-open {
                    pointer-events: auto;
                    opacity: 1;
                }
            }
        }

        .nav {
            display: flex;
            align-items: center;
            justify-content: space-between;
            opacity: 0;
            animation: fadeInDown .3s .6s both;

            @include media-breakpoint-down(medium) {
                flex-direction: column;
                align-items: flex-start;
            }

            &__left {
                display: flex;
                gap: 2rem;
                padding-left: 2rem;
                border-left: 1px solid map-get($colors, gray-70);

                @include media-breakpoint-down(medium) {
                    flex-direction: column;
                    border-left: 0;
                    padding-left: 0;
                }
            }

            &__right {
                display: flex;
                align-items: center;
                gap: 2rem;

                @include media-breakpoint-down(medium) {
                    @include size(100%, auto);

                    flex-direction: column;
                    align-items: flex-start;
                    gap: 1rem;

                    > p {
                        font-size: 1.2rem;
                        color: map-get($colors, gray-70);
                    }
                }

                // shareshare icon
                > a {
                    .nuxt-icon {
                        svg {
                            @include size(12rem, auto);
                        }
                    }
                }

                &-btn {
                    @include media-breakpoint-down(medium) {
                        @include size(100%, auto);
                        
                        margin-top: 1.4rem;
                        padding-top: 2.4rem;
                        border-top: 1px solid map-get($colors, gray-10);
                    }
                    a {
                        display: flex;
                        gap: .8rem;
                        border-radius: 10rem;
                        padding: .7rem 1.9rem .7rem 2.2rem;
                        background-color: map-get($colors, gray-10);
                        transition: all .3s;

                        @include media-breakpoint-down(medium) {
                            padding: 0;
                            background: none;
                        }

                        > span {
                            &:first-child {
                                margin-top: .1rem;
                            }
                        }

                        .nuxt-icon {
                            @include size(2.2rem, auto);
                            
                            transition: transform .3s;
                            color: map-get($colors, red-50);
                            flex: 0 0 auto;

                            > svg {
                                @include size(100%, auto);
                            }
                        }


                        @include media-breakpoint-up(medium) {

                            &:hover {
                                color: #fff;
                                background: map-get($colors, red-50);

                                .nuxt-icon {
                                    color: #fff;
                                    transform: translateX(.5rem);
                                }
                            }

                        }
                    }

                    > p {
                        display: none;

                        @include media-breakpoint-down(medium) {
                            display: block;
                            font-size: 1.2rem;
                            color: map-get($colors, gray-70);
                        }
                    }
                }
            }

            &.navshares {
                .nav {
                    &__right {
                        @include media-breakpoint-down(medium) {
                            // border-top: 1px solid map-get($colors, gray-10);
                            padding: 2.3rem 0;
                        }
                    }
                }
            }
        }
    }

    &.-no-topbar {
        @include media-breakpoint-down(medium) {
            .header__navigation {
                top: 0;
                height: 100%;

                &.-height {
                    top: 5rem;
                }
            }
        }
    }
}
</style>