import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
import '../../../core/services/upload_progress_service.dart';
import '../../../core/utils/logger.dart';
import '../../../core/utils/app_snackbar.dart';
import '../../../core/services/image_processing_service.dart';
import '../../../core/services/invoice_upload_service.dart';
import '../../../core/network/dio_client.dart';

class ScannerController extends GetxController {
  var capturedImages = <File>[].obs;
  var isProcessing = false.obs;
  var uploadProgress = 0.0.obs;
  var companies = <Map<String, dynamic>>[].obs;
  var isLoadingCompanies = false.obs;
  var currentBatchId = ''.obs;
  var processedImagesCount = 0.obs;
  var totalImagesCount = 0.obs;
  var isBatchDone = false.obs;
  var selectedCompanyId = ''.obs;
  var selectedCompanyName = ''.obs;

  final InvoiceUploadService _uploadService = InvoiceUploadService();
  final UploadProgressService _progressService =
      Get.find<UploadProgressService>();

  @override
  void onInit() {
    super.onInit();
    fetchCompanies();
    _initFcmListener();
  }

  void _initFcmListener() {
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      final data = message.data;
      if (data['type'] == 'batch_progress' &&
          data['batch_id'] == currentBatchId.value) {
        processedImagesCount.value =
            int.tryParse(data['processed'].toString()) ?? 0;
        totalImagesCount.value = int.tryParse(data['total'].toString()) ?? 0;

        // Update global progress service
        _progressService.updateProcessingProgress(
            processedImagesCount.value, totalImagesCount.value);

        if (processedImagesCount.value >= totalImagesCount.value) {
          isBatchDone.value = true;
          _progressService.complete();
        }
      }
    });
  }

  Future<void> fetchCompanies() async {
    isLoadingCompanies.value = true;
    try {
      final res = await DioClient().client.get('companies');
      if (res.data['success'] == true && res.data['data'] != null) {
        companies.value = List<Map<String, dynamic>>.from(res.data['data']);
      }
    } catch (e) {
      AppLogger.error('Failed to fetch companies', e);
    } finally {
      isLoadingCompanies.value = false;
    }
  }

  Future<void> addImage(String imagePath) async {
    File originalFile = File(imagePath);
    capturedImages.add(originalFile);
    int index = capturedImages.length - 1;

    ImageProcessingService.processInvoiceImage(originalFile)
        .then((processedFile) {
      if (processedFile != null && index < capturedImages.length) {
        capturedImages[index] = processedFile;
        AppLogger.print('Finished processing image in background.');
      }
    }).catchError((e) {
      AppLogger.error('Failed to process image in background', e);
    });
  }

  void removeImage(int index) {
    if (index >= 0 && index < capturedImages.length) {
      capturedImages.removeAt(index);
    }
  }

  Future<void> uploadBatch() async {
    if (capturedImages.isEmpty) {
      AppSnackbar.showWarning('تنبيه', 'الرجاء تصوير فاتورة واحدة على الأقل');
      return;
    }
    if (selectedCompanyId.isEmpty) {
      AppSnackbar.showWarning('تنبيه', 'الرجاء اختيار الشركة أولاً');
      return;
    }

    try {
      isProcessing.value = true;
      uploadProgress.value = 0.0;

      AppLogger.print(
          'Uploading batch of ${capturedImages.length} images to company ${selectedCompanyId.value}...');

      // Start global progress
      _progressService.startUpload(selectedCompanyName.value, capturedImages.length);

      final batchId = await _uploadService.uploadBatch(
        companyId: selectedCompanyId.value,
        images: capturedImages,
        onProgress: (current, total) {
          uploadProgress.value = current / total;
          _progressService.updateProgress(uploadProgress.value, current);
        },
      );

      if (batchId != null) {
        currentBatchId.value = batchId;
        totalImagesCount.value = capturedImages.length;
        processedImagesCount.value = 0;

        // Clear scanner state and go back to dashboard
        capturedImages.clear();
        uploadProgress.value = 0.0;
        isProcessing.value = false;
        selectedCompanyId.value = '';
        selectedCompanyName.value = '';

        _progressService.startProcessing();
        Get.back(); // Go back to dashboard, progress will show in overlay
        AppSnackbar.showSuccess(
            'تم البدء', 'تم رفع الصور بنجاح، جاري استخراج البيانات في الخلفية');
      } else {
        _progressService.fail();
        AppSnackbar.showError('خطأ', 'فشل رفع الفواتير، يرجى المحاولة لاحقاً');
      }
    } catch (e) {
      _progressService.fail();
      AppLogger.error('Failed to upload batch', e);
      AppSnackbar.showError('خطأ', 'حدث خطأ غير متوقع أثناء الرفع');
    } finally {
      isProcessing.value = false;
    }
  }

  void selectCompany(String id, String name) {
    selectedCompanyId.value = id;
    selectedCompanyName.value = name;
  }

  Future<String> getSavePath() async {
    final directory = await getTemporaryDirectory();
    final fileName = 'invoice_${DateTime.now().millisecondsSinceEpoch}.jpg';
    return path.join(directory.path, fileName);
  }
}
