<template>
  <div class="status-layout">
    <!-- 顶部状态栏 -->
    <StatusBar :title="robotName" />

    <!-- 主内容区 -->
    <div class="layout-main">
      <div class="status-page">
        <!-- 动态背景层 -->
        <div class="bg-layer">
          <div class="bg-orb orb-1"></div>
          <div class="bg-orb orb-2"></div>
          <div class="bg-orb orb-3"></div>
          <div class="bg-grid-overlay"></div>
        </div>

        <!-- 背景模糊遮罩 -->
        <div class="content-shade"></div>

        <!-- 内容区 -->
        <div class="status-content">
          <!-- 标题区 -->
          <div class="status-hero">
            <h1 class="page-title">导航中</h1>
            <p class="page-subtitle">正在前往：{{ targetName }}</p>
          </div>

          <!-- 导航状态大卡片 -->
          <div class="nav-container">
            <!-- 顶部状态提示 -->
            <div class="status-banner" :class="statusClass">
              <span class="banner-text">{{ statusMessage }}</span>
            </div>

            <!-- 路线状态：机器人 + 路径线 + 终点 -->
            <div class="route-track">
              <!-- 机器人 -->
              <div class="route-robot" :class="{ moving: isMoving }">
                <svg viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <circle cx="40" cy="28" r="16" fill="currentColor" />
                  <rect x="20" y="48" width="40" height="24" rx="8" fill="currentColor" />
                  <circle cx="32" cy="24" r="3" fill="white" class="eye-left" />
                  <circle cx="48" cy="24" r="3" fill="white" class="eye-right" />
                  <rect x="60" y="56" width="16" height="4" rx="2" fill="currentColor" class="wheel" />
                  <rect x="4" y="56" width="16" height="4" rx="2" fill="currentColor" class="wheel" />
                </svg>
              </div>

              <!-- 路径条 -->
              <div class="route-line">
                <div class="route-progress" :style="{ width: displayProgress + '%' }"></div>
              </div>

              <!-- 终点图标 -->
              <div class="route-end">
                <svg viewBox="0 0 24 24" fill="currentColor">
                  <path d="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z"/>
                </svg>
              </div>
            </div>

            <!-- 目的地信息三列卡片 -->
            <div class="info-panel">
              <div class="info-item">
                <div class="info-label">目的地</div>
                <div class="info-value">{{ targetName }}</div>
              </div>
              <div class="info-item">
                <div class="info-label">位置</div>
                <div class="info-value">{{ targetFloor }} · {{ targetDescription }}</div>
              </div>
              <div class="info-item">
                <div class="info-label">预计用时</div>
                <div class="info-value">{{ estimatedTime }}</div>
              </div>
            </div>

            <!-- 进度区 -->
            <div class="progress-section">
              <div class="progress-header">
                <span class="progress-label">导航进度</span>
                <span class="progress-value">{{ displayProgress }}%</span>
              </div>
              <div class="progress-track">
                <div class="progress-bar">
                  <div class="progress-fill" :style="{ width: displayProgress + '%' }"></div>
                </div>
              </div>
            </div>

            <!-- 取消按钮 -->
            <button class="btn-cancel" @click="handleCancel">
              取消导航
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 左下角返回按钮 -->
    <button class="btn-back" @click="handleCancel">
      <svg class="back-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
        <path d="M19 12H5M12 5l-7 7 7 7" />
      </svg>
      返回路线引导
    </button>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useNavigationStore } from '@/stores/navigation'
import { useScreenStore } from '@/stores/screen'
import StatusBar from '@/components/StatusBar.vue'

const router = useRouter()
const navigationStore = useNavigationStore()
const screenStore = useScreenStore()

const progress = ref(0)
const isMoving = ref(true)

const robotName = computed(() => screenStore.screenTheme?.robotName || screenStore.robotName || '迎宾巡逻机器人')

const displayProgress = computed(() => Math.round(progress.value))

