// 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/domain/entities/photo.dart';
import 'package:architecture_example/domain/repositories/photo_repository.dart';
import 'package:architecture_example/domain/usecases/photos/check_if_photo_is_in_favorites.dart';
import 'package:architecture_example/presentation/features/photo_details/photo_details.dart';
import 'package:architecture_example/presentation/features/photos/blocs/favorite_checker/favorite_checker_cubit.dart';
import 'package:flutter/material.dart';
import 'package:wyatt_bloc_helper/wyatt_bloc_helper.dart';

class PhotosGridThumbnail
    extends CubitScreen<FavoriteCheckerCubit, FavoriteCheckerState> {
  const PhotosGridThumbnail({required this.photo, super.key});

  final Photo photo;

  @override
  FavoriteCheckerCubit create(BuildContext context) => FavoriteCheckerCubit(
        CheckIfPhotoIsInFavorites(repo<PhotoRepository>(context)),
      );

  @override
  FavoriteCheckerCubit init(BuildContext context, FavoriteCheckerCubit bloc) =>
      bloc..checkIfPhotoIsInFavorites(photo.id);

  @override
  Widget onBuild(BuildContext context, FavoriteCheckerState state) =>
      GestureDetector(
        onTap: () => Navigator.of(context).push(
          MaterialPageRoute<void>(
            builder: (context) => PhotoDetails(photoId: photo.id),
          ),
        ),
        child: Stack(
          children: [
            Positioned.fill(
              child: Image.network(
                photo.thumbnailUrl,
                fit: BoxFit.cover,
              ),
            ),
            if (state is FavoriteCheckerSuccess && state.isFavorite) ...[
              const Positioned(
                  bottom: 10, right: 10, child: Icon(Icons.favorite),)
            ]
          ],
        ),
      );
}
