<template>
  <div class="wishlist">
    <div class="wishlist__top">
      <div class="wishlist__top-left">
        <ui-checkbox
            id="choose-all"
            v-model="allSelected"
            :isAccent="true"
        >
          {{ t('checkboxes.chooseAll') }}
        </ui-checkbox>
        <p>{{ t('profile.wishlist.total', {quantity: productsInWishlist.length, amount: totalPrice}) }}</p>
      </div>
      <el-tooltip
          :content="t('profile.wishlist.deleteTooltip')"
          placement="top-end"
      >
        <div
            class="wishlist__top-button"
            @click="onBulkRemove"
        >
          <icon name="material-symbols-light:delete-rounded" size="20" />
        </div>
      </el-tooltip>
    </div>
    <div class="wishlist__list">
      <div class="wishlist__list-inner" v-if="productsInWishlist.length">
        <div
            class="wishlist__item"
            v-for="product in productsInWishlist"
            :key="product.node.uuid"
        >
          <ui-checkbox
              :id="`item-${product.node.uuid}`"
              :modelValue="selectedProducts.some(o => o.uuid === product.node.uuid)"
              @update:modelValue="checked => toggleUuid(product.node.uuid, checked)"
              class="wishlist__item-checkbox"
              :isAccent="true"
          />
          <cards-product
              :product="product.node"
              :isList="true"
          />
        </div>
      </div>
      <p class="wishlist__empty">{{ t('profile.wishlist.empty') }}</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import {usePageTitle} from "~/composables/utils";
import {useWishlistOverwrite} from "~/composables/wishlist";

const {t} = useI18n();
const wishlistStore = useWishlistStore();

const { overwriteWishlist } = useWishlistOverwrite();

const productsInWishlist = computed(() => {
  return wishlistStore.wishlist ? wishlistStore.wishlist.products.edges : [];
});
const totalPrice = computed(() => {
  return productsInWishlist.value.reduce((acc, p) => acc + p.node.price, 0);
});

const selectedProducts = ref<{ uuid: string }[]>([]);

const allSelected = computed<boolean>({
  get() {
    const ids = productsInWishlist.value.map(p => p.node.uuid);
    return ids.length > 0
      && ids.every(id => selectedProducts.value.some(o => o.uuid === id));
  },
  set(val: boolean) {
    if (val) {
      selectedProducts.value = productsInWishlist.value
        .map(p => ({ uuid: p.node.uuid }));
    }
    else {
      selectedProducts.value = [];
    }
  }
});

function toggleUuid(uuid: string, checked: boolean) {
  if (checked) {
    if (!selectedProducts.value.some(o => o.uuid === uuid)) {
      selectedProducts.value.push({ uuid });
    }
  }
  else {
    selectedProducts.value = selectedProducts.value
      .filter(o => o.uuid !== uuid);
  }
}

function onBulkRemove() {
  overwriteWishlist({
    type: 'bulk',
    bulkAction: 'remove',
    products: selectedProducts.value
  });
}

const { setPageTitle } = usePageTitle();

setPageTitle(t('breadcrumbs.wishlist'));
</script>

<style lang="scss" scoped>
.wishlist {
  display: flex;
  flex-direction: column;
  gap: 50px;

  &__top {
    width: 100%;
    background-color: $white;
    padding: 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.2);
    border-radius: $default_border_radius;

    &-left {
      display: flex;
      align-items: center;
      gap: 50px;

      & p {
        font-weight: 600;
      }
    }

    &-button {
      cursor: pointer;
      width: fit-content;
      border-radius: $default_border_radius;
      background-color: rgba($error, 0.3);
      border: 1px solid $error;
      padding: 5px 10px;
      display: flex;
      align-items: center;
      gap: 10px;
      transition: 0.2s;
      color: $error;
      font-size: 16px;
      font-weight: 600;

      @include hover {
        background-color: $error;
        color: $white;
      }
    }
  }

  &__list {
    background-color: $white;
    padding: 20px;
    box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.2);
    border-radius: $default_border_radius;

    &-inner {
      display: flex;
      flex-direction: column;
      gap: 20px;
    }
  }

  &__item {
    position: relative;
    display: flex;
    align-items: flex-start;
    gap: 5px;
  }

  &__empty {
    font-weight: 600;
  }
}
</style>