const targetName = computed(() => {
  return navigationStore.navigationStatus?.targetName
    || navigationStore.currentTask?.targetName
    || navigationStore.currentTask?.destinationName
    || navigationStore.currentTask?.name
    || '前台'
})

const targetFloor = computed(() => {
  return navigationStore.currentTask?.floor
    || navigationStore.navigationStatus?.floor
    || '1楼'
})

const targetDescription = computed(() => {
  return navigationStore.currentTask?.description
    || navigationStore.navigationStatus?.description
    || '综合服务前台'
})

const estimatedTime = computed(() => {
  const t = navigationStore.currentTask?.estimatedTime
    || navigationStore.navigationStatus?.estimatedTime
    || '约2分钟'
  return t
})

const statusMessage = computed(() => {
  const p = displayProgress.value
  if (p === 0) return '正在规划路线'
  if (p < 30) return '请跟随机器人前进'
  if (p < 60) return '正在前往目的地'
  if (p < 90) return '即将到达，请继续跟随'
  if (p < 100) return '即将到达目的地'
  return '已到达目的地'
})

const statusClass = computed(() => {
  if (displayProgress.value === 100) return 'banner-arrived'
  if (displayProgress.value >= 90) return 'banner-near'
  return 'banner-moving'
})

const isMock = ref(false)

let progressTimer = null
let alertTimer = null
let finishTimer = null

const createMockTask = () => {
  const mockTask = {
    taskId: `mock_nav_${Date.now()}`,
    targetName: '前台',
    destinationName: '前台',
    name: '前台',
    floor: '1楼',
    description: '综合服务前台',
    estimatedTime: '约2分钟',
    status: 'navigating'
  }
  navigationStore.currentTask = mockTask
  navigationStore.navigationStatus = {
    taskId: mockTask.taskId,
    status: 'navigating',
    targetName: mockTask.name,
    progress: 0,
    estimatedTime: mockTask.estimatedTime
  }
  isMock.value = true
}

const clearAllTimers = () => {
  if (progressTimer) { clearInterval(progressTimer); progressTimer = null }
  if (alertTimer) { clearTimeout(alertTimer); alertTimer = null }
  if (finishTimer) { clearTimeout(finishTimer); finishTimer = null }
}

const startNavigation = () => {
  progress.value = 0
  isMoving.value = true

  progressTimer = setInterval(() => {
    if (progress.value < 100) {
      progress.value += Math.random() * 5 + 2
      if (progress.value >= 100) {
        progress.value = 100
        isMoving.value = false
        clearInterval(progressTimer)
        progressTimer = null

        alertTimer = setTimeout(() => {
          screenStore.showAlert({
            type: 'success',
            message: `已到达${targetName.value}`,
            duration: import.meta.env.DEV ? 10000 : 3000
          })

          if (!import.meta.env.DEV) {
            finishTimer = setTimeout(handleFinish, 3000)
          }
        }, 1000)
      }
    }
  }, 500)
}

const handleCancel = async () => {
  clearAllTimers()
  try {
    await navigationStore.cancelNavigation()
  } catch {
    // ignore
  }
  router.push('/navigation')
}

const handleFinish = () => {
  clearAllTimers()
  navigationStore.clearNavigation()
  router.push('/idle')
}

onMounted(() => {
  if (!navigationStore.currentTask) {
    if (import.meta.env.DEV) {
      createMockTask()
    } else {
      router.push('/navigation')
      return
    }
  }
  startNavigation()
})

onUnmounted(() => {
  clearAllTimers()
})
</script>

<style scoped>
/* ===== 布局结构 ===== */
.status-layout {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: linear-gradient(155deg, #e8f4fd 0%, #dbeafe 40%, #eff6ff 100%);
}

.layout-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.status-page {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

/* ===== 动态背景层 ===== */
.bg-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 0;
}

.bg-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.45;
}

.orb-1 {
  width: 480px;
  height: 480px;
  background: rgba(47, 142, 229, 0.28);
  top: -180px;
  left: -140px;
  animation: float1 12s ease-in-out infinite;
}

