import { TaskBatchRemoteModel, TServerReadModel } from "@/types";
import { number } from "echarts";
import Dic from "@/dictionary/aicbqcfg";
import global from '@/until/global';
import tool from "./tool";
import taskDic from '@/dictionary';

/**
 * 根据服务侧读数对象获取goodflag代表的含义
 * @param model 
 * @returns 
 */
function getGoodFlagContent(model: TServerReadModel, digits: any) {
  if (model != null) {
    //return model.identValue;
    return formatDecimal(model.identValue, digits);
    if (model.goodFlag === '1') {
      return formatDecimal(model.identValue, digits);
    } else if (model.goodFlag === '2') {
      return '--'
    } else if (model.goodFlag === '3') {
      return '**'
    } else if (model.goodFlag === '4') {
      return '!!'
    } else if (model.goodFlag === '5') {
      return '~~'
    } else if (model.goodFlag === '6') {
      return '++'
    }
    else {
      return '??'
    }
  } else return '';
}

/**
 * 根据服务器侧读数对象获取其ToolTip显示内容
 * @param model
 * @returns 
 */
function getServerReadToolTip(model: TServerReadModel) {
  if (model != null) {
    if (model.goodFlag === '1') {
      if (model.resultCode === '1') {
        return '高概率识别正确';
      } else if (model.resultCode === '7') {
        return '一般概率识别正确';
      }
      return model.identValue;
    } else if (model.goodFlag === '2') {
      if (model.resultCode === '3') {
        return '不符合最大水流原则';
      } else if (model.resultCode === '7') {
        return '不符合递增原则';
      } else if (model.resultCode === '9') {
        return '没有找到足够目标';
      }
      return '推理异常'
    } else if (model.goodFlag === '3') {
      return '调用异常'
    } else if (model.goodFlag === '4') {
      return '没有配置'
    } else if (model.goodFlag === '5') {
      return '图片异常'
    } else if (model.goodFlag === '6') {
      return '扩展标识'
    }
    else {
      return '未知状态'
    }
  } else return '';
}

/**
 * 日志上传等级名称
 * @param val 
 * @returns 
 */
function getLogUploadLevelLabel(val: string) {
  for (let index = 0; index < Dic.logUploadLevel.length; index++) {
    if (Dic.logUploadLevel[index].value.toString() == val) {
      return Dic.logUploadLevel[index].label;
    }
  }
  return "";
}

/**
 * 获取水表尾数单位名称
 * @param type 
 * @returns 
 */
function getUnitType(type: number) {
  for (let index = 0; index < Dic.unitTypeMap.length; index++) {
    if (Dic.unitTypeMap[index].value == type) {
      return Dic.unitTypeMap[index].label;
    }
  }
  return "";
};

/**
 * 翻译压力表精度范围
 * @param type 
 * @returns 
 */
function getpgPrecisionType(type: number) {
  for (let index = 0; index < Dic.pgPrecisionTypeMap.length; index++) {
    if (Dic.pgPrecisionTypeMap[index].value == type) {
      return Dic.pgPrecisionTypeMap[index].label;
    }
  }
  return "";
};

/**
 * 翻译水表口径
 * @param type 
 * @returns 
 */
function getDnType(type: number) {
  for (let index = 0; index < Dic.dnTypeMap.length; index++) {
    if (Dic.dnTypeMap[index].value == type) {
      return Dic.dnTypeMap[index].label;
    }
  }
  return "";
};

/**
 * 解析大图周规则
 * @param weeks 
 * @returns 
 */
function jxweekString(weeks: string) {
  let reString = "";
  reString += weeks[0] == "1" ? "一" : "";
  reString += weeks[1] == "1" ? "二" : "";
  reString += weeks[2] == "1" ? "三" : "";
  reString += weeks[3] == "1" ? "四" : "";
  reString += weeks[4] == "1" ? "五" : "";
  reString += weeks[5] == "1" ? "六" : "";
  reString += weeks[6] == "1" ? "日" : "";

  return reString === '' ? '不上传' : reString;
};

/**
 * 获取指定日期上传大图的规则Tip
 * @param val 
 * @param type 0:task_remote_cfg,1:t_aicbq_cfg
 */
function getWiTaskSpecdateTip(val: any, type: number) {
  let msg = val.wiTaskSpecdateEnable == '1' ? '启用' : '禁用';
  if (val.wiTaskSpecdateEnable !== '1') {
    return msg;
  }
  //msg+=val.wiTaskSpecdateDate==null?'':val.wiTaskSpecdateDate;
  //console.log(val.wiTaskSpecdateDate);

  msg += ' | 日期:';
  msg += val.wiTaskSpecdateDate == null ? '' : tool.dateFormat(val.wiTaskSpecdateDate, 'YYYYMMDD', 'YYYY-MM-DD');
  msg += ' | 持续:';
  msg += val.wiTaskSpecdateDays == null ? '' : val.wiTaskSpecdateDays;
  msg += '天 | 每天:';
  msg += val.wiTaskSpecdateTimes == null ? '' : val.wiTaskSpecdateTimes;
  msg += '次';
  return msg;
}

