import 'package:anitrack/src/data/data_base.dart';
import 'package:anitrack/src/data/other_titles.dart';
import 'package:anitrack/src/data/source.dart';
import 'package:anitrack/src/data/type.dart';
import 'package:anitrack/src/service/database.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'anime.freezed.dart';
part 'anime.g.dart';

class BoolConverter implements JsonConverter<bool, int> {
  const BoolConverter();

  @override
  bool fromJson(int json) => json.toBool();

  @override
  int toJson(bool object) => object.toInt();
}

/// Data about a tracked anime
@freezed
abstract class AnimeTrackingData
    with _$AnimeTrackingData, TrackingMedium
    implements TrackingDataBase {
  factory AnimeTrackingData(
    /// The ID of the anime
    String id,

    /// The state of the anime
    @MediumTrackingStateConverter() MediumTrackingState state,

    /// The title of the anime
    String title,

    /// Episodes in total.
    int episodesWatched,

    /// Episodes watched.
    int? episodesTotal,

    /// URL to the thumbnail/cover art for the anime.
    String thumbnailUrl,

    /// Flag whether the anime is airing
    @BoolConverter() bool airing,

    /// The day of the week the anime is airing
    String? broadcastDay,

    /// Other titles
    @OtherTitlesConverter() OtherTitles otherTitles,

    /// The source where we got the data from.
    @TrackingDataSourceConverter() TrackingDataSource source,
  ) = _AnimeTrackingData;

  AnimeTrackingData._();

  /// JSON
  factory AnimeTrackingData.fromJson(Map<String, dynamic> json) =>
      _$AnimeTrackingDataFromJson(json);
}