.orb-2 {
  width: 360px;
  height: 360px;
  background: rgba(32, 183, 199, 0.22);
  bottom: -100px;
  right: -80px;
  animation: float2 15s ease-in-out infinite;
}

.orb-3 {
  width: 280px;
  height: 280px;
  background: rgba(139, 92, 246, 0.18);
  top: 50%;
  right: 15%;
  animation: float3 18s ease-in-out infinite;
}

@keyframes float1 {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-40%, -60%) scale(1.08); }
}

@keyframes float2 {
  0%, 100% { transform: translate(50%, 50%) scale(1); }
  50% { transform: translate(60%, 40%) scale(1.06); }
}

@keyframes float3 {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(-5%, 8%) scale(1.1); }
}

.bg-grid-overlay {
  position: absolute;
  inset: 0;
  background-image: radial-gradient(circle, rgba(47, 142, 229, 0.08) 1px, transparent 1px);
  background-size: 32px 32px;
}

.content-shade {
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.18);
  z-index: 0;
  pointer-events: none;
}

/* ===== 内容区 ===== */
.status-content {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 36px 24px;
  z-index: 1;
  overflow: hidden;
}

/* ===== 标题区 ===== */
.status-hero {
  text-align: center;
  margin: 28px 0 16px;
  flex-shrink: 0;
}

.page-title {
  font-size: 46px;
  font-weight: 900;
  color: #111827;
  margin: 0;
  letter-spacing: 2px;
  line-height: 1.2;
}

.page-subtitle {
  font-size: 24px;
  color: #6b7280;
  margin: 8px 0 0;
}

/* ===== 导航状态大卡片 ===== */
.nav-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  width: min(920px, calc(100vw - 120px));
  max-width: 920px;
  padding: 32px 48px;
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 36px;
  box-shadow: 0 12px 40px rgba(47, 142, 229, 0.12), 0 4px 12px rgba(0, 0, 0, 0.06);
  box-sizing: border-box;
}

/* ===== 顶部状态提示 ===== */
.status-banner {
  width: 100%;
  padding: 16px 32px;
  border-radius: 20px;
  text-align: center;
  flex-shrink: 0;
}

.banner-moving {
  background: rgba(47, 142, 229, 0.08);
}

.banner-near {
  background: rgba(245, 158, 11, 0.1);
}

.banner-arrived {
  background: rgba(16, 185, 129, 0.1);
}

.banner-text {
  font-size: 30px;
  font-weight: 800;
  color: #111827;
}

.banner-near .banner-text {
  color: #d97706;
}

.banner-arrived .banner-text {
  color: #10b981;
}

/* ===== 路线状态：机器人 + 路径 + 终点 ===== */
.route-track {
  position: relative;
  width: 100%;
  height: 120px;
  display: flex;
  align-items: center;
}

.route-robot {
  width: 100px;
  height: 100px;
  color: #2f8ee5;
  z-index: 2;
  flex-shrink: 0;
  transition: transform 0.5s ease;
}

.route-robot.moving {
  animation: walkBounce 0.5s ease-in-out infinite;
}

.route-robot svg {
  width: 100%;
  height: 100%;
}

.route-robot .eye-left,
.route-robot .eye-right {
  animation: blink 3s ease-in-out infinite;
}

@keyframes walkBounce {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  25% { transform: translateY(-5px) rotate(-2deg); }
  75% { transform: translateY(-5px) rotate(2deg); }
}

@keyframes blink {
  0%, 90%, 100% { transform: scaleY(1); }
  95% { transform: scaleY(0.1); }
}

.route-line {
  flex: 1;
  height: 14px;
  background: rgba(47, 142, 229, 0.1);
  border-radius: 999px;
  overflow: hidden;
  margin: 0 12px;
}

.route-progress {
  height: 100%;
  background: linear-gradient(90deg, #2f8ee5, #20b7c7);
  border-radius: 999px;
  transition: width 0.4s ease;
}

.route-end {
  width: 52px;
  height: 52px;
  color: #10b981;
  flex-shrink: 0;
}

.route-end svg {
  width: 100%;
  height: 100%;
  animation: endPulse 1.5s ease-in-out infinite;
}

@keyframes endPulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.12); opacity: 0.75; }
}

