<script setup>
import { computed } from "vue";

const lockedBadgeBackground =
  "https://static.shelingxingqiu.com/shootmini/static/training-difficulty-design/unlock.svg";
const unlockedBadgeBackground =
  "https://static.shelingxingqiu.com/shootmini/static/training-difficulty-design/lock.svg";

const props = defineProps({
  node: {
    type: Object,
    required: true,
  },
  active: {
    type: Boolean,
    default: false,
  },
  completedProgress: {
    type: Number,
    default: 0,
  },
  locked: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(["click"]);

const badgeStyle = computed(() => {
  const { left, top } = props.node.style || {};

  return {
    left,
    top,
  };
});

const progressValue = computed(() => {
  const value = Number(props.completedProgress);

  if (!Number.isFinite(value)) return 0;

  return Math.max(0, Math.min(100, value));
});

const badgeStateStyle = computed(() => {
  const label = String(props.node?.label || "");
  const estimatedLabelWidthRpx = Math.max(36, label.length * 14);
  const labelCircleSizeRpx = Math.max(58, estimatedLabelWidthRpx + 18);
  const badgeSizeRpx = Math.max(
    124,
    Math.round(labelCircleSizeRpx / 0.4727)
  );

  return {
    "--badge-progress": progressValue.value,
    "--badge-size": `${badgeSizeRpx}rpx`,
    "--badge-label-size": `${labelCircleSizeRpx}rpx`,
    "--badge-orbit-offset": "12rpx",
    "--badge-locked-ring-offset": "12rpx",
  };
});

const showProgress = computed(() => {
  return !props.locked;
});

const badgeFillSrc = computed(() => {
  return props.locked ? lockedBadgeBackground : unlockedBadgeBackground;
});

const handleClick = () => {
  emit("click", props.node);
};
</script>

<template>
  <view
    class="difficulty-badge"
    :class="{
      'difficulty-badge--active': active,
      'difficulty-badge--progress': showProgress,
      'difficulty-badge--locked': locked,
    }"
    :style="[badgeStyle, badgeStateStyle]"
    @click="handleClick"
  >
    <view class="difficulty-badge__fill">
      <image class="difficulty-badge__bg" :src="badgeFillSrc" mode="aspectFit" />
      <view v-if="active" class="difficulty-badge__active-orbit">
        <view
          class="difficulty-badge__active-triangle difficulty-badge__active-triangle--top"
        ></view>
        <view
          class="difficulty-badge__active-triangle difficulty-badge__active-triangle--right"
        ></view>
        <view
          class="difficulty-badge__active-triangle difficulty-badge__active-triangle--bottom"
        ></view>
        <view
          class="difficulty-badge__active-triangle difficulty-badge__active-triangle--left"
        ></view>
      </view>
      <view class="difficulty-badge__label-wrap">
        <view class="difficulty-badge__label">{{ node.label }}</view>
      </view>
    </view>
  </view>
</template>

<style scoped>
.difficulty-badge,
.difficulty-badge__fill,
.difficulty-badge__label-wrap {
  box-sizing: border-box;
}

.difficulty-badge {
  position: absolute;
  transform: translate(-50%, -50%);
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.difficulty-badge--active {
  transform: translate(-50%, -50%) scale(1.04);
}

.difficulty-badge--active::before {
  content: "";
  position: absolute;
  inset: calc(var(--badge-orbit-offset) * -1);
  border: 4rpx solid transparent;
  border-radius: 50%;
  pointer-events: none;
  box-sizing: border-box;
}

.difficulty-badge--active::after {
  content: "";
  position: absolute;
  inset: -24rpx;
  border: 4rpx solid rgba(254, 208, 152, 0.96);
  border-radius: 50%;
  box-shadow: inset 0 0 10rpx rgba(254, 208, 152, 0.88),
    inset 0 0 22rpx rgba(254, 208, 152, 0.32),
    0 0 14rpx rgba(254, 208, 152, 0.92),
    0 0 32rpx rgba(254, 208, 152, 0.52),
    0 0 52rpx rgba(254, 208, 152, 0.22);
  pointer-events: none;
  box-sizing: border-box;
}

.difficulty-badge__active-orbit {
  position: absolute;
  top: 50%;
  left: 50%;
  width: calc(100% + var(--badge-orbit-offset) * 2);
  height: calc(100% + var(--badge-orbit-offset) * 2);
  border-radius: 50%;
  z-index: 3;
  pointer-events: none;
  transform: translate(-50%, -50%);
  animation: badge-orbit-spin 5.4s linear infinite;
  transform-origin: center;
}

.difficulty-badge__active-triangle {
  position: absolute;
  width: 0;
  height: 0;
  border-style: solid;
  z-index: 2;
  pointer-events: none;
  opacity: 0.92;
  filter: drop-shadow(0 0 8rpx rgba(255, 255, 255, 0.45));
}

.difficulty-badge__active-triangle--top {
  top: -3rpx;
  left: 50%;
  transform: translateX(-50%);
  border-width: 11rpx 8rpx 0 8rpx;
  border-color: #ffffff transparent transparent transparent;
}

.difficulty-badge__active-triangle--right {
  right: -3rpx;
  top: 50%;
  transform: translateY(-50%);
  border-width: 8rpx 11rpx 8rpx 0;
  border-color: transparent #ffffff transparent transparent;
}

.difficulty-badge__active-triangle--bottom {
  bottom: -3rpx;
  left: 50%;
  transform: translateX(-50%);
  border-width: 0 8rpx 11rpx 8rpx;
  border-color: transparent transparent #ffffff transparent;
}

.difficulty-badge__active-triangle--left {
  left: -3rpx;
  top: 50%;
  transform: translateY(-50%);
  border-width: 8rpx 0 8rpx 11rpx;
  border-color: transparent transparent transparent #ffffff;
}

.difficulty-badge__fill {
  width: var(--badge-size);
  height: var(--badge-size);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.difficulty-badge__bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 1;
}

.difficulty-badge--active .difficulty-badge__fill::before,
.difficulty-badge--active .difficulty-badge__fill::after {
  content: none;
}

.difficulty-badge--progress .difficulty-badge__fill::before,
.difficulty-badge--progress .difficulty-badge__fill::after {
  content: "";
  position: absolute;
  inset: -12rpx;
  padding: 6rpx;
  border-radius: inherit;
  -webkit-mask: linear-gradient(#fff 0 0) content-box,
    linear-gradient(#fff 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
  mask-composite: exclude;
  pointer-events: none;
}

.difficulty-badge--progress .difficulty-badge__fill::before {
  background: rgba(255, 255, 255, 0.35);
}

.difficulty-badge--progress .difficulty-badge__fill::after {
  background: conic-gradient(
    from -90deg,
    rgba(254, 208, 152, 1) 0,
    rgba(255, 229, 198, 1) calc(var(--badge-progress) * 1%),
    transparent calc(var(--badge-progress) * 1%) 100%
  );
}

.difficulty-badge--active .difficulty-badge__label,
.difficulty-badge--progress .difficulty-badge__label {
  color: #333333;
}

.difficulty-badge--locked {
  opacity: 1;
}

.difficulty-badge--locked::before {
  content: "";
  position: absolute;
  inset: calc(var(--badge-locked-ring-offset) * -1);
  border: 2rpx solid rgba(160, 160, 160, 0.5);
  border-radius: 50%;
  pointer-events: none;
  box-sizing: border-box;
}

.difficulty-badge--locked .difficulty-badge__label {
  color: rgba(51, 51, 51, 0.54);
}

.difficulty-badge__label-wrap {
  width: var(--badge-label-size);
  height: var(--badge-label-size);
  border-radius: 50%;
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: -5rpx;
}

.difficulty-badge__label {
  color: rgba(51, 51, 51, 0.7);
  font-size: 24rpx;
  max-width: 100%;
  height: 34rpx;
  line-height: 34rpx;
  font-family: "PingFang SC", sans-serif;
  font-weight: 600;
  text-align: center;
  white-space: nowrap;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

@keyframes badge-orbit-spin {
  from {
    transform: translate(-50%, -50%) rotate(0deg);
  }

  to {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}
</style>
