<template>
  <div>
    <div
      v-if="showBanner"
      class="wg-banner-popup"
    >
      <div class="wg-banner-popup__dialog">
        <div class="wg-banner-popup__header">
          <div class="wg-banner-popup__title">
            <i class="fa fa-bell wg-banner-popup__title-icon" />
            <span>
              {{ bannerTitle }}
            </span>
          </div>
          <Button
            type="error"
            size="small"
            class="wg-banner-popup__close"
            @click="closeBanner"
          >
            <i class="fa fa-times" />
          </Button>
        </div>
        <div class="wg-banner-popup__content">
          <div
            class="wg-banner-popup__media"
            @click="openExternalUrl(bannerUrl)"
          >
            <img
              :src="bannerImage"
              class="wg-banner-popup__image"
            >
          </div>

          <div class="wg-banner-popup__text">
            <div
              v-if="bannerHtml"
              v-html="bannerHtml"
            />
            <div v-else>
              {{ bannerSubTitle }}
            </div>
          </div>

          <div class="wg-banner-popup__actions">
            <Button
              type="primary"
              size="large"
              class="wg-banner-popup__cta"
              @click="openExternalUrl(bannerUrl)"
            >
              {{ bannerButton }}
            </Button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useStorage } from '@vueuse/core';
import { STORAGE_KEYS } from '@/constant/storage';

export default {
  name: 'BannerPopup',
  data() {
    return {
      bannerTitle: '',
      bannerSubTitle: '',
      bannerHtml: '',
      bannerButton: '',
      bannerUrl: '',
      bannerClosed: true,
    };
  },
  computed: {
    showBanner() {
      return !this.bannerClosed;
    },
  },
  methods: {
    closeBanner() {
      this.bannerClosed = true;
    },
    openExternalUrl(url) {
      window.open(url, '_blank');
    },
  },
  created() {
    const bannerPopup = this.getNotifyBanner;

    if (!(bannerPopup && bannerPopup.key && bannerPopup.show)) {
      return false;
    }

    this.bannerClosed = useStorage(STORAGE_KEYS.LOCAL_STORAGE.BANNER_POPUP(bannerPopup.key), false);
    this.bannerUrl = bannerPopup.url;
    this.bannerTitle = bannerPopup.title;
    this.bannerSubTitle = bannerPopup.sub_title || '';
    this.bannerHtml = bannerPopup.html_title || '';
    this.bannerImage = bannerPopup.image;
    this.bannerButton = bannerPopup.button || this.$t('BANNER_BUTTON_OPEN_TEXT');
  },
};
</script>

<style lang="scss" scoped>
@import '@/vue/styles/tokens.scss';

.wg-banner-popup {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(31, 35, 41, 0.45);

  &__dialog {
    width: 100%;
    max-width: 650px;
    max-height: 95vh;
    overflow-x: hidden;
    overflow-y: auto;
    background: $c-surface;
    border: 1px solid $c-border;
    border-radius: $radius-card;
    box-shadow: $shadow-card-lg;
    animation: wg-banner-popup-slide-in 0.5s ease-out;

    @include scrollbar(8px);
  }

  &__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 20px;
    border-bottom: 1px solid $c-border;
  }

  &__title {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
    font-size: 15px;
    font-weight: 600;
    color: $c-text-strong;

    span {
      @include ellipsis;
    }
  }

  &__title-icon {
    flex-shrink: 0;
    color: $c-warning;
  }

  &__close.ivu-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 30px;
    min-width: 30px;
    height: 30px;
    padding: 0;
    color: $c-text-muted;
    background: transparent;
    border-color: transparent;
    border-radius: 50%;
    transition: background $motion-fast, color $motion-fast;

    &:hover,
    &:focus {
      color: $c-error;
      background: $c-error-bg;
      border-color: transparent;
    }
  }

  &__content {
    padding: 16px;
    color: $c-text;
    text-align: center;
  }

  &__media {
    overflow: hidden;
    cursor: pointer;
    border-radius: $radius-item;
    transition: box-shadow $motion-base;

    &:hover {
      box-shadow: $shadow-hover;
    }

    &:hover .wg-banner-popup__image {
      transform: scale(1.06);
    }
  }

  &__image {
    display: block;
    width: 100%;
    height: auto;
    border-radius: $radius-item;
    transition: transform $motion-base;
  }

  &__text {
    margin: 20px 0;
    font-size: 14px;
    line-height: 1.6;
    color: $c-text;
  }

  &__actions {
    margin-bottom: 10px;
  }

  &__cta.ivu-btn {
    height: auto;
    padding: 10px 25px;
    font-size: 15px;
    font-weight: 600;
    color: $c-surface;
    background: $c-brand;
    border-color: $c-brand;
    border-radius: $radius-pill;
    transition: background $motion-base, border-color $motion-base, box-shadow $motion-base,
      transform $motion-base;

    &:hover,
    &:focus {
      background: $c-brand-hover;
      border-color: $c-brand-hover;
      box-shadow: $shadow-hover;
      transform: translateY(-1px);
    }
  }
}

@keyframes wg-banner-popup-slide-in {
  from {
    opacity: 0;
    transform: translateY(-50px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .wg-banner-popup {
    padding: 10px;

    &__dialog {
      max-width: none;
    }
  }
}
</style>
