<script setup lang="ts">
import { onBeforeUnmount, onMounted, reactive, ref } from 'vue';
import svgBase from '@/assets/svg/base.svg';
import svgUserIMS from '@/assets/svg/userIMS.svg';
import svgUserSMF from '@/assets/svg/userSMF.svg';
import useI18n from '@/hooks/useI18n';
import Topology from './components/Topology/index.vue';
import NeResources from './components/NeResources/index.vue';
import UserActivity from './components/UserActivity/index.vue';
import AlarnTypeBar from './components/AlarnTypeBar/index.vue';
import UPFFlow from './components/UPFFlow/index.vue';
import { listSub } from '@/api/neUser/sub';
import { listUENumBySMF } from '@/api/neUser/smf';
import { listUENumByIMS } from '@/api/neUser/ims';
import { listBase5G } from '@/api/neUser/base5G';

import {
  graphNodeClickID,
  graphState,
  notNeNodes,
  graphNodeStateNum,
  neStateRequestMap,
} from './hooks/useTopology';
import { upfTotalFlow, upfTFActive } from './hooks/useUPFTotalFlow';
import { useFullscreen } from '@vueuse/core';
import useWS from './hooks/useWS';
import useAppStore from '@/store/modules/app';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';

const appStore = useAppStore();
const { t } = useI18n();
const { wsSend, cdrEventSend, ueEventSend, upfTFSend } = useWS();

/**概览状态类型 */
type SkimStateType = {
  /**UDM签约用户数量 */
  udmSubNum: number;
  /**SMF在线用户数 */
  smfUeNum: number;
  /**IMS在线用户数 */
  imsUeNum: number;
  /**基站数量 */
  nbNum: number;
};

/**概览状态信息 */
let skimState: SkimStateType = reactive({
  udmSubNum: 0,
  smfUeNum: 0,
  imsUeNum: 0,
  nbNum: 0,
});

/**总览节点 */
const viewportDom = ref<HTMLElement | null>(null);
const { isFullscreen, toggle } = useFullscreen(viewportDom);

/**10s调度器 */
const stateInterval = ref<any>(null);

/**网元状态定时器 */
const stateTimeout = ref<any>(null);

/**查询网元状态 */
function fnGetState() {
  clearTimeout(stateTimeout.value);
  // 获取节点状态
  for (const node of graphState.data.nodes) {
    if (notNeNodes.includes(node.id)) continue;
    const { neType, neId } = node.neInfo;
    if (!neType || !neId) continue;
    // 请求标记检查避免重复发送
    if (neStateRequestMap.value.get(neType)) continue;
    neStateRequestMap.value.set(neType, true);

    wsSend({
      requestId: `neState_${neType}_${neId}`,
      type: 'ne_state',
      data: {
        neType: neType,
        neId: neId,
      },
    });
  }

  stateTimeout.value = setTimeout(() => fnGetState(), 10_000);
}

/**初始数据函数 */
function InitData() {
  fnGetState(); // 获取网元状态
  cdrEventSend();
  ueEventSend();
  upfTFSend(0);
  upfTFSend(7);
  upfTFSend(30);

  stateInterval.value = setInterval(() => {
    upfTFActive.value = upfTFActive.value >= 2 ? 0 : upfTFActive.value + 1;
    if (upfTFActive.value === 0) {
      upfTFSend(7);
    } else if (upfTFActive.value === 1) {
      upfTFSend(30);
    } else if (upfTFActive.value === 2) {
      upfTFSend(0);
    }
  }, 10_000);
}

onMounted(() => {
  Promise.allSettled([
    listSub({
      neid: '001',
      pageNum: 1,
      pageSize: 1,
    }),
    listBase5G({
      neType: 'AMF',
      neId: '001',
      id: '',
      pageNum: 1,
      /**每页条数 */
      pageSize: 20,
    }),
    listUENumBySMF('001'),
    listUENumByIMS('001'),
  ])
    .then(resArr => {
      if (resArr[0].status === 'fulfilled') {
        const res = resArr[0].value;
        if (res.code === RESULT_CODE_SUCCESS) {
          skimState.udmSubNum = res.total;
        }
      }

      if (
        resArr[1].status === 'fulfilled' &&
        resArr[1].value.code === RESULT_CODE_SUCCESS &&
        Array.isArray(resArr[1].value.rows)
      ) {
        skimState.nbNum = resArr[1].value.rows.length;
      }

      if (resArr[2].status === 'fulfilled') {
        const res = resArr[2].value;
        if (res.code === RESULT_CODE_SUCCESS) {
          skimState.smfUeNum = res.data;
        }
      }

      if (resArr[3].status === 'fulfilled') {
        const res = resArr[3].value;
        if (res.code === RESULT_CODE_SUCCESS) {
          skimState.imsUeNum = res.data;
        }
      }
    })
    .finally(() => {
      InitData();
    });
});

onBeforeUnmount(() => {
  clearTimeout(stateTimeout.value);
  clearTimeout(stateInterval.value);
});
</script>

