import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flyinsono/lab/components/multiple_click_button.dart';
import 'package:flyinsono/rpc.dart';
import 'package:intl/intl.dart';

class CustomClock extends StatefulWidget {
  const CustomClock({Key? key, this.textStyle}) : super(key: key);

  final TextStyle? textStyle;

  @override
  _CustomClockState createState() => _CustomClockState();
}

class _CustomClockState extends State<CustomClock> {
  String _timeString = '';
  Timer? _timer;

  @override
  void initState() {
    _timeString = _formatDateTime(DateTime.now());
    _timer = Timer.periodic(Duration(seconds: 30), (Timer t) => _getTime());
    super.initState();
  }

  @override
  void dispose() {
    _timer?.cancel();
    _timer = null;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MultipleClickButton(
      clickCount: 6,
      onTriggered: () {
        rpc.platform.openConsole();
      },
      child: Text(
        _timeString,
        style: widget.textStyle,
      ),
    );
  }

  void _getTime() {
    final String formattedDateTime = _formatDateTime(DateTime.now());
    setState(() {
      _timeString = formattedDateTime;
    });
  }

  String _formatDateTime(DateTime dateTime) {
    return DateFormat('yyyy年MM月dd日 hh:mm a').format(dateTime);
  }
}
