<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import Particles from '@/components/Particles/Particles.vue'
import GlareHover from '@/components/GlareHover/GlareHover.vue'
import Shuffle from '@/components/Shuffle/Shuffle.vue'

const router = useRouter()
const authStore = useAuthStore()

const username = ref('')
const email = ref('')
const password = ref('')
const confirmPassword = ref('')
const errorMsg = ref('')
const isLoading = ref(false)
const registerSuccess = ref(false)

const validateForm = () => {
  if (!username.value || !email.value || !password.value || !confirmPassword.value) {
    errorMsg.value = '请填写所有字段'
    return false
  }

  if (username.value.length < 3) {
    errorMsg.value = '用户名至少3个字符'
    return false
  }

  if (password.value.length < 4) {
    errorMsg.value = '密码至少4个字符'
    return false
  }

  if (password.value !== confirmPassword.value) {
    errorMsg.value = '两次输入的密码不一致'
    return false
  }

  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  if (!emailRegex.test(email.value)) {
    errorMsg.value = '请输入有效的邮箱地址'
    return false
  }

  return true
}

const handleRegister = async () => {
  errorMsg.value = ''

  if (!validateForm()) return

  isLoading.value = true

  try {
    const success = await authStore.register({
      name: username.value,
      email: email.value,
      password: password.value
    })

    if (success) {
      registerSuccess.value = true

      setTimeout(() => {
        router.push('/login')
      }, 2000)
    } else {
      errorMsg.value = '注册失败，用户名或邮箱可能已被使用'
    }
  } catch (error: any) {
    errorMsg.value = error?.message || '注册失败，请稍后重试'
  } finally {
    isLoading.value = false
  }
}
</script>

<template>
  <div class="register-page">
    <Particles :particle-count="150" :particle-colors="['#ffffff', '#cccccc', '#999999']" class="particles-bg" />

    <div class="register-container">
      <div class="register-card">
        <div class="register-header">
          <Shuffle text="创建账号" shuffle-direction="right" :duration="0.35" animation-mode="evenodd" :shuffle-times="1"
            ease="power3.out" :stagger="0.03" :threshold="0.1" :trigger-once="true" :trigger-on-hover="true"
            class="shuffle-title" :respect-reduced-motion="true" />
          <p class="subtitle">开启游戏之旅</p>
        </div>

        <form class="register-form" @submit.prevent="handleRegister">
          <div class="form-group">
            <label class="form-label">用户名</label>
            <input v-model="username" type="text" class="form-input" placeholder="3-30个字符，仅限字母数字下划线"
              autocomplete="username" />
          </div>

          <div class="form-group">
            <label class="form-label">邮箱</label>
            <input v-model="email" type="email" class="form-input" placeholder="请输入邮箱地址" autocomplete="email" />
          </div>

          <div class="form-group">
            <label class="form-label">密码</label>
            <input v-model="password" type="password" class="form-input" placeholder="至少4个字符"
              autocomplete="new-password" />
          </div>

          <div class="form-group">
            <label class="form-label">确认密码</label>
            <input v-model="confirmPassword" type="password" class="form-input" placeholder="请再次输入密码"
              autocomplete="new-password" />
          </div>

          <div v-if="errorMsg" class="error-message">
            {{ errorMsg }}
          </div>

          <GlareHover width="100%" height="50px" background="transparent" border-radius="12px"
            border-color="rgba(255,255,255,0.15)" glare-color="#ffffff" :glare-opacity="0.15" class="register-button"
            @click="handleRegister">
            <span v-if="isLoading" class="loading-text">注册中...</span>
            <span v-else-if="registerSuccess" class="button-text">注册成功！</span>
            <span v-else class="button-text">立即注册</span>
          </GlareHover>
        </form>

        <div class="register-footer">
          <span class="footer-text">已有账号？</span>
          <router-link to="/login" class="footer-link">
            立即登录
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000000;
  position: relative;
  overflow: hidden;
}

.particles-bg {
  position: fixed !important;
  top: 0;
  left: 0;
  width: 100% !important;
  height: 100% !important;
}

.register-container {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 380px;
  padding: 20px;
}

.register-card {
  background: rgba(255, 255, 255, 0.03);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 20px;
  padding: 32px;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.8);
}

.register-header {
  text-align: center;
  margin-bottom: 28px;
}

.shuffle-title {
  justify-content: center;
  /* 水平居中 flex 项目 */
  text-align: center;
  /* 文字居中 */
  color: white;
  width: 100%;
  /* 确保占满宽度 */
  font-size: 30px;
}

.title {
  font-size: 1.5rem;
  font-weight: 300;
  color: #ffffff;
  letter-spacing: 0.15em;
  margin-bottom: 8px;
}

.subtitle {
  color: #666666;
  font-size: 0.875rem;
  letter-spacing: 0.05em;
}

.register-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-label {
  color: #888888;
  font-size: 0.75rem;
  font-weight: 400;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.form-input {
  width: 100%;
  padding: 14px 16px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  color: #ffffff;
  font-size: 0.95rem;
  transition: all 0.3s ease;
  outline: none;
}

.form-input::placeholder {
  color: #444444;
}

.form-input:focus {
  border-color: rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.05);
  box-shadow: 0 0 20px rgba(255, 255, 255, 0.05);
}

.error-message {
  color: #888888;
  font-size: 0.8rem;
  text-align: center;
  padding: 10px;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 8px;
}

.register-button {
  margin-top: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.button-text,
.loading-text {
  color: #ffffff;
  font-size: 0.9rem;
  font-weight: 400;
  letter-spacing: 0.2em;
}

.register-footer {
  margin-top: 28px;
  text-align: center;
  display: flex;
  justify-content: center;
  gap: 8px;
}

.footer-text {
  color: #444444;
  font-size: 0.8rem;
}

.footer-link {
  color: #888888;
  font-size: 0.8rem;
  text-decoration: none;
  transition: color 0.3s ease;
}

.footer-link:hover {
  color: #ffffff;
}

/* Mobile responsive */
@media (max-width: 480px) {
  .register-card {
    padding: 28px 20px;
  }

  .title {
    font-size: 1.25rem;
  }

  .form-input {
    padding: 12px 14px;
    font-size: 16px;
  }
}
</style>
