// 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:flutter/material.dart';
import 'package:wyatt_bloc_helper/src/utils/smart_provider.dart';

/// {@template repository_base_provider}
/// Need to implement a [create] function that is responsible for
/// creating the [Repository] and a [builder] which will return a child
/// that have access to the instance via `context.read<Repository>()`.
/// It is used as a dependency injection (DI) widget so that a single instance
/// of a [Repository] can be provided to multiple widgets within a subtree.
///
/// It automatically handles closing the instance when used with [create].
/// By default, [create] is called only when the instance is accessed.
/// To override this behavior, set [lazy] to `false`.
///
/// By default, it provide already provided instance found in the tree.
/// To override this behavior, set [smart] to `false`.
/// {@endtemplate}
abstract class RepositoryBaseProviderScreen<Repository>
    extends StatelessWidget {
  /// {@macro repository_base_provider}
  const RepositoryBaseProviderScreen({
    super.key,
    this.lazy = true,
    this.smart = true,
  });

  /// Whether the [Repository] should be created lazily.
  /// Defaults to `true` which means the [Repository] is created only when
  /// accessed the first time, not when provided.
  final bool lazy;

  /// Whether this uses [SmartProvider].
  /// Defaults to `true`. But if you want to provide new [Repository] of a
  /// same type in a sub-tree you may have to disable this.
  final bool smart;

  /// Creates the [Repository] to be used.
  Repository create(BuildContext context);

  /// Initialize the [Repository].
  ///
  /// This function is useful when using with [SmartProvider], because
  /// if you want to pass an initial call on your repository you can
  /// call it in `create` function but if you re-provide the repo below in the
  /// tree this action will never be call again.
  Repository init(BuildContext context, Repository repository) => repository;

  /// Creates the child [Widget] to be used.
  Widget builder(BuildContext context);

  @override
  Widget build(BuildContext context) => SmartProvider.repo<Repository>(
        context,
        lazy: lazy,
        enable: smart,
        create: (_) => create(context),
        init: (_, repository) => init(context, repository),
        child: Builder(builder: builder),
      );
}
