<template>
  <div
    class="brand-selector"
    :class="{
      'brand-selector__rotate': showBrandsDropdown,
      'brand-selector__height-auto': showBrandsDropdown
    }"
    @click="showBrandsDropdown = !showBrandsDropdown"
  >
    <div
      v-if="isSvg($_getContentImage(currentBrand.menu_icon))"
      class="brand-icon-container"
      v-html="$_getContentImage(currentBrand.menu_icon)"
    />

    <div v-else class="brand-icon-container">
      <img
        class="band-selector__current-brand"
        :src="$_getContentImage(currentBrand.menu_icon)"
        :alt="currentBrand.title"
      />
    </div>

    <Icon
      :icon="showBrandsDropdown ? 'arrow-down' : 'arrow-right'"
      :color="$_theme('colorGrey400')"
      size="xs"
      class="arrow-selector"
    />

    <div v-if="showBrandsDropdown" class="brand-selector__dropdown">
      <div
        v-for="(brand, index) in brands"
        :key="index"
        class="brand-selector__dropdown-item"
      >
        <template v-if="brand.title !== currentBrand.title">
          <div
            v-if="isSvg($_getContentImage(brand.menu_icon))"
            class="brand-icon-container"
            @click="attributeBrand(brand)"
            v-html="$_getContentImage(brand.menu_icon)"
          />

          <img
            v-else
            :src="$_getContentImage(brand.menu_icon)"
            :alt="brand.title"
            @click="attributeBrand(brand)"
          />
        </template>
      </div>
    </div>
  </div>
</template>

<script>
import BrandsMixin from '~instanceCustom/mixins/brands'
import { storage } from '@inside-library/helpers/Storage'
import { formatHref } from '@inside-library/helpers/Langs'
import { LHG_GROUP_CODE } from '../../constants'

import { Icon } from 'inside-collection/atoms'

export default {
  name: 'BrandSelector',
  components: { Icon },
  mixins: [BrandsMixin],
  data() {
    return {
      brands: [],
      currentBrand: '',
      showBrandsDropdown: false
    }
  },
  computed: {
    getURLsInfos() {
      return this.$store.getters.getURLsInfos
    }
  },
  async mounted() {
    this.brands = await this.getBrands()

    const currentBrandTitle = storage.get('brand')?.title

    this.currentBrand = this.brands.find(
      brand => currentBrandTitle === brand.title
    )
  },
  methods: {
    attributeBrand(brand) {
      /*
       * Save in store
       */
      this.setStoreAction('setSpecificUserInfos', {
        key: 'brand',
        value: brand
      })

      /*
       * Save in local storage
       */
      storage.set('brand', brand)

      let url = '/'

      if (brand.code === LHG_GROUP_CODE) {
        url = this.$router.resolve(
          formatHref(
            {
              name: 'LHGPage'
            },
            this.$_currentLang
          )
        ).href
      }

      window.location.href = url
    },
    isSvg(img) {
      if (img) {
        return img.indexOf('<svg') > -1
      }
      return img
    }
  }
}
</script>

<style lang="scss">
@use '@inside-library/styles/mixins/partials/common-mixin' as *;

div.brand-selector {
  cursor: pointer;
  position: absolute;
  top: 0;
  right: 0;
  width: 5.4rem;
  height: 3rem;
  background-color: var(--color-light);
  border: 1px solid var(--color-grey);
  border-radius: 1.5rem;
  transition: all 0.3s ease;
  z-index: 10;

  @include fontSize(10);

  & > .brand-icon-container {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: -6px;
    text-align: center;

    svg {
      max-height: 22px;
    }
  }

  .brand-icon-container {
    width: 100%;
    height: 100%;

    svg {
      width: 36px;
      height: 28px;
    }

    img {
      max-width: 23px;
    }
  }

  &__height-auto {
    height: inherit;
  }

  &__current-brand {
    position: absolute;
    top: 0.8rem;
    left: 0.5rem;
    width: 1.7rem;

    @include fontSize(10);
  }

  .arrow-selector {
    position: absolute;
    top: 3px;
    right: 2px;
    color: var(--color-grey);
    font-weight: bold;
    transition: all 0.3s ease;

    @include fontSize(12);
  }

  &__dropdown {
    width: calc(100% + 2px);
    padding-top: 2rem;
    padding-bottom: 1rem;
    margin-top: -1.4rem;
    margin-left: -1px;
    background-color: var(--color-light);
    border: 1px solid var(--color-grey);
    border-top-color: transparent;
    border-radius: 0 0 1.5rem 1.5rem;
    overflow: hidden;
    z-index: 4;

    @include mobile {
      margin-top: -1rem;
      background-color: transparent;
      border-color: transparent;
    }

    &-item {
      text-align: center;

      img {
        position: relative;
        padding: 1rem 0;

        &:last-of-type {
          padding-bottom: 0;
        }
      }
    }
  }
}
</style>
