<script setup lang="ts">
import { computed } from 'vue'

import { type RouterLinkProps, RouterLink } from 'vue-router';
import { type ColorProps, type PastelProps, type RaisedProps, type VariantProps, propsToColor } from '~/composables/colors'

import Pill from './Pill.vue'
import Alert from './Alert.vue'
import Layout from './Layout.vue';
import Spacer from './layout/Spacer.vue';

interface Props extends Partial<RouterLinkProps> {
  title: string
  category?: true | "h1" | "h2" | "h3" | "h4" | "h5"
  image?: string | { src: string, style?: "withPadding" }
  tags?: string[]
}

const props = defineProps<Props & (PastelProps | ColorProps) & RaisedProps & VariantProps>()

const image = typeof props.image === 'string' ? { src: props.image } : props.image

const isExternalLink = computed(() => {
  return typeof props.to === 'string' && props.to.startsWith('http')
})
</script>

<style module>
.card {
  /* Override --width with your preferred value */
  --fw-card-width: var(--width, 320px);
  --fw-card-padding: 24px;

  position: relative;

  color: var(--fw-text-color);
  background-color: var(--fw-bg-color);
  box-shadow: 0 3px 12px 2px rgb(0 0 0 / 20%);

  border-radius: var(--fw-border-radius);
  font-size: 1rem;

  width: var(--fw-card-width);

  >.covering {
    position: absolute;
    inset: 0;

    &~:is(.title, .content) {
      pointer-events:none;
    }

    &:hover~:is(.content) {
      text-decoration: underline
    }
  }

  &:has(>.image) {
    text-align: center;
  }

  >.image {
    overflow: hidden;
    border-radius: var(--fw-border-radius) var(--fw-border-radius) 0 0;
    object-fit: cover;
    width: 100%;
    aspect-ratio: 1;

    &.with-padding {
      margin: var(--fw-card-padding) var(--fw-card-padding) calc(var(--fw-card-padding) / 2) var(--fw-card-padding);
      width: calc(100% - 2 * var(--fw-card-padding));
      border-radius: var(--fw-border-radius);
    }
  }

  >.title {
    padding: 0 var(--fw-card-padding);
    line-height: 1.3em;
    font-size: 1.125em;
    font-weight: bold;

    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
  }

  &.is-category>.title {
    font-size: 1.75em;
    padding-bottom: .25em;
  }

  >.alert {
    padding-left: var(--fw-card-padding);
    padding-right: var(--fw-card-padding);
  }

  >.tags {
    /* Tags have an inherent padding which we offset here: */
    padding: 0 calc(var(--fw-card-padding) - 12px);
  }

  >.content {
    padding: 0 var(--fw-card-padding);
    /* Consider making all line height, vertical paddings, margins and borders,
    a multiple of a global vertical rhythm so that side-by-side lines coincide */
    line-height: 24px;
  }

  >.footer {
    padding: calc(var(--fw-card-padding) - 4px);
    display: flex;
    align-items: center;
    font-size: 0.8125rem;

    >.options-button {
      margin-left: auto;
    }
  }

  >.action {
    display: flex;
    background: color-mix(in oklab, var(--fw-bg-color) 80%, var(--fw-gray-500));
    border-bottom-left-radius: var(--fw-border-radius);
    border-bottom-right-radius: var(--fw-border-radius);

    >*:not(.with-padding) {
      margin: 0;
      flex-grow: 1;
      border-top-left-radius: 0;
      border-top-right-radius: 0;
      border: 8px solid transparent;

      &:not(:first-child) {
        border-bottom-left-radius: 0;

      }

      &:not(:last-child) {
        border-bottom-right-radius: 0;
      }
    }
  }
}
</style>

<template>
  <Layout stack
    :class="{ [$style.card]: true, [$style['is-category']]: category }"
    style="--gap:16px"
    v-bind="propsToColor({...(props.to? { interactive: true, solid: true, default: true } : {}), ...props})"
  >

    <!-- Link -->
    <a v-if="props.to && isExternalLink" :class="$style.covering" :href="to?.toString()" target="_blank" />
    <RouterLink v-if="props.to && !isExternalLink" :class="$style.covering" :to="props.to"
    />

    <!-- Image -->
    <div v-if="$slots.image" :class="$style.image">
      <slot name="image" :src="image" />
    </div>
    <img v-else-if="image" :src="image?.src"
      :class="{ [$style.image]: true, [$style['with-padding']]: image?.style === 'withPadding' }" />
    <Spacer v-else style="--size:12px" />

    <!-- Content -->
    <component :class="$style.title" :is="typeof category === 'string' ? category : 'h6'">{{ title }}</component>

    <Alert blue v-if="$slots.alert" :class="$style.alert">
      <slot name="alert" />
    </Alert>

    <div v-if="tags" :class="$style.tags">
      <Pill v-for="tag in tags" :key="tag">
        #{{ tag }}
      </Pill>
    </div>

    <div v-if="$slots.default" :class="$style.content">
      <slot />
    </div>

    <!-- Footer and Action -->
    <div v-if="$slots.footer" :class="$style.footer">
      <slot name="footer" />
    </div>

    <div v-if="$slots.action" :class="$style.action">
      <slot name="action" />
    </div>
    <Spacer v-else-if="!$slots.footer" style="--size:16px" />

  </Layout>
</template>
