import 'package:sefer_driver/constant/style.dart';
import 'package:flutter/material.dart';
import 'dart:async';

class MyCircularProgressIndicatorWithTimer extends StatelessWidget {
  final Color backgroundColor;
  final bool isLoading;

  MyCircularProgressIndicatorWithTimer({
    Key? key,
    this.backgroundColor = Colors.transparent,
    required this.isLoading,
  }) : super(key: key);

  final StreamController<int> _streamController = StreamController<int>();

  void startTimer() {
    int _timeLeft = 60;
    Timer.periodic(const Duration(seconds: 1), (timer) {
      if (_timeLeft > 0 && isLoading) {
        _streamController.add(_timeLeft);
        _timeLeft--;
      } else {
        timer.cancel();
        _streamController.close();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    if (isLoading) {
      startTimer();
    }

    return Center(
      child: Container(
        width: 200,
        height: 200,
        decoration: BoxDecoration(
          color: backgroundColor,
          shape: BoxShape.circle,
        ),
        child: Stack(
          children: [
            const Center(child: CircularProgressIndicator()),
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Align(
                  alignment: Alignment.center,
                  child: Image.asset(
                    'assets/images/logo.gif',
                    width: 140,
                    height: 140,
                  ),
                ),
                const SizedBox(height: 10),
                StreamBuilder<int>(
                  stream: _streamController.stream,
                  initialData: 60,
                  builder: (context, snapshot) {
                    return Text('${snapshot.data}', style: AppStyle.title);
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