<template>
  <div class="viewport" ref="viewportDom">
    <div class="brand">
      <div
        class="brand-title"
        @click="toggle"
        :title="t('views.dashboard.overview.fullscreen')"
      >
        {{ t('views.dashboard.overview.title') }}
        <FullscreenExitOutlined v-if="isFullscreen" />
        <FullscreenOutlined v-else />
      </div>
      <div class="brand-desc">{{ appStore.appName }}</div>
    </div>

    <div class="column">
      <!--概览-->
      <div class="skim panel">
        <div class="inner">
          <h3>
            <IdcardOutlined style="color: #68d8fe" />&nbsp;&nbsp;
            {{ t('views.dashboard.overview.skim.userTitle') }}
          </h3>
          <div class="data">
            <div class="item">
              <div>
                <UserOutlined
                  style="color: #4096ff; margin-right: 8px; font-size: 1.1rem"
                />
                {{ skimState.udmSubNum }}
              </div>
              <span>
                {{ t('views.dashboard.overview.skim.users') }}
              </span>
            </div>
            <div class="item" style="margin: 0 12px">
              <div>
                <img :src="svgUserIMS" style="width: 18px; margin-right: 8px" />
                {{ skimState.imsUeNum }}
              </div>
              <span>
                IMS {{ t('views.dashboard.overview.skim.session') }}
              </span>
            </div>
            <div class="item">
              <div>
                <img :src="svgUserSMF" style="width: 18px; margin-right: 8px" />
                {{ skimState.smfUeNum }}
              </div>
              <span>
                Data {{ t('views.dashboard.overview.skim.session') }}
              </span>
            </div>
          </div>
        </div>
      </div>
      <div class="skim panel">
        <div class="inner">
          <h3>
            <GlobalOutlined style="color: #68d8fe" />&nbsp;&nbsp;
            {{ t('views.dashboard.overview.skim.baseTitle') }}
          </h3>
          <div class="data">
            <div class="item">
              <div style="align-items: flex-start">
                <img
                  :src="svgBase"
                  style="width: 18px; margin-right: 8px; height: 2rem"
                />
                {{ skimState.nbNum }}
              </div>
              <span>
                {{ t('views.dashboard.overview.skim.base') }}
              </span>
            </div>
          </div>
        </div>
      </div>

      <!-- 用户行为 -->
      <div class="userActivity panel">
        <div class="inner">
          <h3>
            <WhatsAppOutlined style="color: #68d8fe" />&nbsp;&nbsp;
            {{ t('views.dashboard.overview.userActivity.title') }}
          </h3>
          <div class="chart">
            <UserActivity />
          </div>
        </div>
      </div>
    </div>
    <div class="column" style="flex: 4; margin: 1.333rem 0.833rem 0">
      <!-- 实时流量 -->
      <div class="upfFlow panel">
        <div class="inner">
          <h3>
            <AreaChartOutlined style="color: #68d8fe" />&nbsp;&nbsp;
            {{ t('views.dashboard.overview.upfFlow.title') }}
          </h3>
          <div class="chart">
            <UPFFlow />
          </div>
        </div>
      </div>
      <!-- 网络拓扑 -->
      <div class="topology panel">
        <div class="inner">
          <h3>
            <span>
              <ApartmentOutlined style="color: #68d8fe" />&nbsp;&nbsp;
              {{ t('views.dashboard.overview.topology.title') }}
            </span>
            <span>
              {{ t('views.dashboard.overview.topology.normal') }}:
              <span class="normal"> {{ graphNodeStateNum[0] }} </span>
              {{ t('views.dashboard.overview.topology.abnormal') }}:
              <span class="abnormal"> {{ graphNodeStateNum[1] }} </span>
            </span>
          </h3>
          <div class="chart">
            <Topology />
          </div>
        </div>
      </div>
    </div>
    <div class="column">
      <!-- 流量统计 -->
      <div class="upfFlowTotal panel">
        <div class="inner">
          <h3>
            <span>
              <SwapOutlined style="color: #68d8fe" />&nbsp;&nbsp;
              {{ t('views.dashboard.overview.upfFlowTotal.title') }}
            </span>

            <!-- 筛选 -->
            <div class="filter">
              <span
                :data-key="v"
                :class="{ active: upfTFActive === i }"
                v-for="(v, i) in ['0', '7', '30']"
                :key="v"
                @click="
                  () => {
                    upfTFActive = i;
                  }
                "
              >
                {{
                  v === '0'
                    ? '24' + t('common.units.hour')
                    : v + t('common.units.day')
                }}
              </span>
            </div>
          </h3>
          <div class="chart">
            <!-- 数据 -->
            <div class="data">
              <div class="item">
                <span>
                  <ArrowUpOutlined style="color: #597ef7" />
                  {{ t('views.dashboard.overview.upfFlowTotal.up') }}
                </span>
                <h4>{{ upfTotalFlow[upfTFActive].up }}</h4>
              </div>
              <div class="item">
                <span>
                  <ArrowDownOutlined style="color: #52c41a" />
                  {{ t('views.dashboard.overview.upfFlowTotal.down') }}
                </span>
                <h4>{{ upfTotalFlow[upfTFActive].down }}</h4>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 告警统计 -->
      <div class="alarmType panel">
        <div class="inner">
          <h3>
            <PieChartOutlined style="color: #68d8fe" />&nbsp;&nbsp;
            {{ t('views.dashboard.overview.alarmTypeBar.alarmSum') }}
          </h3>
          <div class="chart">
            <AlarnTypeBar />
          </div>
        </div>
      </div>
      <!-- 资源情况 -->
      <div class="resources panel">
        <div class="inner">
          <h3>
            <DashboardOutlined style="color: #68d8fe" />&nbsp;&nbsp;
            {{ t('views.dashboard.overview.resources.title') }}：
            {{ graphNodeClickID }}
          </h3>
          <div class="chart">
            <NeResources />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
@import url('./css/index.css');
</style>
