import {
  API_GET_ALARMS,
  API_GET_GPS,
  API_PATH_ENTITIES,
  API_PATH_SHIP_INFO,
  API_PATH_SHIP_TRACK_POINTS,
  API_SOS,
} from '@/constants';
import { request } from '@umijs/max';

export function transformEntityResponse(
  raw: API.EntityResponse,
): API.TransformedEntity {
  return {
    id: raw.id,
    value: raw.v,
    valueString: raw.vs,
    time: raw.t,
    type: raw.type,
  };
}

export async function queryEntities(): Promise<API.TransformedEntity[]> {
  const rawList = await request<API.EntityResponse[]>(API_PATH_ENTITIES);
  return rawList.map(transformEntityResponse);
}

export async function getShipInfo(): Promise<API.ShipDetail> {
  return await request<API.ShipDetail>(API_PATH_SHIP_INFO);
}

export async function queryAlarms(): Promise<API.AlarmResponse> {
  return await request<API.AlarmResponse>(API_GET_ALARMS);
}

export async function getGPS() {
  return await request<API.GPSResonse>(API_GET_GPS);
}
export async function getSos() {
  return await request<API.SosResponse>(API_SOS);
}
export async function deleteSos() {
  return await request<API.SosResponse>(API_SOS, {
    method: 'DELETE',
  });
}

export async function sendSosMessage(message: string) {
  return await request<API.SosRequest>(API_SOS, {
    method: 'PUT',
    params: {
      message,
    },
  });
}

export async function queryShipTrackPoints() {
  return await request<API.ShipTrackPoint[]>(API_PATH_SHIP_TRACK_POINTS);
}