import { gql } from '@apollo/client'

export const GET_PHONES_QUERY = gql`
  query GetPhones(
    $page: Int!
    $query: String
    $locked: Boolean
    $needMaintenance: Boolean
    $networkStatus: [PhoneNetworkStatus!]
    $orgs: [ID!]
    $sortDirection: SortDirection!
    $sortField: PhoneSortField!
  ) {
    getPhones(
      page: $page
      query: $query
      locked: $locked
      needMaintenance: $needMaintenance
      networkStatus: $networkStatus
      orgs: $orgs
      sortDirection: $sortDirection
      sortField: $sortField
    ) {
      page {
        id
        imei
        imei2
        serial
        networkStatus

        lastLocation {
          alt
          date
          lat
          lng
        }

        org {
          id
          logoUrl
          name

          policy {
            bluetooth
            bluetoothEditable
            camera
            cameraEditable
            GPS
            gpsEditable
            sim
            simEditable
            offlineTimeThreshold
          }
        }

        orgId

        policy {
          bluetooth
          camera
          GPS
          sim
          locked
        }

        techState {
          batteryLevel
          needMaintenance
          temperature
        }
      }

      totalElements
      totalPages
    }
  }
`

export const GET_PHONES_TABS_STATS_QUERY = gql`
  query GetPhonesTabsStats($query: String, $orgs: [ID!]) {
    totalPhones: getPhones(
      page: 0
      query: $query
      orgs: $orgs
      sortDirection: DESC
      sortField: ID
    ) {
      totalElements
    }

    onlinePhones: getPhones(
      page: 0
      query: $query
      networkStatus: [Online]
      orgs: $orgs
      sortDirection: DESC
      sortField: ID
    ) {
      totalElements
    }

    lockedPhones: getPhones(
      page: 0
      query: $query
      locked: true
      orgs: $orgs
      sortDirection: DESC
      sortField: ID
    ) {
      totalElements
    }

    lostPhones: getPhones(
      page: 0
      query: $query
      networkStatus: [Lost]
      orgs: $orgs
      sortDirection: DESC
      sortField: ID
    ) {
      totalElements
    }

    maintenancePhones: getPhones(
      page: 0
      query: $query
      needMaintenance: true
      orgs: $orgs
      sortDirection: DESC
      sortField: ID
    ) {
      totalElements
    }
  }
`

export const GET_PHONE_QUERY = gql`
  query GetPhone($id: Int!) {
    getPhone(id: $id) {
      id
      imei
      imei2
      networkStatus

      lastLocation {
        alt
        date
        lat
        lng
      }

      org {
        creationDate
        id
        name
        policy {
          bluetooth
          bluetoothEditable
          camera
          cameraEditable
          GPS
          gpsEditable
          sim
          simEditable
          offlineTimeThreshold
        }
      }

      orgId

      policy {
        bluetooth
        camera
        GPS
        sim
        locked
      }

      registerDate
      serial

      techState {
        batteryCycles
        batteryLevel
        batteryRemainingCapacity
        hits
        needMaintenance
        overheats
        worktime
      }
    }
  }
`

export const GET_PHONE_GPS_TRACK_QUERY = gql`
  query GetPhoneGpsTrack($phoneId: ID!, $startDate: Float!, $endDate: Float) {
    getPhoneGpsTrack(phoneId: $phoneId, startDate: $startDate, endDate: $endDate) {
      alt
      date
      lat
      lng
    }
  }
`

export const GET_PHONE_PACKAGES_QUERY = gql`
  query GetPhonePackages($phoneId: ID!) {
    getPhonePackages(phoneId: $phoneId) {
      category
      iconUrl
      installDate
      name
      packageName
      version
      versionName
    }
  }
`

export const GET_TELEMETRY_QUERY = gql`
  query GetTelemetry(
    $phoneId: Int!
    $packagesPhoneId: ID!
    $startDate: Float!
    $endDate: Float
  ) {
    getTelemetry(phoneId: $phoneId, startDate: $startDate, endDate: $endDate) {
      batteryCapacity
      batteryCycles
      batteryLevel
      date
      temperature
    }

    getPhoneStateEvents(phoneId: $packagesPhoneId, startDate: $startDate, endDate: $endDate) {
      date
      phoneId
      type
    }

    getPhonePackagesUseEvents(
      phoneId: $packagesPhoneId
      startDate: $startDate
      endDate: $endDate
    ) {
      events {
        type
        packageName
        date
      }

      phonePackage {
        category
        iconUrl
        installDate
        name
        packageName
        version
        versionName
      }
    }
  }
`

export const GET_DEVICE_PAGE_QUERY = gql`
  query GetDevicePage(
    $id: Int!
    $phoneId: ID!
    $telemetryStartDate: Float!
    $telemetryEndDate: Float
    $gpsStartDate: Float!
    $gpsEndDate: Float
  ) {
    getPhone(id: $id) {
  id
  imei
  imei2
  networkStatus

  lastLocation {
    alt
    date
    lat
    lng
  }

  org {
    creationDate
    id
    name
    policy {
      bluetooth
      bluetoothEditable
      camera
      cameraEditable
      GPS
      gpsEditable
      sim
      simEditable
      offlineTimeThreshold
    }
  }

  orgId

  policy {
    bluetooth
    camera
    GPS
    sim
    locked
  }

  registerDate
  serial

  techState {
      batteryCycles
      batteryLevel
      batteryRemainingCapacity
      hits
      needMaintenance
      overheats
      temperature
      worktime
  }
}

    getTelemetry(
      phoneId: $id
      startDate: $telemetryStartDate
      endDate: $telemetryEndDate
    ) {
      batteryCapacity
      batteryCycles
      batteryLevel
      date
      temperature
    }

    getPhoneGpsTrack(
      phoneId: $phoneId
      startDate: $gpsStartDate
      endDate: $gpsEndDate
    ) {
      alt
      date
      lat
      lng
    }
  }
`

export const CREATE_PHONE_REGISTRATION_TOKEN_MUTATION = gql`
  mutation CreatePhoneRegistrationToken {
    createPhoneRegistrationToken {
      address
      expiresIn
      token
    }
  }
`

export const CHANGE_PHONE_POLICY_MUTATION = gql`
  mutation ChangePhonePolicy(
    $id: ID!
    $bluetooth: ServiceUseOption!
    $camera: ComponentUseOption!
    $GPS: ServiceUseOption!
    $sim: ServiceUseOption!
    $locked: Boolean!
  ) {
    changePhonePolicy(
      id: $id
      policy: {
        bluetooth: $bluetooth
        camera: $camera
        GPS: $GPS
        sim: $sim
        locked: $locked
      }
    ) {
      bluetooth
      camera
      GPS
      sim
      locked
    }
  }
`
