import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';

import '../utils/time_stream.dart';

class ClockWidget extends ConsumerWidget {
  const ClockWidget({this.textStyle, super.key});

  final TextStyle? textStyle;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return StreamBuilder<DateTime>(
      stream: timeStream(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          DateTime time = snapshot.data ?? DateTime.now();
          DateFormat format = DateFormat('HH:mm:ss');
          return Text(
            format.format(time),
            style: textStyle,
          );
        } else {
          return const CircularProgressIndicator();
        }
      },
    );
  }
}
