import 'package:flutter/material.dart';
import 'package:syncrow_web/utils/color_manager.dart';

class DuplicateProcessDialog extends StatefulWidget {
  final VoidCallback onDuplicate;

  const DuplicateProcessDialog({required this.onDuplicate, Key? key})
      : super(key: key);

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

class _DuplicateProcessDialogState extends State<DuplicateProcessDialog> {
  bool isDuplicating = true;

  @override
  void initState() {
    super.initState();
    _startDuplication();
  }

  void _startDuplication() async {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      widget.onDuplicate();
    });
    await Future.delayed(const Duration(seconds: 2));
    setState(() {
      isDuplicating = false;
    });

    await Future.delayed(const Duration(seconds: 2));
    if (mounted) {
      Navigator.of(context).pop();
    }
  }

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;

    return AlertDialog(
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(20),
      ),
      content: SizedBox(
        width: screenWidth * 0.4,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (isDuplicating) ...[
              const CircularProgressIndicator(
                color: ColorsManager.vividBlue,
              ),
              const SizedBox(height: 15),
              Text(
                "Duplicating in progress",
                style: Theme.of(context)
                    .textTheme
                    .headlineSmall
                    ?.copyWith(color: ColorsManager.primaryColor),
                textAlign: TextAlign.center,
              ),
            ] else ...[
              const Icon(
                Icons.check_circle,
                color: ColorsManager.vividBlue,
                size: 50,
              ),
              const SizedBox(height: 15),
              Text(
                "Duplicating successful",
                textAlign: TextAlign.center,
                style: Theme.of(context)
                    .textTheme
                    .headlineSmall
                    ?.copyWith(color: ColorsManager.primaryColor),
              ),
            ],
          ],
        ),
      ),
    );
  }
}
