import baseAxios from '../index'
import { iOutLetDetail, iOutletMsg } from './types'

export default class AddressService {
  private prefix: string
  static instance: AddressService
  private max_len_gps: number
  static getInstance() {
    if (!AddressService.instance) {
      AddressService.instance = new AddressService()
    }
    return AddressService.instance
  }
  constructor() {
    this.prefix = '/address'
    this.max_len_gps = 10
  }

  // getGpses(data: { addrs: string[] }): Promise<string[]> {
  //   return new Promise((resolve, reject) => {
  //     const resultList = [] as string[]
  //     if (data.addrs.length) {
  //       for (
  //         let i = 0;
  //         i < Math.ceil(data.addrs.length / this.max_len_gps);
  //         ++i
  //       ) {
  //         const reqList = data.addrs.slice(
  //           i * this.max_len_gps,
  //           i * this.max_len_gps + this.max_len_gps,
  //         )
  //         this.__getGpses(reqList).then((ret) => {
  //           if (ret.code === 200) {
  //             resultList.push(...ret.data.gps)
  //             if (resultList.length === data.addrs.length) {
  //               resolve(resultList)
  //             }
  //           } else {
  //             reject('解析错误')
  //           }
  //         })
  //       }
  //     }
  //   })
  // }

  getGpses2(data: { addrs: string[] }): Promise<{ [props: string]: string }> {
    return new Promise((resolve, reject) => {
      const theMap = {} as { [props: string]: string }
      if (data.addrs.length) {
        for (
          let i = 0;
          i < Math.ceil(data.addrs.length / this.max_len_gps);
          ++i
        ) {
          const reqList = data.addrs.slice(
            i * this.max_len_gps,
            i * this.max_len_gps + this.max_len_gps,
          )
          this.__getGpses(reqList).then((ret) => {
            if (ret.code === 200) {
              const locations = reqList
              const gpses = ret.data.gps

              locations.map((location, index) => {
                theMap[location] = gpses[index]
              })
              if (Object.keys(theMap).length === data.addrs.length) {
                resolve(theMap)
              }
            } else {
              reject('解析错误')
            }
          })
        }
      }
    })
  }
  __getGpses(addrs: string[]) {
    if (addrs.length > 10) {
      throw new Error('长度超过10')
    } else {
      return baseAxios<{ gps: string[] }>({
        url: this.prefix + '/getGps',
        method: 'post',
        data: {
          addrs: addrs,
        },
      })
    }
  }

  // getOutlet(data: { outlet_name?: string; id?: number }) {
  //   let theData = {} as any
  //   if (data.outlet_name) {
  //     theData.outlet_name = data.outlet_name
  //   } else if (data.id !== undefined) {
  //     theData.id = data.id
  //   }
  //   return baseAxios<iOutLetDetail>({
  //     url: '/address/getOutlet',
  //     method: 'post',
  //     data: theData,
  //   })
  // }

  getOutlets(data: { level?: number; id?: number; branch_name?: string }) {
    return baseAxios<iOutLetDetail[]>({
      url: '/address/getOutlets',
      method: 'post',
      data: data,
    })
  }

  note(id: number) {
    return baseAxios({
      url: '/address/note',
      method: 'post',
      data: {
        id,
      },
    })
  }

  update(data: {
    id: number
    is_normal_work: boolean
    lat_lon: string
    location: string
    note: string
    opening_hours: string
    name: string
    weekend_status: number
  }) {
    return baseAxios({
      url: '/address/update',
      method: 'post',
      data,
    })
  }

  delete(data: { id: number }) {
    return baseAxios({
      url: '/address/delete',
      method: 'post',
      data,
    })
  }

  add(data: iOutletMsg) {
    return baseAxios({
      url: '/address/add',
      method: 'post',
      data: {
        outlets_req: [data],
      },
    })
  }
}
