import 'package:flutter/material.dart';
import 'package:mobdr/network/api_provider.dart';

class SyncCalendarPage2 extends StatefulWidget {
  @override
  _SyncCalendarPage2State createState() => _SyncCalendarPage2State();
}

class _SyncCalendarPage2State extends State<SyncCalendarPage2> {
  bool _isSyncing = false;
  bool _syncSuccessful = false;
  String? _syncErrorMessage;

  Future<void> _syncData() async {
    setState(() {
      _isSyncing = true;
    });

    final ApiProvider _apiProvider = ApiProvider();

    final syncResult = await _apiProvider.SyncCalendar();

    if (syncResult == 'OK') {
      setState(() {
        _isSyncing = false;
        _syncSuccessful = true;
      });

      // Wait for 2 seconds before navigating back to the previous screen.
      await Future.delayed(Duration(seconds: 2));

      _popScreen();
    } else {
      setState(() {
        _isSyncing = false;
        _syncSuccessful = false;
        _syncErrorMessage = syncResult;
      });
    }
  }

  void _popScreen() {
    if (mounted) {
      Navigator.of(context).pop(_syncSuccessful);
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      // Disable the back button while the synchronization is in progress.
      onWillPop: () async => !_isSyncing,
      child: Scaffold(
        appBar: AppBar(
          title: Text('Synchronisation'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              if (!_isSyncing && !_syncSuccessful && _syncErrorMessage == null)
                Text(
                  'Appuyez sur le bouton pour lancer la synchronisation.',
                  textAlign: TextAlign.center,
                  style: TextStyle(fontSize: 18.0),
                ),
              if (_isSyncing)
                Text(
                  'Synchronisation en cours...',
                  textAlign: TextAlign.center,
                  style: TextStyle(fontSize: 18.0),
                ),
              if (_syncSuccessful)
                Column(
                  children: [
                    Icon(Icons.check_circle, size: 100.0, color: Colors.green),
                    SizedBox(height: 16.0),
                    ElevatedButton(
                      onPressed: _popScreen,
                      child: Text('Synchronisation réussie'),
                    ),
                  ],
                ),
              if (_syncErrorMessage != null)
                Column(
                  children: [
                    Icon(Icons.error, size: 100.0, color: Colors.red),
                    SizedBox(height: 16.0),
                    Text(
                      _syncErrorMessage!,
                      textAlign: TextAlign.center,
                      style: TextStyle(fontSize: 18.0),
                    ),
                    SizedBox(height: 16.0),
                    ElevatedButton(
                      onPressed: _isSyncing ? null : _syncData,
                      child: _isSyncing
                          ? CircularProgressIndicator()
                          : Text('Relancer la synchronisation'),
                    ),
                  ],
                ),
              if (!_isSyncing && !_syncSuccessful && _syncErrorMessage == null)
                ElevatedButton(
                  onPressed: _isSyncing ? null : _syncData,
                  child: _isSyncing
                      ? CircularProgressIndicator()
                      : Text('Lancer la synchronisation'),
                ),
            ],
          ),
        ),
      ),
    );
  }
}
