import { ref } from 'vue';

/**ueEventAMFParse UE会话事件AMF 数据解析 */
function ueEventAMFParse(
  item: Record<string, any>
): false | Record<string, any> {
  let evData: Record<string, any> = item.eventJSON;
  if (typeof evData === 'string') {
    try {
      evData = JSON.parse(evData);
    } catch (error) {
      console.error(error);
    }
  }

  return {
    eType: 'amf_ue',
    eId: `amf_ue_${item.id}_${Date.now()}`,
    eTime: +item.timestamp,
    id: item.id,
    type: item.eventType,
    data: evData,
  };
}

/**ueEventMMEParse UE会话事件MME 数据解析 */
function ueEventMMEParse(
  item: Record<string, any>
): false | Record<string, any> {
  let evData: Record<string, any> = item.eventJSON;
  if (typeof evData === 'string') {
    try {
      evData = JSON.parse(evData);
    } catch (error) {
      console.error(error);
    }
  }

  return {
    eType: 'mme_ue',
    eId: `mme_ue_${item.id}_${Date.now()}`,
    eTime: +item.timestamp,
    id: item.id,
    type: item.eventType,
    data: evData,
  };
}

/**cdrEventIMSParse CDR会话事件IMS 数据解析 */
function cdrEventIMSParse(
  item: Record<string, any>
): false | Record<string, any> {
  let evData: Record<string, any> = item.cdrJSON || item.CDR;
  if (typeof evData === 'string') {
    try {
      evData = JSON.parse(evData);
    } catch (error) {
      console.error(error);
      return false;
    }
  }

  // 指定显示CDR类型MOC/MTSM
  if (!['MOC', 'MTSM'].includes(evData.recordType)) {
    return false;
  }

  return {
    eType: 'ims_cdr',
    eId: `ims_cdr_${item.id}_${Date.now()}`,
    eTime: +item.timestamp,
    id: item.id,
    data: evData,
  };
}

/**eventListParse 事件列表解析 */
export function eventListParse(
  type: 'ims_cdr' | 'amf_ue' | 'mme_ue',
  data: any
) {
  eventTotal.value += data.total;
  for (const item of data.rows) {
    let v: false | Record<string, any> = false;
    if (type === 'ims_cdr') {
      v = cdrEventIMSParse(item);
    }
    if (type === 'amf_ue') {
      v = ueEventAMFParse(item);
    }
    if (type === 'mme_ue') {
      v = ueEventMMEParse(item);
    }

    if (v) {
      eventData.value.push(v);
    }
  }

  // 激活选中
  if (eventData.value.length > 0) {
    eventId.value = eventData.value[0].eId;
  }
}

/**eventItemParseAndPush 事件项解析并添加 */
export async function eventItemParseAndPush(
  type: 'ims_cdr' | 'amf_ue' | 'mme_ue',
  item: any
) {
  let v: false | Record<string, any> = false;
  if (type === 'ims_cdr') {
    v = cdrEventIMSParse(item);
  }
  if (type === 'amf_ue') {
    v = ueEventAMFParse(item);
  }
  if (type === 'mme_ue') {
    v = ueEventMMEParse(item);
  }

  if (v) {
    eventData.value.unshift(v);
    eventTotal.value += 1;
    eventId.value = v.eId;
    await new Promise(resolve => setTimeout(resolve, 800));
    if (eventData.value.length > 20) {
      eventData.value.pop();
    }
  }
}

/**CDR+UE事件数据 */
export const eventData = ref<Record<string, any>[]>([]);
/**CDR+UE事件总量 */
export const eventTotal = ref<number>(0);
/**CDR/UE事件推送id */
export const eventId = ref<string>('');

/**属性复位 */
export function userActivityReset() {
  eventData.value = [];
  eventTotal.value = 0;
  eventId.value = '';
}
