import Validation from "@helpers/validation"
import Formating from "@helpers/formating"
import {stringToArr} from "@helpers/functions"
import {Adapter} from '1-toolkits-helpers'
import {lastPacksConfig, freePacksConfig} from "@adapters/AdapterOfPacks"

const {convertList} = Adapter

const validation = new Validation()
const formating = new Formating()

class ServiceOfPacks {
  constructor(api, store) {
    this.api = api
    this.store = store
  }

  /**
   * 
   * @param {String} imei
   * @param {String} dtStart
   * @param {String} dtFinish
   * @returns {Promise<Array>}
   */
  async fetchLastPacks(imei, dtStart, dtFinish) {
    let result = await this.api.fetchLastPacks(imei, dtStart, dtFinish)
    result = convertList(result, {
      config: lastPacksConfig
    })
    return result
  }

  /**
   * 
   * @param {Array} packs
   * @param {String} imei
   * @param {String} dtStart
   * @param {String} dtFinish
   * @param {Object} pagination
   * @param {Object} settings
   * @returns {Promise<Array>}
   */
  async fetchFreePacks({packs, imei, dt_start, dt_finish, pagination, settings}) {
    const result = await this.api.fetchFreePacks({packs, imei, dt_start, dt_finish, pagination, settings})
    const logs = convertList(result.logs, {
      config: freePacksConfig
    })
    return {maxDataLength: result.max_data_length, logs}
  }

  /**
   * @param {Array} packs
   * @returns {Promise<Array>}
   */
  async setLastPacks(packs) {
    const result = await this.store.dispatch('packs/setLastPacks', packs)
    return result
  }

  /**
   * @param {Array} packs
   * @returns {Promise<Array>}
   */
  async setFreePacks(packs) {
    const result = await this.store.dispatch('packs/setFreePacks', packs)
    return result
  }

  /**
   * @param {String} dateFormat
   * @returns {Promise<Object>}
   */
  getDefaultDates(dateFormat) {
    return {
      dtStart: formating.dateStart(dateFormat),
      dtFinish: formating.dateFinish(dateFormat)
    }
  }

  /**
   * @param {Object} paramsFields
   * @param {Object} params
   * @returns {Promise<Object>}
   */
  setPackFields (paramsFields, params) {
    paramsFields[params.key] = params.value
    if (params.key === 'dtStart' || params.key === 'dtFinish') {
      paramsFields = {...paramsFields, [params.key]: formating.getDate(params.key, params.value)}
    }
    if (params.key === 'imei') {
      return {paramsFields, errorKey: 'errorImei', errorValue: validation.imei(params.value)}
    }
    if (params.key === 'packNumber') {
      return {paramsFields, errorKey: 'errorPackNumber', errorValue: validation.packNumber(params.value)}
    }
    return {paramsFields}
  }

  /**
   * 
   * @param {String} imei
   * @param {String} packNumber
   * @param {String} dtStart
   * @param {String} dtFinish
   * @param {String} countEntries
   * @param {String} sortBy
   * @param {String} sortOrder
   * @param {Number} page
   * @param {String} format
   * @returns {Promise<Object>}
   */
  async getFreePacks ({imei, packNumber, dtStart, dtFinish, countEntries, sortBy, sortOrder, page, format}) {
    imei = Number(imei)
    const packs = stringToArr(packNumber)
    const dt_start = formating.dateStart(format, dtStart)
    const dt_finish = formating.dateFinish(format, dtFinish)
    const pagination = {count: Number(countEntries), page: page}
    const settings = {date_param: sortBy, sort_order: sortOrder}
    await this.fetchAndSetFreePacks({imei, packs, dt_start, dt_finish, pagination, settings})
  }

  /**
   * 
   * @param {String} imei
   * @param {String} dtStart
   * @param {String} dtFinish
   * @param {String} format
   * @returns {Promise<Object>}
   */
  async getLastPacks ({imei, dtStart, dtFinish, format}) {
    dtStart = formating.dateStart(format, dtStart)
    dtFinish = formating.dateFinish(format, dtFinish)
    await this.fetchAndSetLastPacks(imei, dtStart, dtFinish)
  }

  /**
   * 
   * @param {String} imei
   * @param {String} dtStart
   * @param {String} dtFinish
   * @returns {Promise<Array>}
   */
  async fetchAndSetLastPacks(imei, dtStart, dtFinish) {

    const packs = await this.fetchLastPacks(imei, dtStart, dtFinish)
    await this.setLastPacks(packs)
    return packs
  }

  /**
   * 
   * @param {Array} packs
   * @param {String} imei
   * @param {String} dtStart
   * @param {String} dtFinish
   * @param {Object} pagination
   * @param {Object} settings
   * @returns {Promise<Object>}
   */
  async fetchAndSetFreePacks({packs, imei, dt_start, dt_finish, pagination, settings}) {
    const freePacks = await this.fetchFreePacks({packs, imei, dt_start, dt_finish, pagination, settings})
    await this.setFreePacks(freePacks)
    return freePacks
  }

}

export default ServiceOfPacks