<template>
  <span
    class="Avatar"
    :class="{ '-compact': compact }"
  >
    <div
      v-if="user && user.is_cat && canShowEars && !showPlaceholder && earsReady"
      class="ears"
      :style="{'color': earsColor}"
    >
			<div class="earLeft" />
			<div class="earRight" />
		</div>
    <StillImage
      v-if="user"
      class="avatar"
      :alt="user.screen_name_ui"
      :title="user.screen_name_ui"
      :src="imgSrc(user.profile_image_url_original)"
      :image-load-error="imageLoadError"
      :class="{ '-compact': compact, '-better-shadow': betterShadow }"
    />
    <div
      v-else
      class="avatar -placeholder"
      :class="{ '-compact': compact }"
    />

    <FAIcon
      v-if="bot"
      icon="robot"
      class="bot-indicator"
    />
  </span>
</template>

<script src="./user_avatar.js"></script>
<style lang="scss">
@import '../../_variables.scss';

/* ears ripped from https://github.com/ihateblueb/aster/blob/main/packages/frontend/src/lib/components/Avatar.svelte */
.Avatar {
  --_avatarShadowBox: var(--avatarStatusShadow);
  --_avatarShadowFilter: var(--avatarStatusShadowFilter);
  --_avatarShadowInset: var(--avatarStatusShadowInset);
  // --_still-image-label-visibility: hidden;

  display: inline-block;
  position: relative;
  width: 48px;
  height: 48px;

  &:hover {
    .ears {
      .earLeft {
        animation: earwiggleleft 1s infinite;
      }
      .earRight {
        animation: earwiggleright 1s infinite;
      }
    }
  }

  &.-compact {
    width: 32px;
    height: 32px;
    border-radius: $fallback--avatarAltRadius;
    border-radius: var(--avatarAltRadius, $fallback--avatarAltRadius);
  }

  .avatar {
    width: 100%;
    height: 100%;
    box-shadow: var(--_avatarShadowBox);
    border-radius: $fallback--avatarRadius;
    border-radius: var(--avatarRadius, $fallback--avatarRadius);

    &.-better-shadow {
      box-shadow: var(--_avatarShadowInset);
      filter: var(--_avatarShadowFilter);
    }

    &.-animated::before {
      display: none;
    }

    &.-compact {
      border-radius: $fallback--avatarAltRadius;
      border-radius: var(--avatarAltRadius, $fallback--avatarAltRadius);
    }

    &.-placeholder {
      background-color: $fallback--fg;
      background-color: var(--fg, $fallback--fg);
    }
  }

  img {
    width: 100%;
    height: 100%;
  }

  .bot-indicator {
    position: absolute;
    bottom: 0;
    right: 0;
    margin: -0.2em;
    padding: 0.2em;
    background: rgba(127, 127, 127, 0.5);
    color: #fff;
    border-radius: var(--tooltipRadius);
  }

  .ears {
    contain: strict;
    position: absolute;
    z-index: 0;
    display: flex;

    top: -50%;
    left: -50%;
    width: 100%;
    height: 100%;
    padding: 50%;

    pointer-events: none;

    .earLeft,
    .earRight {
      contain: strict;
      display: inline-block;
      height: 50%;
      width: 50%;
      background: currentColor;

      &::after {
        contain: strict;
        content: '';
        display: block;
        width: 60%;
        height: 60%;
        margin: 20%;
        background: #df548f;
      }
    }

    .earLeft {
      transform: rotate(37.5deg) skew(30deg);

      &,
      &::after {
        border-radius: 25% 75% 75%;
      }
    }

    .earRight {
      transform: rotate(-37.5deg) skew(-30deg);

      &,
      &::after {
        border-radius: 75% 25% 75% 75%;
      }
    }
  }

  @keyframes earwiggleleft {
    from {
      transform: rotate(37.6deg) skew(30deg);
    }
    25% {
      transform: rotate(10deg) skew(30deg);
    }
    50% {
      transform: rotate(20deg) skew(30deg);
    }
    75% {
      transform: rotate(0deg) skew(30deg);
    }
    to {
      transform: rotate(37.6deg) skew(30deg);
    }
  }

  @keyframes earwiggleright {
    from {
      transform: rotate(-37.6deg) skew(-30deg);
    }
    30% {
      transform: rotate(-10deg) skew(-30deg);
    }
    55% {
      transform: rotate(-20deg) skew(-30deg);
    }
    75% {
      transform: rotate(0deg) skew(-30deg);
    }
    to {
      transform: rotate(-37.6deg) skew(-30deg);
    }
  }

  @keyframes eartightleft {
    from {
      transform: rotate(37.6deg) skew(30deg);
    }
    50% {
      transform: rotate(37.4deg) skew(30deg);
    }
    to {
      transform: rotate(37.6deg) skew(30deg);
    }
  }

  @keyframes eartightright {
    from {
      transform: rotate(-37.6deg) skew(-30deg);
    }
    50% {
      transform: rotate(-37.4deg) skew(-30deg);
    }
    to {
      transform: rotate(-37.6deg) skew(-30deg);
    }
  }
}
</style>
