// 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:bloc_layout_example/bloc/example_cubit.dart';
import 'package:bloc_layout_example/components/theme_components.dart';
import 'package:flutter/material.dart';
import 'package:wyatt_bloc_layout/wyatt_bloc_layout.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ComponentTheme(
      componentThemeWidget: AppThemeComponent.components,
      child: MaterialApp(
        title: 'Bloc Layout Example',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
          body: Column(
            children: [
              Expanded(
                child: Row(
                  children: [
                    Expanded(
                      child: BlocProvider(
                        create: (_) => ExampleCubit()..run(),
                        child: ExampleCrudStateManagement(),
                      ),
                    ),
                    Expanded(
                      child: BlocProvider(
                        create: (_) => ExampleCubit()..runList(),
                        child: ExampleListCrudStateManagement(),
                      ),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: Row(
                  children: [
                    Expanded(
                      child: BlocProvider(
                          create: (_) => ExampleCubit()..run(),
                          child: ExampleFrameLayoutCrudConsumer()),
                    ),
                    Expanded(
                      child: BlocProvider(
                          create: (_) => ExampleCubit()..runList(),
                          child: ExampleFrameLayoutCrudListConsumer()),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class ExampleCrudStateManagement
    extends CrudCubitConsumerScreen<ExampleCubit, String> {
  @override
  Widget initialBuilder(BuildContext context, CrudInitial state) =>
      Container(color: Colors.yellow);
  @override
  Widget successBuilder(BuildContext context, CrudLoaded<String> state) =>
      Center(child: Text(state.data ?? 'errors'));
}

class ExampleListCrudStateManagement
    extends CrudListCubitConsumerScreen<ExampleCubit, String> {
  @override
  Widget initialBuilder(BuildContext context, CrudInitial state) =>
      Container(color: Colors.green);
  @override
  Widget successBuilder(BuildContext context, CrudListLoaded<String> state) =>
      Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: state.data.map((e) => Text(e ?? 'error')).toList(),
      );
}

class ExampleFrameLayoutCrudConsumer
    extends FrameLayoutCrudCubitConsumerScreen<ExampleCubit, String> {
  const ExampleFrameLayoutCrudConsumer()
      : super(
          title: 'Example Title',
          currentIndex: 0,
        );

  @override
  Widget successBuilder(BuildContext context, CrudLoaded<String> state) =>
      Center(child: Text(state.data ?? 'errors'));
}

class ExampleFrameLayoutCrudListConsumer
    extends FrameLayoutCrudListCubitConsumerScreen<ExampleCubit, String> {
  const ExampleFrameLayoutCrudListConsumer()
      : super(
          title: 'Example Title',
          currentIndex: 0,
        );

  @override
  Widget successBuilder(BuildContext context, CrudListLoaded<String> state) =>
      Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: state.data.map((e) => Text(e ?? 'error')).toList(),
      );
}
