/**
 * @description 条件子项
 */
interface DirectionItem {
  /**
   * 编号，注意与后端匹配
   */
  Id: number
  /**
   * 名称，用于显示
   */
  Name: string
  /**
   * 例外的规则类型
   */
  ExceptionMapping?: Array<number>
  /**
   * 是否不需要值
   */
  IsNotValue?: boolean
}

/**
 * @description 条件集合
 */
interface Direction {
  Id: number
  Remark?: string
  MappingType: number[]
  Childs: DirectionItem[]
}

/**
 * 自定义对象
 */
interface SelectModel {
  Name: string
  Type: number
  Direction: number
  StartValue: string
  EndValue: string
  IsCustom: number
}

/**
 * 初始化数据
 */
const DirectionRule = new Array<Direction>()

//#region 第一组用于多选之类的
DirectionRule.push({
  Id: 1,
  MappingType: [4],
  Childs: [
    {
      Id: 1,
      Name: '包含所有'
    },
    {
      Id: 2,
      Name: '包含任意'
    },
    {
      Id: 3,
      Name: '不包含'
    },
    {
      Id: 4,
      Name: '为空',
      IsNotValue: true
    },
    {
      Id: 5,
      Name: '不为空',
      IsNotValue: true
    }
  ]
})
//#endregion

//#region 第二组用于文本，多好文本，单选之类
DirectionRule.push({
  Id: 2,
  MappingType: [1, 2, 3],
  Childs: [
    {
      Id: 1,
      Name: '等于'
    },
    {
      Id: 2,
      Name: '不等于'
    },
    {
      Id: 3,
      Name: '为空',
      IsNotValue: true
    },
    {
      Id: 4,
      Name: '不为空',
      IsNotValue: true
    },
    {
      Id: 5,
      Name: '模糊',
      ExceptionMapping: [3]
    }
  ]
})
//#endregion

//#region 第三组组用于文本，多好文本，单选之类
DirectionRule.push({
  Id: 2,
  MappingType: [7],
  Childs: [
    {
      Id: 1,
      Name: '等于'
    },
    {
      Id: 2,
      Name: '大于'
    },
    {
      Id: 3,
      Name: '大于等于'
    },
    {
      Id: 4,
      Name: '小于'
    },
    {
      Id: 5,
      Name: '小于等于'
    },
    {
      Id: 6,
      Name: '不等于'
    }
  ]
})
//#endregion

/**
 * 获取指定类型的比较规则
 * @param type 类型
 * @returns 返回值
 */
const QueryDirectionByType = (type: number): DirectionItem[] => {
  const temp = DirectionRule.filter(x => x.MappingType.indexOf(type) != -1)
  if (temp && temp.length > 0) {
    const tempChild = temp[0].Childs.filter(y => !y.ExceptionMapping || y.ExceptionMapping.indexOf(type) == -1)

    if (tempChild && tempChild.length > 0) {
      return tempChild
    } else {
      console.warn('已匹配规则，但是所有子项都已排除该类型的匹配')
      return new Array<DirectionItem>()
    }
    return tempChild
  } else {
    console.warn('未受到支持的数据类型' + type)
    return new Array<DirectionItem>()
  }
}

//#region  时间范围验证
const RangeTypeValidateRule = (model: SelectModel): string => {
  if (model.Type == 6 && model.StartValue.trim() != '' && model.EndValue.trim() != '' && model.Direction == 0) {
    return ''
  } else {
    return '数据不合法，请检查'
  }
}
//#endregion

//#region  单选/多选/文本验证
const TextTypeValidateRule = (model: SelectModel): string => {
  const SupportList = [1, 2, 3, 4, 7]
  if (SupportList.indexOf(model.Type) != -1) {
    const directModel = DirectionRule.filter(x => x.MappingType.indexOf(model.Type) != -1)
    if (directModel && directModel.length > 0) {
      const directItemModel = DirectionRule[0].Childs.find(x => (x.Id = model.Direction))
      if (directItemModel) {
        if (!directItemModel.IsNotValue && model.StartValue.trim() != '') {
          return ''
        } else if (directItemModel.IsNotValue) {
          return ''
        } else {
          return '数据不合法，请检查'
        }
      } else {
        return '数据不合法，请检查'
      }
    } else {
      return '数据不合法，请检查'
    }
  } else {
    return '数据不合法，请检查'
  }
}
//#endregion

//#region  多选日期验证
const DateTypeValidateRule = (model: SelectModel): string => {
  if (model.Type == 5 && model.StartValue.trim() != '') {
    return ''
  } else {
    return '数据不合法，请检查'
  }
}
//#endregion

//#region 验证数据
const ValidateCoustom = (list: SelectModel[]): string => {
  console.log(list)
  let msg = ''
  //定义映射规则
  for (let i = 0; i < list.length; i++) {
    if ([1, 2, 3, 4, 7].indexOf(list[i].Type) != -1) {
      msg = TextTypeValidateRule(list[i])
    } else if (list[i].Type == 5) {
      msg = DateTypeValidateRule(list[i])
    } else if (list[i].Type == 6) {
      msg = RangeTypeValidateRule(list[i])
    } else {
      console.log(list[i])
      msg = '不存在的数据验证规则'
    }
    if (msg != '') {
      msg = `第${i + 1}行 ${msg}`
      break
    }
  }
  return msg
}

const MappingAuto = (list: SelectModel[]): SelectModel[] => {
  const rule = [
    {
      type: 1,
      mapping: [4]
    },
    {
      type: 2,
      mapping: [1, 2, 3]
    },
    {
      type: 3,
      mapping: [5]
    },
    {
      type: 4,
      mapping: [6]
    },
    {
      type: 5,
      mapping: [7]
    }
  ]
  list.forEach(x => {
    rule.forEach(y => {
      if (y.mapping.indexOf(x.Type)!=-1) {
        x.Type = y.type
      }
    })
  })
  console.log(list)
  return list
}

//#endregion

const UseDirection = {
  QueryDirectionByType,
  ValidateCoustom,
  MappingAuto
}

export { UseDirection }
