import Authorize from '@/api/setting-center/Authorize'

const log = console.log.bind(console)

class AuthorizeApi {
  constructor() {
    this.api = new Authorize()
  }

  cleanForTb(dataCell) {
    const d = dataCell
    const keyData = JSON.parse(d.keyData)
    // 字段有残缺暂未完成
    const result = {
      id: d.id,
      markName: d.name,
      authorizeChannel: d.name,
      apiAppKey: d.apiAppKey,
      apiAppSecret: d.apiAppSecret,
      promoteShare: d.promoteShare,
      selfShare: d.selfShare,
      androidSelfKey: keyData.android.selfKey,
      iosSelfKey: keyData.ios.slefKey,
      authorizeStatus: d.authStatus,
      authorizeTime: 10,
      // authorizeMode: '网页授权',
      syncStartDate: d.syncTime,
      useStatus: d.isUse,
      accessToken: keyData.accessToken
    }
    return result
  }

  cleanForJd(dataCell) {
    const d = dataCell
    const keyData = JSON.parse(d.keyData)
    // 字段有残缺暂未完成
    const result = {
      id: d.id,
      markName: d.name,
      authorizeChannel: d.name,
      apiAppKey: d.apiAppKey,
      apiAppSecret: d.apiAppSecret,
      promoteShare: d.promoteShare,
      selfShare: d.selfShare,
      androidSelfKey: keyData.android.selfKey,
      iosSelfKey: keyData.ios.selfKey,
      authorizeStatus: d.authStatus,
      authorizeTime: 10,
      // authorizeMode: '网页授权',
      syncStartDate: d.syncTime,
      useStatus: d.isUse,
      accessToken: keyData.accessToken
    }
    return result
  }

  cleanForPdd(dataCell) {
    const d = dataCell
    const keyData = JSON.parse(d.keyData)
    // 字段有残缺暂未完成
    const result = {
      id: d.id,
      markName: d.name,
      authorizeChannel: d.name,
      apiAppKey: d.apiAppKey,
      apiAppSecret: d.apiAppSecret,
      promoteShare: d.promoteShare,
      selfShare: d.selfShare,
      clientId: keyData.clientId,
      authorizeStatus: d.authStatus,
      authorizeTime: 10,
      // authorizeMode: '网页授权',
      syncStartDate: d.syncTime,
      useStatus: d.isUse,
      accessToken: keyData.accessToken
    }
    return result
  }

  cleanForWph(dataCell) {
    const d = dataCell
    const keyData = JSON.parse(d.keyData)
    // 字段有残缺暂未完成
    const result = {
      id: d.id,
      markName: d.name,
      authorizeChannel: d.name,
      apiAppKey: d.apiAppKey,
      apiAppSecret: d.apiAppSecret,
      promoteShare: d.promoteShare,
      selfShare: d.selfShare,
      androidSelfKey: keyData.android.selfKey,
      iosSelfKey: keyData.ios.selfKey,
      authorizeStatus: d.authStatus,
      authorizeTime: 10,
      // authorizeMode: '网页授权',
      syncStartDate: d.syncTime,
      useStatus: d.isUse,
      accessToken: keyData.accessToken
    }
    return result
  }

  cleanForSn(dataCell) {
    const d = dataCell
    const keyData = JSON.parse(d.keyData)
    // 字段有残缺暂未完成
    const result = {
      id: d.id,
      markName: d.name,
      authorizeChannel: d.name,
      apiAppKey: d.apiAppKey,
      apiAppSecret: d.apiAppSecret,
      promoteShare: d.promoteShare,
      selfShare: d.selfShare,
      authorizeStatus: d.authStatus,
      authorizeTime: 10,
      // authorizeMode: '网页授权',
      syncStartDate: d.syncTime,
      useStatus: d.isUse,
      accessToken: keyData.accessToken
    }
    return result
  }

  cleanForKl(dataCell) {
    const d = dataCell
    const keyData = JSON.parse(d.keyData)
    // 字段有残缺暂未完成
    const result = {
      id: d.id,
      markName: d.name,
      authorizeChannel: d.name,
      apiAppKey: d.apiAppKey,
      apiAppSecret: d.apiAppSecret,
      promoteShare: d.promoteShare,
      selfShare: d.selfShare,
      authorizeStatus: d.authStatus,
      authorizeTime: 10,
      // authorizeMode: '网页授权',
      syncStartDate: d.syncTime,
      useStatus: d.isUse,
      accessToken: keyData.accessToken
    }
    return result
  }

  cleanForDm(dataCell) {
    const d = dataCell
    const keyData = JSON.parse(d.keyData)
    // 字段有残缺暂未完成
    const result = {
      id: d.id,
      markName: d.name,
      authorizeChannel: d.name,
      apiAppKey: d.apiAppKey,
      apiAppSecret: d.apiAppSecret,
      promoteShare: d.promoteShare,
      selfShare: d.selfShare,
      authorizeStatus: d.authStatus,
      authorizeTime: 10,
      // authorizeMode: '网页授权',
      syncStartDate: d.syncTime,
      useStatus: d.isUse,
      accessToken: keyData.accessToken
    }
    return result
  }

  cleanByName(tableName, dataCell) {
    const nameMap = {
      taobao: this.cleanForTb,
      jd: this.cleanForJd,
      pdd: this.cleanForPdd,
      wph: this.cleanForWph,
      suning: this.cleanForSn,
      kaola: this.cleanForKl,
      duomai: this.cleanForDm
    }
    // log(tableName, 'in clean')
    let func = () => {
      log('name不存在于map中')
    }
    if (nameMap[tableName] !== undefined) {
      func = nameMap[tableName]
    }
    return func(dataCell)
  }

  clean(tableName, data) {
    const result = []
    data.map(item => {
      // const cell = func(item)
      const cell = this.cleanByName(tableName, item)
      result.push(cell)
    })
    // log(result, 'in clean')
    return result
  }

  // 根据tabs选中的activeName拉取对应的数据
  loadByName(tableName, page) {
    const l = this.api.loadByName(tableName, page)
    return l.then(res => {
      const data = res.data
      log(data, 'in load')
      return this.clean(tableName, data)
    })
  }

  // 提交数据，name是alliance-authorize中的activeName，data是要提交的数据
  updataByName(name, data) {
    // log(data, 'in updata')
    const d = JSON.stringify(data)
    const u = this.api.updataByName(name, d)
    return u
  }

  unionUse(data) {
    const d = JSON.stringify(data)
    const u = this.api.unionUse(d)
    return u
  }

  unionDelete(data) {
    const d = JSON.stringify(data)
    const u = this.api.unionDelete(d)
    return u
  }
}

const authorizeApi = new AuthorizeApi()

export default authorizeApi
