import 'dart:async';

import 'package:absensi_sas/app/constant.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

class RealTimeClock extends HookConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // Menggunakan useState untuk menyimpan waktu saat ini
    final currentTime = useState<DateTime>(DateTime.now());
    StreamSubscription<DateTime>? subscription;

    useEffect(() {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
        final clockStream = Stream<DateTime>.periodic(
            Duration(seconds: 1), (i) => DateTime.now());
        subscription = clockStream.listen((time) {
          currentTime.value = time;
        });
      });
      return () async {
        subscription?.cancel();
      };
    }, []);

    // Format waktu menjadi tanggal yang diinginkan
    String formattedTime = DateFormat('HH:mm:ss').format(currentTime.value);

    // Tampilkan widget Text dengan waktu yang diformat
    return Text(
      formattedTime,
      style: Constant.time_700(context: context)
          .copyWith(color: Constant.textBlack),
    );
  }
}
