import Schema from 'async-validator'

const step1Descriptor = {
  busTypeData: {
    type: 'object',
    required: true,
    fields: {
      userType: { type: 'string', required: true },
      isDelegate: { type: 'boolean', required: true }
    }
  },
  carCardData: {
    type: 'object',
    required: true,
    fields: {
      vin: { type: 'string', required: true },
      iccidList: {
        type: 'array',
        required: true,
        defaultField: {
          type: 'object',
          required: true,
          fields: {
            iccid: { type: 'string' },
            state: { type: 'string', required: true },
            _formId: { type: 'number', required: true },
            _inputId: { type: 'number', required: true }
          }
        }
      }
    }
  }
}
const step1Validator = new Schema(step1Descriptor)

// 第2步车主数据校验
const step2MasterDataDescriptor = {
  isDelegate: {
    type: 'boolean',
    required: true
  },
  vin: {
    type: 'string',
    required: true
  },
  userType: {
    type: 'string',
    required: true
  },
  masterData: {
    type: 'object',
    required: true,
    fields: {
      basicInfoData: {
        type: 'object',
        required: true,
        fields: {
          name: { type: 'string', required: true },
          sex: { type: 'string', required: true },
          vin: { type: 'string', required: true }
        }
      },
      certInfoData: {
        type: 'object',
        required: true,
        fields: {
          address: { type: 'string', required: true },
          connectAddress: { type: 'string', required: false },
          no: { type: 'string', required: true },
          type: { type: 'string', required: true },
          validDate: { type: 'string', required: true },
          photoUrls: {
            type: 'array',
            required: true,
            defaultField: {
              type: 'object',
              required: true,
              fields: {
                id: { type: 'number', required: true },
                url: { type: 'object' }
              }
            }
          }
        }
      },
      fileInfoData: {
        type: 'object',
        required: true,
        fields: {
          protocolUrl: {
            type: 'array',
            required: true,
            defaultField: {
              type: 'object',
              required: true,
              fields: {
                accessUrl: { type: 'string' }
              }
            }
          },
          contractUrl: {
            type: 'array',
            defaultField: {
              type: 'object',
              required: true,
              fields: {
                accessUrl: { type: 'string' }
              }
            }
          },
          billUrl: {
            type: 'array',
            defaultField: {
              type: 'object',
              required: true,
              fields: {
                accessUrl: { type: 'string' }
              }
            }
          },
          transferUrl: {
            type: 'array',
            defaultField: {
              type: 'object',
              required: true,
              fields: {
                accessUrl: { type: 'string' }
              }
            }
          }
        }
      },
      concatInfoData: {
        type: 'object',
        required: true,
        fields: {
          phone: { type: 'string', required: true },
          veCode: { type: 'string' }
        }
      }
    }
  }
}
const step2MasterDataValidator = new Schema(step2MasterDataDescriptor)

// 第2步代办人数据校验
const step2DelegateDataDescriptor = {
  delegateData: {
    type: 'object',
    required: true,
    fields: {
      basicInfoData: {
        type: 'object',
        required: true,
        fields: {
          name: { type: 'string', required: true },
          sex: { type: 'string', required: true }
        }
      },
      certInfoData: {
        type: 'object',
        required: true,
        fields: {
          address: { type: 'string', required: true },
          connectAddress: { type: 'string', required: false },
          no: { type: 'string', required: true },
          type: { type: 'string', required: true },
          validDate: { type: 'string', required: true },
          photoUrls: {
            type: 'array',
            required: true,
            defaultField: {
              type: 'object',
              required: true,
              fields: {
                id: { type: 'number', required: true },
                url: { type: 'object' }
              }
            }
          }
        }
      },
      fileInfoData: {
        type: 'object',
        required: true,
        fields: {
          fileList: {
            type: 'array',
            required: true,
            defaultField: {
              type: 'object',
              required: true,
              fields: {
                accessUrl: { type: 'string', required: true }
              }
            }
          }
        }
      },
      concatInfoData: {
        type: 'object',
        required: true,
        fields: {
          phone: { type: 'string', required: true },
          veCode: { type: 'string' }
        }
      }
    }
  }
}
const step2DelegateDataValidator = new Schema(step2DelegateDataDescriptor)

export default {
  methods: {
    step1Validate(data = {}) {
      return new Promise((resolve, reject) => {
        step1Validator.validate(data || {}, (errors, fields) => {
          if (errors) {
            resolve(false)
            return
          }
          resolve(true)
        })
      })
    },
    step2Validate(data = {}) {
      data = data || {}

      const masterData = {
        isDelegate: data.isDelegate,
        vin: data.vin,
        userType: data.userType,
        masterData: data.masterData
      }

      return new Promise((resolve, reject) => {
        if (data.isDelegate) {
          step2MasterDataValidator.validate(masterData, (errors, fields) => {
            if (errors) {
              resolve(false)
              return
            }
            step2DelegateDataValidator.validate({
              delegateData: data.delegateData
            }, (errors, fields) => {
              if (errors) {
                resolve(false)
                return
              }
              resolve(true)
            })
          })
          return
        }
        step2MasterDataValidator.validate(masterData, (errors, fields) => {
          if (errors) {
            resolve(false)
            return
          }
          resolve(true)
        })
      })
    }
  }
}
