import { MESSAGE_TYPE } from '../constants';
import parseMessageToJSON from './parse';

import alarmIcon from '../images/types/alarm.png';
import approveIcon from '../images/types/approve.png';
import meetingIcon from '../images/types/meeting.png';
import noticeIcon from '../images/types/notice.png';
import taskIcon from '../images/types/task.png';
import workIcon from '../images/types/work.png';
import writeIcon from '../images/types/write.png';
import otherIcon from '../images/types/other.png';
import recoverIcon from '../images/types/recover.png';

export const getMessageTypeIcon = message => {
  const { messageType, infoType, infoContent = {} } = message;
  const { alarmState = 0 } = infoContent;
  let icon = otherIcon;
  if (infoType === '通用报警') {
    icon = alarmIcon;
    if (alarmState === 1) {
      icon = recoverIcon;
      return icon;
    }
  } else if (infoType === '系统通知') {
    icon = noticeIcon;
  } else if (infoType === '工单提醒') {
    icon = workIcon;
  }
  const { webIcon } = message;
  if (webIcon) return `${window.location.origin}/${webIcon}`;
  if (!messageType) return icon;
  if (messageType === '工时填报') {
    icon = writeIcon;
  } else if (messageType.indexOf('审批') > -1) {
    icon = approveIcon;
  } else if (messageType.indexOf('会议') > -1) {
    icon = meetingIcon;
  } else if (messageType.indexOf('任务') > -1) {
    icon = taskIcon;
  }
  return icon;
};

export const getMessageClasses = messageType => {
  let infoType = '';
  switch (messageType) {
    // 报警类
    case 'SCADA报警':
    case '通用报警':
      infoType = MESSAGE_TYPE.ALARM_TYPE;
      break;
    // 工单类
    case '工单流程':
    case '工单提醒':
      infoType = MESSAGE_TYPE.CASE_TYPE;
      break;
    // 系统消息类
    case '系统通知':
    case '系统消息':
      infoType = MESSAGE_TYPE.SYS_TYPE;
      break;
    // 其他类
    default:
      infoType = MESSAGE_TYPE.UNKNOWN;
      break;
  }
  return infoType;
};

/**
 * 消息语音单位转换
 * 说明:消息通过语音播放时,需要将一些字母单位转换成文字,不然会当做字母播放。
 * @param {*} msg
 * @returns
 */
export const replaceSpeak = msg => {
  msg = msg.replaceAll(' ', '');
  msg = msg.replace(/MPa/gi, '兆帕');
  msg = msg.replace(/m³\/h/gi, '立方米每小时');
  msg = msg.replace(/m³/gi, '立方米');
  msg = msg.replace(/kWh/gi, '千瓦时');
  msg = msg.replace(/kW/gi, '千瓦');
  msg = msg.replace(/min/gi, '分钟');
  msg = msg.replace(/m/gi, '米');
  msg = msg.replace(/A/gi, '安');
  msg = msg.replace(/V/gi, '伏');
  msg = msg.replace(/Hz/gi, '赫兹');
  msg = msg.replace(/h/gi, '小时');
  return msg;
};

// eslint-disable-next-line arrow-body-style
export const generatedId = () => {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
    .replace(/[xy]/g, function(c) {
      // eslint-disable-next-line no-bitwise
      const r = (Math.random() * 16) | 0;
      // eslint-disable-next-line no-bitwise
      const v = c === 'x' ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    })
    .toUpperCase();
};

export { parseMessageToJSON };