import { createElement, lazy } from 'react'

const HostInfo = lazy(
  () => import('../../components/Dashboard/Management/Hosts/HostInfo')
)
const HostOTP = lazy(
  () => import('../../components/Dashboard/Management/Hosts/HostOtp')
)
import HostIcon from '../../components/Icons/HostIcon'
import InfoCircleIcon from '../../components/Icons/InfoCircleIcon'
import EditIcon from '../../components/Icons/EditIcon'
import CheckIcon from '../../components/Icons/CheckIcon'
import XMarkIcon from '../../components/Icons/XMarkIcon'
import BinIcon from '../../components/Icons/BinIcon'
import OTPIcon from '../../components/Icons/OTPIcon'

export const Host = {
  name: 'host',
  label: 'Host',
  labelPlural: 'Hosts',
  url: '/dashboard/management/hosts',
  prefix: 'HST',
  icon: HostIcon,
  actions: [
    {
      name: 'info',
      type: 'page',
      pageName: 'info',
      label: 'Info',
      default: true,
      row: true,
      icon: InfoCircleIcon
    },
    {
      name: 'edit',
      type: 'page',
      pageName: 'info',
      label: 'Edit',
      row: true,
      icon: EditIcon,
      visible: (objectData) => {
        return !(objectData?._isEditing && objectData?._isEditing == true)
      }
    },
    {
      name: 'cancelEdit',
      label: 'Cancel Edits',
      type: 'page',
      pageName: 'info',
      icon: XMarkIcon,
      visible: (objectData) => {
        return objectData?._isEditing && objectData?._isEditing == true
      }
    },
    {
      name: 'finishEdit',
      label: 'Save Edits',
      type: 'page',
      pageName: 'info',
      icon: CheckIcon,
      visible: (objectData) => {
        return objectData?._isEditing && objectData?._isEditing == true
      }
    },
    {
      type: 'divider'
    },
    {
      name: 'connect',
      type: 'modal',
      modalWidth: 520,
      label: 'Connect',
      icon: OTPIcon,
      content: (objectData, { onOk } = {}) => {
        return createElement(HostOTP, { id: objectData._id, onOk })
      }
    },
    {
      name: 'delete',
      type: 'page',
      pageName: 'info',
      label: 'Delete',
      icon: BinIcon,
      danger: true
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(HostInfo)
    }
  ],
  columns: [
    '_reference',
    'name',
    'state',
    'tags',
    'connectedAt',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    'name',
    '_id',
    'state',
    'tags',
    'connectedAt',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: ['name', 'state', 'connectedAt', 'createdAt', 'updatedAt'],
  group: ['tags'],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      objectType: 'host',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'host',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'name',
      label: 'Name',
      required: true,
      type: 'text',
      columnWidth: 200,
      columnFixed: 'left'
    },
    {
      name: 'connectedAt',
      label: 'Connected At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'state',
      label: 'State',
      type: 'state',
      objectType: 'host',
      showName: false,
      readOnly: true,
      columnWidth: 260
    },
    {
      name: 'online',
      label: 'Online',
      type: 'bool',
      readOnly: true,
      columnWidth: 150
    },
    {
      name: 'active',
      label: 'Active',
      type: 'bool',
      required: true,
      columnWidth: 125
    },

    {
      name: 'deviceInfo.os',
      label: 'Operating System',
      type: 'text',
      required: false,
      readOnly: true,
      columnWidth: 220,
      value: (objectData) => {
        if (
          objectData.deviceInfo?.os?.type &&
          objectData.deviceInfo?.os?.release &&
          objectData.deviceInfo?.os?.arch
        ) {
          return `${objectData.deviceInfo.os.type} ${objectData.deviceInfo.os.release} (${objectData.deviceInfo.os.arch})`
        }
      }
    },
    {
      name: 'deviceInfo.os.hostname',
      label: 'Hostname',
      type: 'text',
      required: false,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'deviceInfo.cpu.model',
      label: 'CPU Model',
      type: 'text',
      required: false,
      readOnly: true,
      columnWidth: 200
    },
    {
      name: 'deviceInfo.cpu',
      label: 'CPU Info',
      type: 'text',
      required: false,
      readOnly: true,
      columnWidth: 150,
      value: (objectData) => {
        if (
          objectData.deviceInfo?.cpu?.cores &&
          objectData.deviceInfo?.cpu?.speedMHz
        ) {
          return `Cores: ${objectData.deviceInfo.cpu.cores}, Speed: ${objectData.deviceInfo.cpu.speedMHz} MHz`
        }
      }
    },
    {
      name: 'deviceInfo.user.username',
      label: 'User',
      type: 'text',
      required: false,
      readOnly: true,
      columnWidth: 120
    },
    {
      name: 'deviceInfo.user.homedir',
      label: 'User Home',
      type: 'text',
      required: false,
      readOnly: true,
      columnWidth: 200
    },
    {
      name: 'deviceInfo.process.nodeVersion',
      label: 'NodeJS Version',
      type: 'text',
      required: false,
      readOnly: true,
      columnWidth: 150
    },
    {
      name: 'tags',
      label: 'Tags',
      type: 'tags',
      required: false,
      columnWidth: 200
    },
    {
      name: 'files',
      label: 'Files',
      type: 'objectList',
      objectType: 'file',
      required: false,
      readOnly: true,
      columnWidth: 200
    }
  ]
}
