import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.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/sembast/hydration.dart';
import 'package:habitrack_app/sembast/tasks_list.dart';
import 'package:habitrack_app/sembast/timer.dart';

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

  @override
  ConsumerState<ResetSubpageButton> createState() => _ResetSubpageButtonState();
}

class _ResetSubpageButtonState extends ConsumerState<ResetSubpageButton> {
  Future<void> _confirmPopup() async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          backgroundColor: Theme.of(context).colorScheme.onPrimary,
          title: Text(
            'Are you sure?',
            style: Theme.of(context).textTheme.bodyMedium!.copyWith(
                  color: Theme.of(context).colorScheme.onPrimaryContainer,
                  fontWeight: FontWeight.bold,
                  fontSize: 25,
                ),
          ),
          scrollable: true,
          actions: <Widget>[
            OutlinedButton(
              style: OutlinedButton.styleFrom(
                backgroundColor: Theme.of(context).colorScheme.primary,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
              ),
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text(
                'Cancel',
                style: Theme.of(context).textTheme.bodyMedium!.copyWith(
                      color: Theme.of(context).colorScheme.onPrimary,
                    ),
              ),
            ),
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: Theme.of(context).colorScheme.primary,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
              ),
              onPressed: () {
                final controller = ref.watch(homeControllerProvider);
                final items = ref.watch(itemsProvider);
                switch (items) {
                  case AsyncData(:final value):
                    final items = value;
                    for (var i = 0; i < items.length; i++) {
                      final item = items.elementAt(i);
                      if (item is Hydration) {
                        controller.delete(item.id);
                      } else if (item is TasksItem) {
                        controller.delete(item.id);
                      } else if (item is TimerItem) {
                        controller.delete(item.id);
                      }
                    }
                    Navigator.of(context).pop();
                }
                //DateTime due = DateTime.parse(formattedDate);
                // ignore: cascade_invocations
              },
              child: Text(
                'Yes',
                style: Theme.of(context).textTheme.bodyMedium!.copyWith(
                      color: Theme.of(context).colorScheme.onPrimary,
                    ),
              ),
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 300,
      margin: const EdgeInsets.only(top: 7.5, bottom: 7.5),
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          backgroundColor: Theme.of(context).colorScheme.primary,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10),
          ),
          minimumSize: const Size(10, 70),
        ),
        onPressed: _confirmPopup,
        child: Row(
          children: [
            Icon(
              Icons.delete,
              color: Theme.of(context).colorScheme.onPrimary,
            ),
            Text(
              'Clear Database',
              style: Theme.of(context).textTheme.bodyMedium!.copyWith(
                    color: Theme.of(context).colorScheme.onPrimary,
                  ),
            ),
          ],
        ),
      ),
    );
  }
}
