export type DeviceLocation = {
  alt: number
  date: number
  lat: number
  lng: number
}

export type ServiceUseOption =
  | 'Disabled'
  | 'Enabled'
  | 'Unspecified'

export type ComponentUseOption =
  | 'Allowed'
  | 'Disallowed'

export type PhoneUsePolicyOption = ServiceUseOption | ComponentUseOption

export type LegacyPhoneUsePolicyOption =
  | 'Allowed'
  | 'Disallowed'
  | 'Changeable'

export type GroupUsePolicy = {
  bluetooth?: ServiceUseOption | LegacyPhoneUsePolicyOption
  bluetoothEditable?: boolean
  camera?: ComponentUseOption | LegacyPhoneUsePolicyOption
  cameraEditable?: boolean
  GPS?: ServiceUseOption | LegacyPhoneUsePolicyOption
  gpsEditable?: boolean
  sim?: ServiceUseOption | LegacyPhoneUsePolicyOption
  simEditable?: boolean
  offlineTimeThreshold?: number
  date?: number
}

export type DevicePolicy = {
  bluetooth?: ServiceUseOption | LegacyPhoneUsePolicyOption
  camera?: ComponentUseOption | LegacyPhoneUsePolicyOption
  GPS?: ServiceUseOption | LegacyPhoneUsePolicyOption
  sim?: ServiceUseOption | LegacyPhoneUsePolicyOption
  canUseBluetooth?: boolean
  canUseCamera?: boolean
  canUseGPS?: boolean
  canUseSim?: boolean
  locked?: boolean
}

export type DeviceTechState = {
  batteryCycles: number | null
  batteryLevel: number | null
  batteryRemainingCapacity: number | null
  hits: number | null
  needMaintenance: boolean
  overheats: number | null
  temperature?: number | null
  worktime: number | null
}

export type DeviceNetworkStatus = 'Online' | 'Offline' | 'Lost'

export type DeviceOrganisation = {
  creationDate?: number
  id: number
  logoUrl?: string | null
  name: string
  policy?: GroupUsePolicy | null
}

export type Device = {
  id: number
  imei: string
  imei2: string
  serial: string
  networkStatus?: DeviceNetworkStatus
  orgId: number
  registerDate: number
  org: DeviceOrganisation | null
  policy: DevicePolicy | null
  techState: DeviceTechState | null
  lastLocation: DeviceLocation | null
}

export type GetPhonesData = {
  getPhones: {
    page: Device[]
    totalElements: number
    totalPages: number
  }
}

export type GetPhonesVariables = {
  page: number
  query?: string
  locked?: boolean
  needMaintenance?: boolean
  networkStatus?: DeviceNetworkStatus[]
  orgs?: string[]
  sortDirection: 'ASC' | 'DESC'
  sortField: 'ID' | 'Serial' | 'Date'
}

type GetPhonesStatsPage = {
  totalElements: number
}

export type GetPhonesTabsStatsData = {
  totalPhones: GetPhonesStatsPage
  onlinePhones: GetPhonesStatsPage
  lockedPhones: GetPhonesStatsPage
  lostPhones: GetPhonesStatsPage
  maintenancePhones: GetPhonesStatsPage
}

export type GetPhonesTabsStatsVariables = {
  query?: string
  orgs?: string[]
}

export type GetPhoneData = {
  getPhone: Device | null
}

export type GetPhoneVariables = {
  id: number
}

export type DeviceTelemetryItem = {
  batteryCapacity: number | null
  batteryCycles: number | null
  batteryLevel: number | null
  date: number
  temperature: number | null
}

export type DeviceTelemetryEventItem = {
  date: number;
  type: 'event';
  eventType: DevicePhoneStateEventType;
  label: string;
};

export type DeviceHistoryChartItem =
  | DeviceTelemetryItem
  | DeviceTelemetryEventItem;

export type DevicePhoneStateEventType =
  | 'ScreenOn'
  | 'ScreenOff'
  | 'EnterNetwork'
  | 'LeaveNetwork'
  | 'PowerOn'
  | 'PowerOff'
  | 'Hit'
  | 'Overheat'
  | 'PowerAC'
  | 'PowerUSB'
  | 'PowerWireless'
  | 'PowerDisconnected'

export type DevicePhoneStateEvent = {
  date: number
  phoneId: string
  type: DevicePhoneStateEventType
}

export type DevicePackage = {
  category: string | null
  iconUrl: string | null
  installDate: number | null
  name: string | null
  packageName: string
  version: number | null
  versionName: string | null
}

export type GetPhonePackagesData = {
  getPhonePackages: DevicePackage[]
}

export type GetPhonePackagesVariables = {
  phoneId: string
}

export type DevicePackageUseEventType =
  | 'ResumeActivity'
  | 'PauseActivity'
  | 'StopActivity'
  | string

export type DevicePackageUseEvent = {
  date: number
  packageName: string
  type: DevicePackageUseEventType
}

export type DevicePackageUseEventsGroup = {
  events: DevicePackageUseEvent[]
  phonePackage: DevicePackage | null
}

export type GetTelemetryData = {
  getTelemetry: DeviceTelemetryItem[]
  getPhoneStateEvents: DevicePhoneStateEvent[]
  getPhonePackagesUseEvents: DevicePackageUseEventsGroup[]
}

export type GetTelemetryVariables = {
  phoneId: number
  packagesPhoneId: string
  startDate: number
  endDate?: number
}

export type DeviceGpsTrackPoint = {
  alt: number
  date: number
  lat: number
  lng: number
}

export type GetPhoneGpsTrackData = {
  getPhoneGpsTrack: DeviceGpsTrackPoint[]
}

export type GetPhoneGpsTrackVariables = {
  phoneId: string
  startDate: number
  endDate?: number
}

export type GetDevicePageData = {
  getPhone: Device | null
  getTelemetry: DeviceTelemetryItem[]
  getPhoneGpsTrack: DeviceGpsTrackPoint[]
}

export type GetDevicePageVariables = {
  id: number
  phoneId: string
  telemetryStartDate: number
  telemetryEndDate?: number
  gpsStartDate: number
  gpsEndDate?: number
}

export type ChangePhonePolicyData = {
  changePhonePolicy: DevicePolicy
}

export type ChangePhonePolicyVariables = {
  id: string
  bluetooth: ServiceUseOption
  camera: ComponentUseOption
  GPS: ServiceUseOption
  sim: ServiceUseOption
  locked: boolean
}

export type CreatePhoneRegistrationTokenData = {
  createPhoneRegistrationToken: {
    address: string
    expiresIn: number
    token: string
  }
}

export type CreatePhoneRegistrationTokenVariables = Record<string, never>
