<script setup>
import { ref, computed, onMounted, onBeforeUnmount } from "vue";
import HeaderProgress from "@/components/HeaderProgress.vue";
import Avatar from "@/components/Avatar.vue";

import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);

const currentPage = computed(() => {
  const pages = getCurrentPages();
  return pages[pages.length - 1].route;
});

const props = defineProps({
  title: {
    type: String,
    default: "",
  },
  onBack: {
    type: Function,
    default: null,
  },
  whiteBackArrow: {
    type: Boolean,
    default: true,
  },
});

const onClick = () => {
  if (props.onBack) {
    props.onBack();
  } else {
    const pages = getCurrentPages();
    if (pages.length > 1) {
      uni.navigateBack();
    } else {
      uni.redirectTo({
        url: "/pages/index",
      });
    }
  }
};

const toUserPage = () => {
  uni.navigateTo({
    url: "/pages/user",
  });
};

const signin = () => {
  if (!user.value.id) {
    uni.$emit("point-book-signin");
  }
};

const loading = ref(false);
const showLoader = ref(false);
const pointBook = ref(null);
const showProgress = ref(false);
const heat = ref(0);
const updateLoading = (value) => {
  loading.value = value;
};

const updateHot = (value) => {
  heat.value = value;
};

onMounted(() => {
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  if (
    currentPage.route === "pages/point-book-edit" ||
    currentPage.route === "pages/point-book-detail"
  ) {
    pointBook.value = uni.getStorageSync("point-book");
    if (!pointBook.value) {
      pointBook.value = uni.getStorageSync("last-point-book");
    }
  }
  if (
    currentPage.route === "pages/team-battle" ||
    currentPage.route === "pages/melee-match"
  ) {
    showLoader.value = true;
  }
  if (currentPage.route === "pages/team-battle") {
    showProgress.value = true;
  }
  uni.$on("update-header-loading", updateLoading);
  uni.$on("update-hot", updateHot);
});
onBeforeUnmount(() => {
  uni.$off("update-header-loading", updateLoading);
  uni.$off("update-hot", updateHot);
});
</script>

<template>
  <view class="container">
    <view class="back-btn" @click="onClick">
      <image v-if="whiteBackArrow" src="../static/back.png" mode="widthFix" />
      <image
        v-if="!whiteBackArrow"
        src="../static/back-black.png"
        mode="widthFix"
      />
    </view>
    <view :style="{ color: whiteBackArrow ? '#fff' : '#000' }">
      <view
        v-if="currentPage === 'pages/point-book'"
        class="user-header"
        @click="signin"
      >
        <block v-if="user.id">
          <Avatar
            :src="user.avatar"
            :onClick="toUserPage"
            :size="40"
            borderColor="#333"
          />
          <text class="truncate">{{ user.nickName }}</text>
          <image
            v-if="heat"
            :src="`../static/hot${heat}.png`"
            mode="widthFix"
          />
        </block>
        <block v-else>
          <image src="../static/user-icon.png" mode="widthFix" />
          <text>新来的弓箭手你好呀~</text>
        </block>
      </view>
      <block
        v-if="
          '-凹造型-感知距离-小试牛刀'.indexOf(title) === -1 ||
          '-凹造型-感知距离-小试牛刀'.indexOf(title) === 10
        "
      >
        <text>{{ title }}</text>
      </block>
      <block
        v-if="
          title &&
          '-凹造型-感知距离-小试牛刀'.indexOf(title) !== -1 &&
          '-凹造型-感知距离-小试牛刀'.indexOf(title) !== 10
        "
      >
        <view class="first-try-steps">
          <text :class="title === '-凹造型' ? 'current-step' : ''">凹造型</text>
          <text>-</text>
          <text :class="title === '-感知距离' ? 'current-step' : ''"
            >感知距离</text
          >
          <text>-</text>
          <text :class="title === '-小试牛刀' ? 'current-step' : ''"
            >小试牛刀</text
          >
        </view>
      </block>
    </view>
    <image
      :style="{ opacity: showLoader && loading ? 0 : 0 }"
      src="../static/btn-loading.png"
      mode="widthFix"
      class="loading"
    />
    <view v-if="pointBook" class="point-book-info">
      <text>{{ pointBook.bowType.name }}</text>
      <text>{{ pointBook.distance }} 米</text>
      <text
        >{{
          pointBook.bowtargetType.name.substring(
            0,
            pointBook.bowtargetType.name.length - 3
          )
        }}
        {{
          pointBook.bowtargetType.name.substring(
            pointBook.bowtargetType.name.length - 3
          )
        }}</text
      >
    </view>
    <view v-if="showProgress" class="battle-progress">
      <HeaderProgress />
    </view>
  </view>
</template>

<style scoped>
.container {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  width: 72vw;
  height: 50px;
  /* margin-top: var(--status-bar-height); */
  padding-left: 15px;
}
.container > view:nth-child(2) {
  font-size: 16px;
  font-weight: bold;
}
.back-btn {
  display: flex;
  align-items: center;
}
.back-btn > image {
  width: 22px;
  height: 22px;
  margin-right: 10px;
}
.first-try-steps {
  display: flex;
  align-items: center;
  color: #fff6;
  font-size: 14px;
}
.first-try-steps > text {
  transition: all 0.3s ease;
}
.first-try-steps > text:nth-child(2),
.first-try-steps > text:nth-child(4) {
  margin: 0 5px;
}
.current-step {
  font-size: 16px;
  color: #fff;
}
.loading {
  width: 20px;
  height: 20px;
  margin-left: 10px;
  transition: all 0.3s ease;
  background-blend-mode: darken;
  animation: rotate 2s linear infinite;
}
.point-book-info {
  color: #333;
  position: fixed;
  width: 60%;
  left: 20%;
  display: flex;
  justify-content: center;
}
.point-book-info > text {
  border-radius: 6px;
  background-color: #fff;
  font-size: 10px;
  padding: 5px 10px;
  margin: 3px;
}
.battle-progress {
  position: fixed;
  width: 60%;
  left: 20%;
  display: flex;
  justify-content: center;
}
.user-header {
  display: flex;
  align-items: center;
  justify-content: flex-start;
}
.user-header > image:first-child {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2rpx solid #333;
}
.user-header > image:last-child {
  width: 36rpx;
  height: 36rpx;
}
.user-header > text:nth-child(2) {
  font-weight: 500;
  font-size: 30rpx;
  color: #333333;
  margin: 0 20rpx;
  max-width: 300rpx;
}
</style>
