import Vue from 'vue'
import store from '@/util/store'

class SearchLog {
  public addSearchLog(data: any) {
    Vue.prototype.$SearchLog.push(data)
  }
}

class shipUtil {

  /**
   * 通过船舶类型代码获取船舶类型
   * @param data 船舶类型代码
   */
  public getShipType(data: any, en?: boolean) {
    const shipTypes = store.getShipType()
    if (shipTypes) {
      const shipType = shipTypes.find((val: any) => {
        return val.id === data
      })
      if (shipType) {
        if (en) {
          return shipType.valueEn
        } else {
          return shipType.valueChs
        }
      } else {
        if (en) {
          return 'Other'
        } else {
          return '其他类型'
        }
      }
    }
  }

  public newGetShipType(shipTypeId: any, en?: boolean) {
    if (shipTypeId === '30') {
      return en ? 'Fishing vessel' : '渔船'
    } else if (shipTypeId === '52') {
      return en ? 'TUG' : '拖轮'
    } else if (['31', '32', '33', '34', '53'].indexOf(shipTypeId) !== -1) {
      return en ? 'Working ship' : '工程船'
    } else if (shipTypeId === '37') {
      return en ? 'Passenger' : '客船'
    } else if (shipTypeId[0] === '6') {
      return en ? 'Passenger' : '客船'
    } else if (['35', '50', '51', '55', '58', '59'].indexOf(shipTypeId) !== -1) {
      return en ? 'Official law enforcement ship' : '公务执法船'
    } else if (shipTypeId[0] === '7') {
      return en ? 'Cargo ship' : '货船'
    } else if (shipTypeId[0] === '8') {
      return en ? 'Tanker' : '油船'
    } else {
      return en ? 'Other' : '其他'
    }
  }

  /**
   * 通过mmsi的前3位获取船籍国
   * @param data mmsi的前3位
   */
  public getShipCountryImg(data: any) {
    const countries = store.getCountry()
    if (countries) {
      const country = countries.find((val: any) => {
        return val.id === data
      })
      return country ? require(`../../../public/country-img/${country.valueAbbr}.png`) : require(`../../../public/country-img/undefined.png`)
    }
  }

  /**
   * 通过mmsi的前3位获取国家名称
   * @param data mmsi的前3位
   */
  public getShipCountryImgName(data: any) {
    const countries = store.getCountry()
    if (countries) {
      const country = countries.find((val: any) => {
        return val.id === data
      })
      return country && country.valueEn ? country.valueEn : ''
    }
  }

  /**
   * 通过mmsi的前3位获取船籍字符
   * @param data mmsi的前3位
   */
  public getShipCountryCode(data: any) {
    const countries = store.getCountry()
    if (countries) {
      const country = countries.find((val: any) => {
        return val.id === data
      })
      return country ? `${country.valueEn ? country.valueEn + '/' : ''}${country.valueChs ? country.valueChs : ''}` : 'undefined'
    }
  }

  /**
   * 通过mmsi的前3位获取船籍对象
   * @param data mmsi的前3位
   */
  public getShipCountry(data: any) {
    const countries = store.getCountry()
    if (countries) {
      const country = countries.find((val: any) => {
        return val.id === data
      })
      return country || 'undefined'
    }
  }

  /**
   * 通过状态码获取船舶状态
   * @param data
   */
  public getShipStatus(data: any, en?: boolean) {
    const status = store.getShipStatus()
    if (status) {
      const sta = status.find((val: any) => {
        return val.id === data + ''
      })
      if (sta) {
        if (en) {
          return sta.valueEn
        } else {
          return sta.valueChs
        }
      } else {
        if (en) {
          return 'undefined'
        } else {
          return '未知'
        }
      }
      // return sta ? sta.valueChs : '未知'
    }
  }

  /**
   * 纬度格式化
   * @param value
   */
  public filterLat(value: any) {
    if (value === undefined || value === '' || value === null) {
      return
    }
    let dData = 0
    if (Math.abs(value) < 90) {
      dData = Math.abs(value)
    } else {
      dData = Math.abs(value / 10000 / 60)
    }
    let isPlus = value > 0 ? 'N' : 'S'
    let degree = parseInt(dData + '')
    let min = parseInt((dData - degree) * 60 + '')
    let sec = (((dData - degree) * 60 - min) * 60).toFixed(2)
    return `${degree}°${min}′${sec}″${isPlus}`
  }

  /**
   * 经度格式化
   * @param value
   */
  public filterLon(value: any) {
    if (value === undefined || value === '' || value === null) {
      return
    }
    let dDate = 0
    if (Math.abs(value) < 180) {
      dDate = Math.abs(value)
    } else {
      dDate = Math.abs(value / 10000 / 60)
    }
    let isPlus = value > 0 ? 'E' : 'W'
    let degree = parseInt(dDate + '')
    let min = parseInt((dDate - degree) * 60 + '')
    let sec = (((dDate - degree) * 60 - min) * 60).toFixed(2)
    return `${degree}°${min}′${sec}″${isPlus}`
  }

  /**
   * 时间格式化
   * @param data
   */
  public formatDate(time: any, fmt: any) {
    if (time === undefined || time === '' || time === null || time === 0) {
      return
    }
    if (typeof time === 'string') {
      return time
    }
    time = time * 1000
    const date = new Date(time)
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    const o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    }
    for (const k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        // @ts-ignore
        const str = o[k] + ''
        fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str))
      }
    }
    return fmt
  }

  private padLeftZero(str: any) {
    return ('00' + str).substr(str.length)
  }
}

/**
 * 最小化列表
 */
class MiniList {

  /**
   * 添加最小化内容
   * @param data
   */
  public addMini(data: any) {
    Vue.prototype.$MiniList.push(data)
  }

  /**
   * 删除最小化内容
   * @param index
   */
  public delMini(type: string) {
    const list = Vue.prototype.$MiniList
    if (list) {
      list.forEach((item: any, index: any) => {
        if (item.type === type) {
          Vue.prototype.$MiniList.splice(index, 1)
        }
      })
    }
  }

  /**
   * 判断传入的类型是否最小化显示
   * @param type
   */
  public isHasMini(type: string) {
    let data: any = null
    const list = Vue.prototype.$MiniList
    list.forEach((item: any) => {
      if (item.type === type) {
        data = item.data
      }
    })
    return data
  }
}

const SearchLogUtil = new SearchLog()
const ShipUtils = new shipUtil()
const MiniUtils = new MiniList()
export {
  SearchLogUtil,
  ShipUtils,
  MiniUtils
}
