// 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:flutter_bloc/flutter_bloc.dart' as bloc_base;

import 'package:wyatt_crud_bloc/wyatt_crud_bloc.dart';
import 'package:wyatt_ui_components/wyatt_wyatt_ui_components.dart';

mixin CrudMixin<Cubit extends bloc_base.Cubit,
    SuccessState extends CrudSuccess> {
  Widget errorBuilder(BuildContext context, CrudError state) =>
      context.components.errorWidget?.copyWith(error: state.message) ??
      const SizedBox.shrink();

  Widget loadingBuilder(BuildContext context, CrudLoading state) =>
      context.components.loadingWidget ?? const SizedBox.shrink();

  Widget initialBuilder(BuildContext context, CrudInitial state) =>
      const SizedBox.shrink();

  Widget successBuilder(BuildContext context, SuccessState state);

  Widget crudBuilder(BuildContext context, CrudState state) =>
      CrudBuilder<CrudInitial, CrudLoading, SuccessState, CrudError>(
        errorBuilder: errorBuilder,
        loadingBuilder: loadingBuilder,
        initialBuilder: initialBuilder,
        state: state,
        builder: successBuilder,
      );
}
