import Location from "../entities/location";
import LocationsRepository from "../entities/locationsRepository";
import Presenter from "./presenter";

// On pourrait l'injecter dans le use case
// export interface CreateLocationDataValidator {
//   validate(cmd: CreateLocationData): void;
// }

export type CreateLocationData = {
  name: string;
  lat: number;
  lon: number;
};

// interface UseCase<TCommand> {
//   execute(cmd: TCommand): void;
// }

// class CreateLocationUseCase implements UseCase<CreateLocationData> {
//   public constructor(private readonly locationRepository: LocationRepository) {}

//   execute(cmd: CreateLocationData): void {
//     // FIXME: Valider les entrées utilisateurs
//     if (typeof cmd !== "object") {
//       throw new Error("should be an object");
//     }

//     const location = new Location(cmd.name, cmd.lat, cmd.lon);

//     this.locationRepository.save(location);
//   }
// }

// const db = new Database("monfichier.db")
// const sqliteRepository = new SqliteLocationRepository(db);
// const uc = new CreateLocationUseCase(sqliteRepository)

// uc.execute({

// })

// createLocation(sqliteRepository, {

// });

export default function createLocation(
  locationRepository: LocationsRepository,
  presenter: Presenter<Location>,
  cmd: CreateLocationData
) {
  // FIXME: Valider les entrées utilisateurs
  if (typeof cmd !== "object") {
    throw new Error("should be an object");
  }

  const location = new Location(cmd.name, cmd.lat, cmd.lon);

  locationRepository.save(location);
  presenter.show(location);
}

// Autre possibilité, exit les presenters et on retourne le résultat directement.
// export function createLocation_(
//   locationRepository: LocationsRepository,
//   cmd: CreateLocationData
// ): string {
//   // TODO implémenter le cas d'utilisation d'écriture
//   return "id de la localisation crée";
// }

// interface

/**
 * Dans mon application web
 * post("/locations", () => {
 *  const id = createLocation(req.body)
 * return getLocation(id)
 * })
 */

type LocationDto = {
  id: string;
  name: string;
  lat: number;
  lon: number;
  temperaturesHistory: number[];
};

interface DataGateway {
  getLocationById(id: string): LocationDto;
}

export function getLocation(
  gateway: DataGateway,
  locationId: string
): LocationDto {
  return gateway.getLocationById(locationId);
}

// export function createLocation_(
//   locationRepository: LocationsRepository,
//   cmd: CreateLocationData
// ): Location {
//   const location = new Location(cmd.name, cmd.lat, cmd.lon);

//   locationRepository.save(location);

//   return location;
// }
