import 'package:json_annotation/json_annotation.dart';
part 'classes.g.dart';

/*
        Voyage Handbook - The open-source WikiVoyage reader
        Copyright (C) 2023 Matyáš Caras

        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License version 3 as published by
        the Free Software Foundation.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

@JsonSerializable()
class SearchResponse {
  final int id;
  final String key;
  final String title;
  final String excerpt;
  final String? description;
  @JsonKey(includeFromJson: false, includeToJson: false)
  bool downloaded;
  SearchResponse(this.id, this.key, this.title, this.excerpt, this.description,
      {this.downloaded = false});

  /// Connect the generated function to the `fromJson`
  /// factory.
  factory SearchResponse.fromJson(Map<String, dynamic> json) =>
      _$SearchResponseFromJson(json);

  /// Connect the generated function to the `toJson` method.
  Map<String, dynamic> toJson() => _$SearchResponseToJson(this);
}

@JsonSerializable()
class LicenseAttribution {
  final String url;
  final String title;
  const LicenseAttribution(this.url, this.title);
  factory LicenseAttribution.fromJson(Map<String, dynamic> json) =>
      _$LicenseAttributionFromJson(json);

  /// Connect the generated function to the `toJson` method.
  Map<String, dynamic> toJson() => _$LicenseAttributionToJson(this);
}

@JsonSerializable()
class RawPage {
  final int id;
  final String key;
  final String title;
  @JsonKey(fromJson: _editedFromJson, toJson: _editedToJson, name: "latest")
  final String edited;
  String html;
  final LicenseAttribution license;

  RawPage(this.id, this.key, this.title, this.edited, this.html, this.license);

  factory RawPage.fromJson(Map<String, dynamic> json) =>
      _$RawPageFromJson(json);

  /// Connect the generated function to the `toJson` method.
  Map<String, dynamic> toJson() => _$RawPageToJson(this);

  static String _editedFromJson(Map<String, dynamic> json) => json["timestamp"];
  static Map<String, dynamic> _editedToJson(String json) => {"timestamp": json};
}
