import 'dart:io';
import 'package:flutter/material.dart';

import 'package:mobdr/main.dart';
import 'package:mobdr/events.dart';
import 'package:mobdr/db/box_visit_photo.dart';
import 'package:mobdr/network/api_provider.dart';
import 'package:mobdr/service/shared_prefs.dart';

class UploadPhotosPage extends StatefulWidget {
  final int pp_id_visite;

  UploadPhotosPage({required this.pp_id_visite});

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

class _UploadPhotosPageState extends State<UploadPhotosPage> {
  bool _isSyncing = false;
  bool _syncSuccessful = false;
  int _totalUploaded = 0;
  int _totalPhotos = 0;

  late List<VisitPhoto> _visitPhotosList;

  ApiProvider _apiProvider = ApiProvider();

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

    loadData();
  }

  Future<void> _uploadVisitPhotos() async {
    setState(() {
      _isSyncing = true;
      _syncSuccessful = false;
    });

    // parse all photos
    for (var photo in _visitPhotosList) {
      int id_photo_mp4 = -1;
      bool isUpdatePhotoTypologie = true;
      bool isUpdatePhotoVisibility = true;
      bool isUpdatePhotoCompetitor = true;
      bool isUpdatePhotoTags = true;

      // if photo not already uploaded
      if (photo.id_photo_mp4 <= 0) {
        // try upload the photo
        id_photo_mp4 = await _apiProvider.uploadPhotoServlet(
            photo.id_visite, photo.getImage());
      } else
        id_photo_mp4 = photo.id_photo_mp4;

      // the photo is saved in MP4
      if (id_photo_mp4 > 0) {
        // update photo typology
        isUpdatePhotoTypologie = await _apiProvider.updatePhotoTypology(
            photo.id_visite, id_photo_mp4, photo.id_photo_typologie);

        // update photo visibility
        if (photo.photo_principale == 1 || photo.photo_privee == 1) {
          isUpdatePhotoVisibility = await _apiProvider.updatePhotoVisibility(
              photo.id_visite,
              id_photo_mp4,
              photo.photo_principale,
              photo.photo_privee);
        }

        // update photo tags
        if (photo.tags.isNotEmpty) {
          isUpdatePhotoTags = await _apiProvider.updatePhotoTags(
              photo.id_visite, id_photo_mp4, photo.tags);
        }

        // update photo competitor
        if (photo.id_concurrence_lien > 0) {
          isUpdatePhotoCompetitor = await _apiProvider.updatePhotoConpetitor(
              photo.id_visite, id_photo_mp4, photo.id_concurrence_lien);
        }
      }

      if (id_photo_mp4 > 0 &&
          isUpdatePhotoTypologie == true &&
          isUpdatePhotoVisibility == true &&
          isUpdatePhotoTags == true &&
          isUpdatePhotoCompetitor == true) {
        // delete photo in database
        objectbox.delPhotoById(photo.id);

        // delete the file
        File file = File(photo.getImage());
        if (await file.exists()) {
          await file.delete();
        }

        _totalUploaded++;
      } else {
        // if successful upload
        if (id_photo_mp4 != -1) {
          photo.id_photo_mp4 = id_photo_mp4;

          // save MP4 id in database
          objectbox.putPhotoIdMP4(photo.id, id_photo_mp4);
        }
      }

      //await Future.delayed(Duration(seconds: 5));
    }

    // Get unique id_visite values from _photosList
    Set<int> uniqueIds =
        _visitPhotosList.map((photo) => photo.id_visite).toSet();

    // Send VisitPhotoCountEvent for each unique id_visite
    for (int id_visite in uniqueIds) {
      eventBus.fire(VisitPhotoCountEvent(
          id_visite, objectbox.getVisitPhotoCount(id_visite)));
    }

    setState(() {
      _isSyncing = false;
      _syncSuccessful = true;
    });
  }

  Future<void> _clearVisitPhotoCache() async {
    List<VisitPhoto> _visitPhotoListTokeep;
    Directory photosDir = Directory(SharedPrefs().photosDir);

    _visitPhotoListTokeep = objectbox.getAllVisitPhotos();

    // Get a list of all files in the "photos" directory
    final List<FileSystemEntity> files = await photosDir.list().toList();

    // Check each file in the directory
    for (FileSystemEntity file in files) {
      if (file is File) {
        // Extract the file name from the file path
        String fileName = file.path.split('/').last;

        // Check if the file exists in the _visitPhotoListTokeep
        bool existsInList = _visitPhotoListTokeep
            .any((visitPhoto) => visitPhoto.image_name == fileName);

        if (!existsInList) {
          // Delete the file if it doesn't exist in the _visitPhotoListTokeep
          await file.delete();
          print('Deleted file: $fileName');
        }
      }
    }
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('Upload Photos'),
        centerTitle: true,
      ),
      body: Padding(
        padding: EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text(
              'Upload Photos',
              style: TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
              ),
              textAlign: TextAlign.center,
            ),
            SizedBox(height: 16),
            Expanded(
              child: Stack(
                children: [
                  Center(
                    child: Visibility(
                      visible: _isSyncing,
                      child: Container(
                        height: 150,
                        width: 150,
                        child: CircularProgressIndicator(
                          valueColor: AlwaysStoppedAnimation<Color>(
                            Theme.of(context).primaryColor,
                          ),
                          strokeWidth: 10,
                        ),
                      ),
                    ),
                  ),
                  Center(
                    child: Visibility(
                      visible: !_isSyncing && _totalUploaded == _totalPhotos,
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Icon(
                            Icons.check_circle,
                            size: 48,
                            color: Theme.of(context).primaryColor,
                          ),
                          SizedBox(height: 16),
                          Text(
                            'Upload complete',
                            textAlign: TextAlign.center,
                          ),
                        ],
                      ),
                    ),
                  ),
                  Center(
                    child: Visibility(
                      visible: !_isSyncing && _totalUploaded != _totalPhotos,
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Icon(
                            Icons.cloud_upload,
                            size: 48,
                            color: Colors.grey,
                          ),
                          SizedBox(height: 16),
                          Text(
                            'Tap the button below to upload your photos',
                            textAlign: TextAlign.center,
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
            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'),
                  ),
                ],
              ),
            SizedBox(height: 16),
            Visibility(
              visible: !_isSyncing,
              child: ElevatedButton(
                  child:
                      Text('Upload Photos ($_totalUploaded / $_totalPhotos)'),
                  onPressed: _syncSuccessful
                      ? null
                      : () async {
                          // upload photo to server
                          await _uploadVisitPhotos();

                          // deletes photos that are no longer in any visits
                          await _clearVisitPhotoCache();

                          // go back
                          Navigator.pop(context);
                        }

                  //,
                  ),
            ),
          ],
        ),
      ),
    );
  }

  /// Initializes data when the page loads.
  void loadData() {
    // "visit" mode
    if (widget.pp_id_visite > 0) {
      _visitPhotosList =
          objectbox.getAllVisitPhotosByVisit(widget.pp_id_visite);
      _totalPhotos = _visitPhotosList.length;
      // "all" mode
    } else {
      _visitPhotosList = objectbox.getAllVisitPhotos();
      _totalPhotos = _visitPhotosList.length;
    }
  }
}
