<script setup lang="ts">
import { computed, reactive } from 'vue'
import { getActionTypeDisplay, getAgentMapIcon } from '@/layout/components/config.ts'
import SvgIcon from '@/components/SvgIcon/index.vue'
import { type Agent, useAgentsStore } from '@/stores'
import { v4 as uuidv4 } from 'uuid'

const porps = defineProps<{
  agentList: Agent[]
}>()

// 每个智能体的标签状态
const tabState = reactive<Record<string, 'duty' | 'info'>>({})

const getTabState = (agentName: string) => {
  if (!tabState[agentName]) {
    tabState[agentName] = 'duty' // 默认显示"当前职责"
  }
  return tabState[agentName]
}

const setTabState = (agentName: string, tab: 'duty' | 'info') => {
  tabState[agentName] = tab
}

const taskProcess = computed(() => {
  const list = agentsStore.currentTask?.TaskProcess ?? []
  return list.map(item => ({
    ...item,
    key: uuidv4()
  }))
})

const agentsStore = useAgentsStore()
</script>

<template>
  <div
    v-for="item in porps.agentList"
    :key="item.Name"
    class="user-item"
    :class="agentsStore.currentTask?.AgentSelection?.includes(item.Name) ? 'active-card' : ''"
  >
    <div class="flex items-center justify-between relative h-[43px]">
      <!-- 图标区域 -->
      <div
        class="w-[44px] h-[44px] rounded-full flex items-center justify-center flex-shrink-0 relative right-[2px] icon-container"
        :style="{ background: getAgentMapIcon(item.Name).color }"
      >
        <svg-icon :icon-class="getAgentMapIcon(item.Name).icon" color="#fff" size="24px" />
      </div>

      <div class="flex-1 text-[14px] textClass flex flex-col items-end justify-start truncate ml-1">
        <div class="flex items-center justify-start gap-2 w-full">
          <span
            class="truncate"
            :style="
              agentsStore.currentTask?.AgentSelection?.includes(item.Name)
                ? 'color:var(--color-accent)'
                : ''
            "
            >{{ item.Name }}</span
          >
        </div>
        <div
          v-if="agentsStore.currentTask?.AgentSelection?.includes(item.Name)"
          class="flex items-center gap-[7px] h-[8px] mr-1"
        >
          <!-- 小圆点 -->
          <div
            v-for="item1 in taskProcess.filter(i => i.AgentName === item.Name)"
            :key="item1.key"
            class="w-[6px] h-[6px] rounded-full"
            :style="{
              background: getActionTypeDisplay(item1.ActionType)?.color,
              border: `1px solid ${getActionTypeDisplay(item1.ActionType)?.border}`
            }"
          ></div>
        </div>
      </div>
    </div>

    <!-- 职责信息只有当执行流程中有当前智能体并且鼠标移入时才显示 -->
    <div class="duty-info">
      <div class="w-full flex justify-center">
        <div
          class="rounded-[9px] text-[12px] py-0.5 px-5 text-center my-2 cursor-pointer transition-colors"
          :class="
            getTabState(item.Name) === 'info'
              ? 'bg-[var(--color-tab-bg-active)]'
              : 'bg-[var(--color-tab-bg)]'
          "
          :style="{
            color:
              getTabState(item.Name) === 'info'
                ? 'var(--color-tab-text)'
                : 'var(--color-tab-text-inactive)'
          }"
          @click="setTabState(item.Name, 'info')"
        >
          信息描述
        </div>
        <div
          class="rounded-[9px] text-[12px] py-0.5 px-5 text-center my-2 cursor-pointer transition-colors"
          :class="
            getTabState(item.Name) === 'duty'
              ? 'bg-[var(--color-tab-bg-active)]'
              : 'bg-[var(--color-tab-bg)]'
          "
          :style="{
            color:
              getTabState(item.Name) === 'duty'
                ? 'var(--color-tab-text)'
                : 'var(--color-tab-text-inactive)'
          }"
          @click="setTabState(item.Name, 'duty')"
        >
          当前职责
        </div>
      </div>
      <div class="p-[8px] pt-0">
        <!-- 信息描述 -->
        <template v-if="getTabState(item.Name) === 'info'">
          <!-- 数据空间 -->
          <div class="text-[12px]">
            <span class="text-[var(--color-text)] font-bold">数据空间</span>
            <div class="text-[var(--color-text-secondary)] mt-1">归属于{{ item.Classification }}数据空间</div>
          </div>
          <!-- 分割线 -->
          <div class="h-[1px] w-full bg-[var(--color-border-default)] my-[8px]"></div>
          <!-- 介绍 -->
          <div class="text-[12px]">
            <span class="text-[var(--color-text)] font-bold">介绍</span>
            <div class="text-[var(--color-text-secondary)] mt-1">{{ item.Profile }}</div>
          </div>
        </template>
        <!-- 当前职责 -->
        <template v-else>
          <div
            v-for="(item1, index1) in taskProcess.filter(i => i.AgentName === item.Name)"
            :key="item1.key"
            class="text-[12px]"
          >
            <div>
              <div class="mx-1 inline-block h-[14px]">
                <div
                  :style="{ background: getActionTypeDisplay(item1.ActionType)?.color }"
                  class="w-[6px] h-[6px] rounded-full mt-[7px]"
                ></div>
              </div>
              <span :style="{ color: getActionTypeDisplay(item1.ActionType)?.color }"
                >{{ getActionTypeDisplay(item1.ActionType)?.name }}：</span
              >
              <span>{{ item1.Description }}</span>
            </div>
            <!-- 分割线 -->
            <div
              v-if="index1 !== taskProcess.filter(i => i.AgentName === item.Name).length - 1"
              class="h-[1px] w-full bg-[var(--color-border-default)] my-[8px]"
            ></div>

            <AssignmentButton />
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.user-item {
  background: var(--color-agent-list-bg);
  border-radius: 40px;
  padding-right: 12px;
  cursor: pointer;
  transition: all 0.25s ease;
  color: var(--color-text-detail);
  border: 1px solid var(--color-agent-list-border);
  box-sizing: border-box;
  .duty-info {
    transition: height 0.25s ease;
    height: 0;
    overflow: hidden;
  }

  & + .user-item {
    margin-top: 8px;
  }

  &:hover {
    box-shadow: var(--color-agent-list-hover-shadow);
    color: var(--color-text);
    background: var(--color-agent-list-hover-bg);
    border: 1px solid var(--color-agent-list-hover-border);
  }
}

.textClass {
  color: var(--color-text-agent-list);
  &:hover {
    color: var(--color-text-agent-list-hover);
  }
}

.active-card {
  background: linear-gradient(var(--color-bg-quaternary), var(--color-bg-quaternary)) padding-box,
    linear-gradient(to right, var(--color-accent), var(--color-accent-secondary)) border-box;
  border: 1px solid var(--color-agent-list-border);

  &:hover {
    box-shadow: var(--color-agent-list-hover-shadow);
    color: var(--color-text);
    background: var(--color-agent-list-hover-bg);
    border: 1px solid var(--color-agent-list-hover-border);
    border-radius: 20px;
    .duty-info {
      height: auto;
    }

    .icon-container {
      bottom: 2px;
    }
  }
}

// 添加头像容器样式修复
.icon-container {
  right: 0 !important;
  margin-left: 0px;
}

// 标签悬浮样式（仅未激活状态）
.duty-info .rounded-\[9px\]:not(.bg-\[var\(--color-tab-bg-active\)\]):hover {
  background: var(--color-tab-bg-hover) !important;
  color: var(--color-tab-text-hover) !important;
}
</style>
