// 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/core/enums/fetch_status.dart';
import 'package:architecture_example/domain/repositories/photo_repository.dart';
import 'package:architecture_example/domain/usecases/photos/open_album.dart';
import 'package:architecture_example/presentation/features/photos/blocs/photo/photo_bloc.dart';
import 'package:architecture_example/presentation/features/photos/state_management/photos_wrapper_widget.dart';
import 'package:architecture_example/presentation/features/photos/state_management/widgets/photos_grid.dart';
import 'package:flutter/material.dart';
import 'package:wyatt_bloc_helper/wyatt_bloc_helper.dart';

class PhotosScreen extends BlocScreen<PhotoBloc, PhotoEvent, PhotoState> {
  const PhotosScreen({required this.albumId, super.key});

  final int albumId;

  @override
  PhotoBloc create(BuildContext context) =>
      PhotoBloc(OpenAlbum(repo<PhotoRepository>(context)));

  @override
  PhotoBloc init(BuildContext context, PhotoBloc bloc) =>
      bloc..add(PhotoFetched(albumId));

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

  @override
  Widget onBuild(BuildContext context, PhotoState state) {
    switch (state.status) {
      case FetchStatus.initial:
        return const Center(child: CircularProgressIndicator());
      case FetchStatus.failure:
        return const Center(
          child: Text('failed to fetch photos'),
        );
      case FetchStatus.success:
        return PhotosGrid(
          photos: state.photos,
          albumId: albumId,
          hasReachedMax: state.hasReachedMax,
        );
    }
  }
}
