import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:habitrack_app/function_widgets/compound_widgets/compound_timer_widget.dart';
import 'package:habitrack_app/function_widgets/compound_widgets/compound_widget_tasks.dart';
import 'package:habitrack_app/function_widgets/compound_widgets/compound_widget_water.dart';
import 'package:habitrack_app/infrastructure/widget_wall/add_widget_button.dart';
import 'package:habitrack_app/infrastructure/widget_wall/items_controller.dart';
import 'package:habitrack_app/infrastructure/widget_wall/items_state.dart';
import 'package:habitrack_app/main.dart';
import 'package:habitrack_app/sembast/hydration.dart';
import 'package:habitrack_app/sembast/tasks_list.dart';
import 'package:habitrack_app/sembast/timer.dart';

/// Displays detailed information about a SampleItem.

class WidgetWall extends ConsumerStatefulWidget {
  const WidgetWall({super.key});

  @override
  ConsumerState<WidgetWall> createState() => _WidgetWallState();
}

class _WidgetWallState extends ConsumerState<WidgetWall> {
  Future<List<Widget>> buildList() async {
    ref.watch(homeControllerProvider);
    //var itemCount = 0;

    final items = ref.watch(itemsProvider);

    // ignore: unused_local_variable
    final val = items.value;

    return <Widget>[];
  }

  @override
  Widget build(BuildContext context) {
    // this.buildList();
    final controller = ref.watch(itemsProvider);
    switch (controller) {
      case AsyncError(:final error):
        return Text('Error: $error');
      case AsyncData(:final value):
        final allItems = value;
        final items = <dynamic>[];
        for (var i = 0; i < allItems.length; i++) {
          // ignore: avoid_dynamic_calls
          if (allItems.elementAt(i).isVisible == true) {
            logger.i('INSERTING VISIBLE ITEM');
            items.add(allItems.elementAt(i));
          }
        }
        final itemWidgets = <Widget>[];
        final itemCount = items.length;
        for (var i = 0; i < itemCount; i++) {
          final item = items.elementAt(i);
          if (item is Hydration && item.isVisible) {
            final itemwidget = CompoundWidgetWater(item: item);
            itemWidgets.insert(i, itemwidget);
          } else if (item is TimerItem && item.isVisible) {
            if (item.isVisible == true) {
              logger.i('VISIBLE');
              final itemwidget = CompoundWidgetTimer(item: item);
              itemWidgets.insert(i, itemwidget);
            } else {
              logger.i('IS NOT VISIBLE');
            }
          } else if (item is TasksItem && item.isVisible) {
            final itemwidget = CompoundWidgetTasks(item: item);
            itemWidgets.insert(i, itemwidget);
          }
        }
        return Scaffold(
          appBar: AppBar(
            iconTheme: IconThemeData(
              color: Theme.of(context).colorScheme.onPrimary,
            ),
            backgroundColor: Theme.of(context).colorScheme.secondary,
            foregroundColor: Theme.of(context).colorScheme.primary,
            title: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  textScaler: const TextScaler.linear(2),
                  'Habitrack ',
                  style: Theme.of(context).textTheme.bodyMedium!.copyWith(
                        color: Theme.of(context).colorScheme.onSecondary,
                        //       fontStyle: FontStyle.italic,
                      ),
                ),
                const Align(
                  alignment: Alignment.topRight,
                  child: AddWidgetButton(),
                ),
              ],
            ),
          ),
          body: ColoredBox(
            color: Theme.of(context).colorScheme.primaryContainer,
            child: ListView(
              padding: const EdgeInsets.all(8),
              children: itemWidgets,
            ),
          ),
        );

      default:
        return const CircularProgressIndicator();
    }
  }
}
