// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// 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/>.

import 'package:architecture_example/data/models/photo_model.dart';
import 'package:architecture_example/domain/data_sources/remote/photo_remote_data_source.dart';
import 'package:architecture_example/domain/entities/photo.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';

class PhotoMockDataSourceImpl extends PhotoRemoteDataSource {
  final Map<int, Photo> _mock = {
    1: const PhotoModel(
      albumId: 1,
      id: 1,
      title: 'Photo 1',
      url: 'https://via.placeholder.com/600/92c952',
      thumbnailUrl: 'https://via.placeholder.com/150/92c952',
    ),
    2: const PhotoModel(
      albumId: 1,
      id: 2,
      title: 'Photo 2',
      url: 'https://via.placeholder.com/600/771796',
      thumbnailUrl: 'https://via.placeholder.com/150/771796',
    ),
  };

  @override
  Future<Photo> getPhoto(int id) async {
    final response = _mock[id];
    if (response.isNull) {
      throw Exception('Unknown photo');
    }
    return response!;
  }

  @override
  Future<List<Photo>> getAllPhotos({int? start, int? limit}) async {
    final response = _mock.values;
    final nullableEnd = (limit != null) ? start ?? 0 + limit : null;
    return response.toList().sublist(start ?? 0, nullableEnd);
  }

  @override
  Future<List<Photo>> getPhotosFromAlbum(
    int albumId, {
    int? start,
    int? limit,
  }) async {
    // TODO(hpcl): use start and limit
    final response = _mock.values
      ..where((element) => element.albumId == albumId);
    return response.toList();
  }
}
