import { isEmpty } from "ramda"
import {globalServices} from './StaticData'
import moment from "moment/dist/moment"
import ru from "moment/dist/locale/ru"
moment.updateLocale("ru", ru)

class Services {
  constructor() {
  }

  textSlice (text, length) {
    if (!text) return ''
    if (text.length > length) {
      return text.slice(0, length) + '...'
    }
    return text
  }

  getServices() {

    /*

      Получаем все сервисы
      Вызывается при загрузке страницы, записывается в сокет/стор
      defp global_services_list do
        Helpers.try_get(:"global@172.25.78.129", GlobalNode.ServicesSupervisor, :list_workers, [])
        |> Enum.map(&Map.merge(%{service: &1}, Helpers.get_cache(&1))) // После запроса к GlobalNode.ServicesSupervisor получаем список сервисов

        |> Enum.map(fn
          %{init_params: init_params} = map when is_map(init_params) ->
            Map.merge(map, %{
              is_online: // Установка соединения с сервисом, если возвращается :pong, то соединение успешно установлено, соответственно получаем true, если возвращается :pang - false 
                Node.ping(String.to_atom("#{init_params.service}@#{init_params.machine_addr}")) ==
                  :pong
            })

          map ->
            map
        end)
        |> Enum.to_list()
      rescue
        _e -> []
      end

      */

    const services = globalServices ? globalServices : []
    return services
  }

  serviceDetales(services) {
    
    if (isEmpty(services)) return []
    return services.map((service) => {
      const serviceName = service.service || service.name
      const title = this.textSlice(serviceName, 25)
      const serviceNameArr = serviceName?.split('_')
      let serviceIp = ''
      let serviceCode = ''
      if (serviceNameArr?.length > 0) {
        if (serviceNameArr[serviceNameArr.length - 1].length > 15)
          serviceCode = serviceNameArr[serviceNameArr.length - 1]
        serviceIp = serviceNameArr.reduce((acc, el, idx) => {
          const ipIdx = el.indexOf('@')
          if (ipIdx !== -1) {
            acc = el.slice(ipIdx)
          }
          else {
            acc
          }
          return acc
        }, '')
      }
      const dateStart = service.init_params && service.init_params.datetime_started ? moment(service.init_params.datetime_started).format('dddd, MMMM DD, YYYY') : ''
      const ip = service.init_params && service.init_params.machine_addr ? service.init_params.machine_addr : ''
      const divider = ip ? 5 : 15
      const serviceLink = `${this.textSlice(title, 10)}${serviceCode ? '_' + this.textSlice(serviceCode, divider) : ''}${ip ? ':' + ip : ''}`
      return {...service, title, serviceIp, serviceCode, dateStart, serviceLink}
    })
    
  }

}

class Service {
  constructor(service) {
    this.service = service
  }

  openService() {
    return console.log('openService')
  }

  getCashe(service) {
    return console.log('getCashe', service)
  }

  saveCashe(service) {
    return console.log('saveCashe', service)
  }

}

export {Services, Service}