import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/utils/date-utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:flutter/material.dart';

class ShowTimer extends StatefulWidget {
  final PatiantInformtion patientInfo;

  const ShowTimer({
    Key? key,
    required this.patientInfo,
  }) : super(key: key);

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

class _ShowTimerState extends State<ShowTimer> {
  int remainingTime = 600;
  String displayTime = '';

  @override
  Widget build(BuildContext context) {
    if (displayTime.isEmpty) {
      startShowTimer();
    }
    return AppText(
      "$displayTime",
      color: Colors.red,
      textAlign: TextAlign.start,
      fontWeight: FontWeight.bold,
      fontSize: 11,
    );
  }

  startShowTimer() {
    // this.remainingTime--;
    setState(() {
      displayTime = this.generateShowTimerString();
    });

    Future.delayed(Duration(seconds: 1), () {
      if (this.remainingTime > 0) {
          startShowTimer();
      }
    });
  }

  generateShowTimerString() {
    DateTime now = DateTime.now();
    DateTime liveCareDate = DateTime.parse(widget.patientInfo.arrivalTime!);
    String timer = AppDateUtils.differenceBetweenDateAndCurrent(liveCareDate, context, isShowSecond: true, isShowDays: false);

    return timer;
  }
}
