// 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/repositories/photo_repository.dart';
import 'package:architecture_example/domain/usecases/photos/add_photo_to_favorites.dart';
import 'package:architecture_example/domain/usecases/photos/check_if_photo_is_in_favorites.dart';
import 'package:architecture_example/domain/usecases/photos/display_photo.dart';
import 'package:architecture_example/domain/usecases/photos/remove_photo_from_favorites.dart';
import 'package:architecture_example/presentation/features/photo_details/blocs/photo_details/photo_details_cubit.dart';
import 'package:architecture_example/presentation/features/photo_details/state_management/photo_details_wrapper_widget.dart';
import 'package:flutter/material.dart';
import 'package:wyatt_bloc_helper/wyatt_bloc_helper.dart';

class PhotoDetailsScreen
    extends CubitScreen<PhotoDetailsCubit, PhotoDetailsState> {
  const PhotoDetailsScreen({required this.photoId, super.key});

  final int photoId;

  @override
  PhotoDetailsCubit create(BuildContext context) => PhotoDetailsCubit(
        DisplayPhoto(repo<PhotoRepository>(context)),
        CheckIfPhotoIsInFavorites(repo<PhotoRepository>(context)),
        AddPhotoToFavorites(repo<PhotoRepository>(context)),
        RemovePhotoFromFavorites(repo<PhotoRepository>(context)),
      );

  @override
  PhotoDetailsCubit init(BuildContext context, PhotoDetailsCubit bloc) =>
      bloc..load(photoId);

  @override
  Widget onWrap(BuildContext context, Widget child) =>
      PhotoDetailsWrapperWidget(child: child);

  @override
  Widget onBuild(BuildContext context, PhotoDetailsState state) {
    if (state is PhotoDetailsFailure) {
      return const Center(
        child: Text('failed to fetch photo details'),
      );
    }
    if (state is PhotoDetailsSuccess) {
      return CustomScrollView(
        slivers: [
          SliverAppBar(
            expandedHeight: 400,
            stretch: true,
            flexibleSpace: FlexibleSpaceBar(
              background: Stack(
                children: [
                  Positioned.fill(
                    child: Image.network(
                      state.photo.thumbnailUrl,
                      fit: BoxFit.cover,
                    ),
                  ),
                  Positioned.fill(
                    child: Image.network(
                      state.photo.url,
                      fit: BoxFit.cover,
                    ),
                  ),
                ],
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildListDelegate([
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Flexible(
                      child: Text(
                        state.photo.title,
                        overflow: TextOverflow.ellipsis,
                        style: const TextStyle(fontWeight: FontWeight.bold),
                      ),
                    ),
                    IconButton(
                      onPressed: () {
                        state.isFavorite
                            ? bloc(context).removeFromFavorites()
                            : bloc(context).addToFavorites();
                      },
                      icon: Icon(
                        state.isFavorite
                            ? Icons.favorite
                            : Icons.favorite_outline,
                      ),
                    ),
                  ],
                ),
              ),
            ]),
          ),
        ],
      );
    }
    return const Center(
      child: CircularProgressIndicator(),
    );
  }
}
