import { Rule } from 'ant-design-vue/lib/form/interface';
/**
 * 常用表单自定义验证规则
 *
 */

export const validatorLongitude = (_rule: Rule, value: string) => {
  const reg =
    /^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,20})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,20}|180)$/;
  if (!value) {
    return Promise.reject('请输入经度');
  } else if (!reg.test(value)) {
    return Promise.reject('经度整数部分为0-180,小数部分为0到6位!');
  }
  return Promise.resolve();
};
export const validatorLatitude = (_rule: Rule, value: string) => {
  const reg = /^(\-|\+)?([0-8]?\d{1}\.\d{0,20}|90\.0{0,20}|[0-8]?\d{1}|90)$/;
  if (value === '') {
    return Promise.reject('请输入维度');
  } else if (!reg.test(value)) {
    return Promise.reject('纬度整数部分为0-90,小数部分为0到6位!');
  }

  return Promise.resolve();
};

//正整数并且可以是英文
export const numberAndEngLishRule: Rule[] = [
  {
    validator: (_, value: string) => {
      const reg1 = /^[0-9]*[1-9][0-9]*$/;
      const reg2 = /^[A-Za-z0-9]+$/;
      if (!reg1.test(value) && !reg2.test(value)) {
        return Promise.reject('不能为负数');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

//数字和非负数
export const numberAndNonegativeRule: Rule[] = [
  {
    validator: (_, value: string) => {
      const reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/;
      if (!reg.test(value)) {
        return Promise.reject('不能为负数或者不能输入非数字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

// 数字验证
export const numberRule: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      const reg = /^[0-9]{8}$/;
      if (!reg.test(value)) {
        return Promise.reject('只能输入数字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];
export const phoneRegexp = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;

//中英文正则
export const chineseAndEnlishRule: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      const reg = /^[a-zA-Z\u4e00-\u9fa5]+$/;
      if (value === '' || value === undefined) {
        return Promise.reject('请输入');
      } else if (!reg.test(value)) {
        return Promise.reject('格式不正确');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

// 手机号验证
export const phoneRule: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
      if (value === '') {
        return Promise.reject('请输入手机号码');
      } else if (!reg.test(value)) {
        return Promise.reject('手机号格式不正确');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

// 电子邮箱验证
export const emailRule: Rule[] = [
  {
    validator: (_, value: string) => {
      const reg = /^\w{3,}(\.\w+)*@[A-z0-9]+(\.[A-z]{2,5}){1,2}$/;
      if (!value) {
        return Promise.resolve();
      } else if (!reg.test(value)) {
        return Promise.reject('电子邮箱格式不正确');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

// 中文正则
export const ChineseRegexp = /[\u4E00-\u9FA5]/;

//字母或者数字正则
export const allowLetterAndNumberRegExp = /^[A-Za-z0-9]+$/;

// 电子邮箱正则
export const EmailRegexp = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;

// 手机号正则
export const PhoneRegexp = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;

//中文正则
export const ChineseRegVerify: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      const ChineseRegexp = /[\u4E00-\u9FA5]/;
      if (!ChineseRegexp.test(value)) {
        return Promise.reject('用户名只能是中文');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

/**
 * 视频管理
 */
export const CameraMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 50) {
        return Promise.reject('视频标题长度不超过200字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

export const CameraVideoUrl: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      const ChineseRegexp = /[\u4E00-\u9FA5]/;
      if (ChineseRegexp.test(value)) {
        return Promise.reject('输入内容不能是中文');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

export const CameraVideoStreamUrl: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      const ChineseRegexp =
        /^https?:\/\/(.+\/)+.+(\.(swf|avi|flv|mpg|rm|mov|wav|asf|3gp|mkv|rmvb|mp4|m3u8))$/i;
      if (!ChineseRegexp.test(value)) {
        return Promise.reject('请输入正确格式的视频地址');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];
export const CameraNumberUrl: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      const ChineseRegexp = /^[A-Za-z0-9]+$/;
      if (!ChineseRegexp.test(value)) {
        return Promise.reject('请输入正确格式的视频编号');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

//站内通知
export const NotificationTitleMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 50) {
        return Promise.reject('标题长度不超过200字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

export const NotificationContentMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 50) {
        return Promise.reject('内容长度不超过50字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

export const DeviceNameMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 30) {
        return Promise.reject('设备名称长度不超过30字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

export const DeviceProfileIdMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 36) {
        return Promise.reject('所属产品长度不超过36字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

export const DeviceOrgIdMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 36) {
        return Promise.reject('组织长度不超过36字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

export const DeviceLabelMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 255) {
        return Promise.reject('设备标签不超过255字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];
export const DeviceDescriptionlMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 500) {
        return Promise.reject('备注不超过500字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];
export const DeviceIdMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 36) {
        return Promise.reject('id不超过36字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

export const DeviceTenantIdMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 36) {
        return Promise.reject('租户Code不超过36字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

export const DeviceTbDeviceIdMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 36) {
        return Promise.reject('tbDeviceId不超过36字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

export const DeviceUserNameMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 50) {
        return Promise.reject('用户名长度不超过50字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

export const DeviceQueryUserNameMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 50) {
        return Promise.reject('设备名称长度不超过50字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

export const DeviceProfileQueryUserNameMaxLength: Rule[] = [
  {
    required: true,
    validator: (_, value: string) => {
      if (String(value).length > 255) {
        return Promise.reject('配置名称长度不超过255字');
      }
      return Promise.resolve();
    },
    validateTrigger: 'blur',
  },
];

// enum MediaType {
//   MP4 = 'mp4',
//   M3U8 = 'm3u8',
// }

export const MediaTypeValidate: Rule[] = [
  {
    required: true,
    // validator: (_, value: string) => {
    //   // const reg = /(?:.*)(?<=\.)/;
    //   // const type = (value || '').replace(reg, '');
    //   // if (type !== MediaType.M3U8) {
    //   //   return Promise.reject('视频流只支持m3u8格式');
    //   // }
    //   return Promise.resolve();
    // },
    validateTrigger: 'blur',
  },
];
