<script setup>
import { ref, unref, onMounted, onUnmounted, computed, watch } from 'vue';
import { useMessage, NInput, NSwitch, NPopover, NScrollbar } from 'naive-ui';
import { helperApi } from '@/api/helper';
import SvgIcon from '@/components/SvgIcon';

import 'load-awesome/css/ball-running-dots.min.css';

const props = defineProps({
  activeItem: {
    type: Object,
    default: () => ({})
  }
});

const emit = defineEmits(['onClick', 'onEnter']);

const modelLoading = defineModel('loading');
const switchStatus = defineModel('switch');

const message = useMessage();

const inpVal = ref('');
const inpRef = ref(null);
const isFocusState = ref(false);

const isOpen = ref(false);
const highlightedIndex = ref(0);
const selectedOption = ref(null);
const helperList = ref([]);

const popoverTriggerRef = ref(null);
const popoverInnerRef = ref(null);

const agentOptions = computed(() => helperList.value.filter(({ tools }) => tools));

const focusInput = _ => isFocusState.value = true;

const blurInput = _ => isFocusState.value = false;

watch(inpVal, (curVal) => {
  if (curVal === "@" && curVal.length === 1) {
    if ( !unref(agentOptions).length ) {
      return message.warning('当前未配置智能体');
    }
    if ( modelLoading.value ) {
      return message.warning('当前对话进行中');
    }
    isOpen.value = true;
  } else {
    isOpen.value = false;
  }
  // isOpen.value = (curVal === "@" && curVal.length === 1);
})

watch(() => props.activeItem, (curVal) => {
  selectedOption.value = curVal?.tools ? curVal :  null;
})

const handleInpFocus = () => {
  inpRef.value?.focus();
}

const commonEmitEvent = (eventName) => {
  const val = unref(inpVal);
  const len = val.trim().length;

  if ( !len ) {
    return message.warning('请输入您的问题或需求');
  }

  if ( len > 2000 ) {
    return message.warning('问题限制2000个字以内');
  }

  if ( modelLoading.value ) {
    return message.warning('当前对话进行中');
  }

  // emit(eventName, val);
  emit(eventName, {question: val, selectedOption: selectedOption.value || {}});

  inpVal.value = '';
}

// 回车事件
const handleInpEnter = (event) => {
  if (event.key === 'Enter' && !event.shiftKey && inpVal.value) {
    event.preventDefault();
    commonEmitEvent('onEnter');
    inpRef.value?.blur();
  }
}

// 点击事件
const handleBtnClick = () => {
  commonEmitEvent("onClick");
}

const clearInpVal = () => {
  inpVal.value = '';
}

// 键盘事件
const handleKeyDown = (event) => {
  const len = unref(agentOptions).length;

  if ( !isOpen.value ) return;

  switch (event.key) {
    case 'ArrowUp':
      event.preventDefault();
      highlightedIndex.value = (unref(highlightedIndex) - 1 + len) % len;
      break;
    case 'ArrowDown':
      event.preventDefault();
      highlightedIndex.value = (unref(highlightedIndex) + 1) % len;
      break;
    case 'Enter':
      event.preventDefault();
      selectOption(unref(highlightedIndex));
      break;
    default:
      break;
  }
}

// 处理点击空白处关闭
const closePopoverOutside =(event) => {
  if (!isOpen.value) return; 
  const triggerResult = popoverTriggerRef.value.contains(event.target);
  const innerResult = popoverInnerRef.value.contains(event.target);
  isOpen.value = triggerResult || innerResult;
}

// 选中选项
const selectOption = (index) => {
  selectedOption.value = agentOptions.value[index];
  highlightedIndex.value = index;
  isOpen.value = false;
 
  inpVal.value = selectedOption.value.content;

  // clearInpVal();
}

onMounted(async () => {
  const { data } = await helperApi.getHelperList();
  helperList.value = data;
  document.addEventListener('keydown', handleKeyDown);
  document.addEventListener('click', closePopoverOutside);
})

onUnmounted(() => {
  document.removeEventListener('keydown', handleKeyDown);
  document.removeEventListener('click', closePopoverOutside);
})

defineExpose({
  clearInpVal,
  handleInpFocus,
  inpVal,
})