/* ===== 目的地信息三列卡片 ===== */
.info-panel {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  flex-shrink: 0;
}

.info-item {
  background: rgba(47, 142, 229, 0.06);
  border-radius: 18px;
  padding: 16px 18px;
  box-sizing: border-box;
}

.info-label {
  font-size: 18px;
  color: #6b7280;
  margin-bottom: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.info-value {
  font-size: 24px;
  font-weight: 800;
  color: #111827;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ===== 进度区 ===== */
.progress-section {
  width: 100%;
  flex-shrink: 0;
}

.progress-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.progress-label {
  font-size: 20px;
  color: #6b7280;
  font-weight: 600;
}

.progress-value {
  font-size: 26px;
  font-weight: 800;
  color: #2f8ee5;
}

.progress-track {
  width: 100%;
}

.progress-bar {
  height: 18px;
  background: rgba(47, 142, 229, 0.1);
  border-radius: 999px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #2f8ee5, #20b7c7);
  border-radius: 999px;
  transition: width 0.4s ease;
}

/* ===== 取消导航按钮 ===== */
.btn-cancel {
  height: 72px;
  padding: 0 56px;
  font-size: 24px;
  font-weight: 800;
  color: #6b7280;
  background: rgba(255, 255, 255, 0.9);
  border: 2px solid rgba(148, 163, 184, 0.3);
  border-radius: 999px;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
  box-sizing: border-box;
  flex-shrink: 0;
}

.btn-cancel:hover {
  border-color: #ef4444;
  color: #ef4444;
  box-shadow: 0 6px 20px rgba(239, 68, 68, 0.15);
  transform: translateY(-2px);
}

.btn-cancel:active {
  transform: scale(0.97);
}

/* ===== 左下角返回按钮 ===== */
.btn-back {
  position: fixed;
  left: 24px;
  bottom: 28px;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 66px;
  padding: 0 28px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 2px solid rgba(148, 163, 184, 0.22);
  border-radius: 999px;
  color: #374151;
  font-size: 22px;
  font-weight: 800;
  cursor: pointer;
  letter-spacing: 1px;
  transition: all 0.2s ease;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.1);
  box-sizing: border-box;
}

.btn-back:hover,
.btn-back:focus-visible {
  color: #2f8ee5;
  border-color: rgba(47, 142, 229, 0.36);
  background: rgba(255, 255, 255, 0.98);
  transform: translateY(-2px);
  box-shadow: 0 10px 30px rgba(47, 142, 229, 0.18);
}

.btn-back:active {
  transform: scale(0.97);
}

.back-icon {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
}

/* ===== 响应式适配 ===== */
@media (max-height: 700px) {
  .status-hero {
    margin: 18px 0 12px;
  }

  .page-title {
    font-size: 38px;
  }

  .page-subtitle {
    font-size: 20px;
  }

  .nav-container {
    gap: 14px;
    padding: 24px 36px;
  }

  .status-banner {
    padding: 12px 24px;
  }

  .banner-text {
    font-size: 24px;
  }

  .route-track {
    height: 100px;
  }

  .route-robot {
    width: 80px;
    height: 80px;
  }

  .route-end {
    width: 44px;
    height: 44px;
  }

  .route-line {
    height: 12px;
  }

  .info-panel {
    gap: 10px;
  }

  .info-item {
    padding: 12px 14px;
  }

  .info-label {
    font-size: 16px;
  }

  .info-value {
    font-size: 20px;
  }

  .progress-value {
    font-size: 22px;
  }

  .progress-bar {
    height: 16px;
  }

  .btn-cancel {
    height: 62px;
    padding: 0 44px;
    font-size: 22px;
  }

  .btn-back {
    height: 58px;
    padding: 0 22px;
    font-size: 20px;
    left: 20px;
    bottom: 22px;
  }
}
</style>