/**
 * 获取日志上传规则的Tip
 * @param val 
 * @param type 0:task_remote_cfg,1:t_aicbq_cfg
 */
function getLogUploadRulesTip(val: any, type: number) {
  let msg = val.logUploadEnable == '1' ? '启用' : '禁用';
  if (val.logUploadEnable !== '1') {
    return msg;
  }
  //msg+=val.logUploadDate==null?'':val.logUploadDate;
  msg += ' | 日期:';
  msg += val.logUploadDate == null ? '' : tool.dateFormat(val.logUploadDate, 'YYYYMMDD', 'YYYY-MM-DD');
  msg += ' | 持续:';
  msg += val.logUploadDays == null ? '' : val.logUploadDays;
  msg += '天 | 每天:';
  msg += val.logUploadTimes == null ? '' : val.logUploadTimes;
  msg += '次 | MCU日志等级:';
  msg += global.getLogUploadLevelLabel(val.logUploadMcuLevel);
  msg += ' | AI日志等级:';
  msg += global.getLogUploadLevelLabel(val.logUploadAiLevel);
  return msg;
}

/**
 * 获取人工配置数字坐标区域的Tip
 * @param val 
 */
function getImageConfigip(val: any) {
  let msg = val.imageConfigFlag == '1' || val.imageConfigFlag == '01' ? '启用' : '禁用';
  if (val.imageConfigFlag !== '1') {
    return msg;
  }
  msg += ' | 旋转角度:';
  msg += val.imageLeanAngle == null ? '' : val.imageLeanAngle;
  msg += ' | 区域坐标:[';
  msg += val.imageNumRegionXMin;
  msg += ',';
  msg += val.imageNumRegionYMin;
  msg += '][';
  msg += val.imageNumRegionXMax;
  msg += ',';
  msg += val.imageNumRegionYMax;
  msg += ']';
  return msg;
}

/**
 * 返回图片名称Tip
 * @param val 
 * @returns 
 */
function getImageNameTip(val: string) {
  if (val) {
    let index = val.indexOf('_');
    return index > -1 ? val.substring(index + 1) : val;
  }
  return '';
}

/**
 * 翻译IP1发送状态码
 * @param val 
 * @returns 
 */
function getIp1SendStatusTip(val: number) {
  if (val && val > -1) {
    let str = "IP1发送状态:"
    if (val === 0) {
      return str + '无任务';
    } else if (val === 1) {
      return str + '发送成功';
    } else if (val === 2) {
      return str + '发送成功,未收到应答';
    } else if (val === 11) {
      return str + '打开Socket错误';
    } else if (val === 12) {
      return str + 'NB写入错误';
    } else {
      return str + '未定义的错误码';
    }
  }
  return '';
}

/**
 * 获取批量任务状态(处理未创建的情况)
 * @param val 
 * @returns 
 */
function getTaskBatchState(val: TaskBatchRemoteModel) {
  if (val) {
    return taskDic.taskBatchState[val.finishedFlag];
  } else {
    return '未创建';
  }
}

/**
 * 是否是中文字符串
 * @param data 
 * @returns 
 */
function isChinese(data: string) {
  var pattern = new RegExp("[\u4E00-\u9FA5]+");
  return pattern.test(data);
}

/**
 * 是否是字母
 * @param data 
 * @returns 
 */
function isEnglish(data: string) {
  var pattern = new RegExp("[A-Za-z]+");
  return pattern.test(data);
}

/**
 * 是否是数值
 * @param data 
 * @returns 
 */
function isNumber(data: string) {
  var pattern = new RegExp("[0-9]+");
  return pattern.test(data);
}

/**
 * 调试信息字符串格式化
 * @param value 
 * @param index 
 * @returns 
 */
function debugInfoFormat(value: string, index: number) {
  value = index > 9 && value === '-' ? '0' : value;
  if (value.length === 1) {
    value = '\xa0\xa0\xa0\xa0' + value;
  } else if (value.length === 2) {
    value = '\xa0\xa0' + value;
  }
  return value + '\xa0\xa0'
}

/**
 * 去除非数字
 */
function removeNonNumbers(val: string) {
  return val.replace(/[^\-\d.]/g, '').replace(/\b(0+){2,}/g, '0').replace(/\-{2,}/g, '-').replace(/^\./g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
}

/**
 * 
 * @param value 格式化小数
 * @param digits 
 * @returns 
 */
function formatDecimal(value: any, digits: any) {
  return value == null || digits == null || digits == undefined ? value : value.toFixed(digits);
}

export default {
  getGoodFlagContent,
  getServerReadToolTip,
  isChinese,
  isEnglish,
  isNumber,
  debugInfoFormat,
  getLogUploadLevelLabel,
  getWiTaskSpecdateTip,
  getLogUploadRulesTip,
  getUnitType,
  getpgPrecisionType,
  getDnType,
  jxweekString,
  getTaskBatchState,
  removeNonNumbers,
  formatDecimal,
  getImageConfigip,
  getIp1SendStatusTip,
  getImageNameTip
}