</script>
<template>
  <NPopover
    trigger="hover"
    width="trigger"
    display-directive="show"
    content-style="padding: 0;"
    :show-arrow="false"
    :show="isOpen"
  >
    <template #trigger>
      <div class="popover-trigger" ref="popoverTriggerRef">
        <div class="chat-inp-outer border-[1px]" :class="[{ 'border-[#2454FF]': isFocusState }]">
          <ul class="chat-tools-inner py-[10px] px-[10px] bg-[#fcfcfc]" v-show="selectedOption">
            <li class="tools-tips space-x-[10px]">
              <span>与</span>            
              <p class="agent-name space-x-[5px]" @click="isOpen = true">
                <img src="https://static.fuxicarbon.com/userupload/db77ffe0cef843278a23b0d2db9505fa.png" alt="">
                <span>{{ selectedOption?.title }}</span>
              </p>
              <span>对话中</span>
            </li>
            <li class="tools-close" @click="selectedOption = null">
              <SvgIcon name="chat-icon-close-btn"></SvgIcon>
            </li>
          </ul>
          <div class="chat-inp-inner">
            <div class="inp-wrapper flex-1" @click="handleInpFocus">
              <NInput 
                class="flex-1"
                ref="inpRef" 
                type="textarea" 
                size="medium"
                placeholder="输入@，召唤智能体"
                v-model:value="inpVal" 
                :autosize="{ minRows: 1, maxRows: 5 }"
                @focus="focusInput"
                @blur="blurInput"
                @keypress="handleInpEnter"
              />
            </div>
            <div class="submit-btn">
              <button class="btn bg-[#1A2029] hover:bg-[#3C4148]" @click="handleBtnClick">
                <SvgIcon name="tool-send-plane" size="22" v-show="!modelLoading"></SvgIcon>
                <div style="color: #fff" class="la-ball-running-dots la-sm" v-show="modelLoading">
                  <div v-for="item in 5" :key="item"></div>
                </div>
              </button>
            </div>
          </div>
        </div>
        <div class="switch-inner pt-[8px] space-x-[6px]">
          <NSwitch size="small" v-model:value="switchStatus"></NSwitch>
          <span class="text-[12px] text-[#9E9E9E]">使用搜索增强</span>
        </div>
        <div class="masking-inner text-center text-[#2454FF]"></div>
      </div>
    </template>
  
    <div class="popover-inner" ref="popoverInnerRef">
      <div class="header">
        <span>选择智能体</span>
        <p class="tools-close" @click="isOpen = false">
          <SvgIcon name="chat-icon-close-btn"></SvgIcon>
        </p>
      </div>
      <NScrollbar style="max-height: 240px;">
        <div class="item" v-for="item, index in agentOptions" :class="['item', { active: highlightedIndex === index }]" @click="selectOption(index)">
          <p class="icon">
            <img :src="item.banner" alt="">
          </p>
          <p class="ml-[10px] space-x-[5px] text">
            <span class="text-[15px]">{{item.title}}</span>
            <!-- <span class="text-[#888] text-[14px]">这里可以补充个描述</span> -->
          </p>
        </div>
      </NScrollbar>
    </div>
  </NPopover>
</template>

<style scoped lang="scss">
.chat-inp-outer {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0px 3px 12px 0px #97D3FF40;

  .chat-tools-inner {
    @include flex(x, center, between);

    .tools-tips {
      @include flex(x, center, start);
      color: #666;
      font-size: 14px;
      
      .agent-name {
        @include flex(x, center, start);
        font-weight: bold;
        color: #333;
        cursor: pointer;

        img {
          width: 14px;
          height: 14px;
        }
      }
    }
  }

  .chat-inp-inner {
    position: relative;
    @include flex(x, center, between);
    background: #fff;

    .inp-wrapper {
      padding: 17px 0px 17px 34px;
    }

    .submit-btn {
      @include flex(x, center, center);
      width: 84px;

      .btn {
        @include flex(x, center, center);
        width: 50px;
        height: 32px;
        border-radius: 32px;
        transition: all .3s;
      }
    }
  }
}

.popover-inner {
  .header {
    @include flex(x, center, between);
    padding-bottom: 8px;
    font-size: 14px;
    color: #666;
  }

    .item {
      @include flex(x, center, start);
      padding: 8px 10px;
      cursor: pointer;
      &:hover {
        background: #f0fafe;
      }
      .icon {
        @include flex(x, center, center);
        width: 24px;
        height: 24px;
        border-radius: 100%;
        background: #e9eef8;
        img {
          width: 16px;
          height: 16px;
        }
      }
      .text {
        text-align: left;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
    }
    .active {
      background: #f0fafe;
    }
  }

.tools-close {
  @include flex(x, center, center);
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  &:hover {
    background: #e9eef8;
  }
}

.masking-inner {
  position: absolute;
  top: -30px;
  left: 0;
  width: 100%;
  height: 30px;
  background: linear-gradient(180deg, rgba(232, 241, 250, 0) 0%, #E7F0FA 95%);
}


</style>