import 'package:camerawesome/camerawesome_plugin.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../core/utils/app_snackbar.dart';
import '../controllers/scanner_controller.dart';

class ScannerView extends GetView<ScannerController> {
  const ScannerView({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      if (controller.selectedCompanyId.value.isEmpty) {
        return _buildCompanySelection(context);
      }
      return _buildScanner(context);
    });
  }

  Widget _buildCompanySelection(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('اختر الشركة أولاً',
            style: TextStyle(fontFamily: 'El Messiri')),
        centerTitle: true,
        backgroundColor: const Color(0xFF0F4C81),
        foregroundColor: Colors.white,
      ),
      body: Obx(() {
        if (controller.isLoadingCompanies.value) {
          return const Center(child: CircularProgressIndicator());
        }
        if (controller.companies.isEmpty) {
          return const Center(
            child: Text('لا توجد شركات مسجلة في حسابك.\nيرجى إضافة شركة أولاً.',
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 16)),
          );
        }
        return ListView.separated(
          padding: const EdgeInsets.all(16),
          itemCount: controller.companies.length,
          separatorBuilder: (_, __) => const SizedBox(height: 12),
          itemBuilder: (context, index) {
            final company = controller.companies[index];
            return Card(
              elevation: 2,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12)),
              child: ListTile(
                contentPadding: const EdgeInsets.symmetric(
                    horizontal: 16, vertical: 8),
                leading: Container(
                  padding: const EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: const Color(0xFF0F4C81).withValues(alpha: 0.1),
                    shape: BoxShape.circle,
                  ),
                  child: const Icon(Icons.business, color: Color(0xFF0F4C81)),
                ),
                title: Text(company['name'] ?? '',
                    style: const TextStyle(
                        fontWeight: FontWeight.bold, fontSize: 16)),
                subtitle: Text('الرقم الضريبي: ${company['tax_identification_number'] ?? 'غير محدد'}'),
                trailing: const Icon(Icons.arrow_forward_ios, size: 16),
                onTap: () {
                  controller.selectCompany(company['id'], company['name'] ?? '');
                },
              ),
            );
          },
        );
      }),
    );
  }

  Widget _buildScanner(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          // 1. Camera Layer
          CameraAwesomeBuilder.awesome(
            saveConfig: SaveConfig.photo(
              pathBuilder: (sensors) async {
                final path = await controller.getSavePath();
                if (sensors.length == 1) {
                  return SingleCaptureRequest(path, sensors.first);
                } else {
                  // For multiple sensors, we take the first one for the path
                  return MultipleCaptureRequest({
                    for (var sensor in sensors) sensor: path,
                  });
                }
              },
            ),
            onMediaTap: (media) {
              final path = media.captureRequest.when(
                single: (single) => single.file?.path,
                multiple: (multiple) =>
                    multiple.fileBySensor.values.first?.path,
              );
              if (path != null) {
                controller.addImage(path);
              }
            },
            onMediaCaptureEvent: (event) {
              if (event.status == MediaCaptureStatus.success) {
                final path = event.captureRequest.when(
                  single: (single) => single.file?.path,
                  multiple: (multiple) =>
                      multiple.fileBySensor.values.first?.path,
                );
                if (path != null) {
                  controller.addImage(path);
                }
              }
            },
            topActionsBuilder: (state) => AwesomeTopActions(
              state: state,
              children: [
                AwesomeFlashButton(state: state),
                const Spacer(),
                TextButton.icon(
                  onPressed: () => Get.back(),
                  icon: const Icon(Icons.close, color: Colors.white),
                  label: const Text('إغلاق',
                      style: TextStyle(color: Colors.white)),
                ),
              ],
            ),
            middleContentBuilder: (state) => const Center(
              child: Text(
                'قم بمحاذاة الفاتورة داخل الإطار',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 16,
                  backgroundColor: Colors.black26,
                ),
              ),
            ),
            bottomActionsBuilder: (state) => AwesomeBottomActions(
              state: state,
              left: const SizedBox(),
              right: AwesomeCameraSwitchButton(state: state),
            ),
          ),

          // 2. Batch Overlay (Bottom)
          Positioned(
            bottom: 120,
            left: 0,
            right: 0,
            child: Obx(() => controller.capturedImages.isEmpty
                ? const SizedBox()
                : Container(
                    height: 100,
                    padding: const EdgeInsets.symmetric(horizontal: 16),
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      itemCount: controller.capturedImages.length,
                      itemBuilder: (context, index) {
                        return Padding(
                          padding: const EdgeInsets.only(right: 8.0),
                          child: Stack(
                            children: [
                              ClipRRect(
                                borderRadius: BorderRadius.circular(8),
                                child: Image.file(
                                  controller.capturedImages[index],
                                  width: 80,
                                  height: 100,
                                  fit: BoxFit.cover,
                                ),
                              ),
                              Positioned(
                                top: 0,
                                right: 0,
                                child: GestureDetector(
                                  onTap: () => controller.removeImage(index),
                                  child: Container(
                                    decoration: const BoxDecoration(
                                      color: Colors.red,
                                      shape: BoxShape.circle,
                                    ),
                                    child: const Icon(Icons.close,
                                        size: 16, color: Colors.white),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        );
                      },
                    ),
                  )),
          ),

          // 3. Upload Button
          Positioned(
            top: 40,
            left: 20,
            right: 20,
            child: Obx(() => controller.capturedImages.isEmpty
                ? const SizedBox()
                : ElevatedButton.icon(
                    onPressed: controller.isProcessing.value
                        ? null
                        : () => controller.uploadBatch(),
                    style: ElevatedButton.styleFrom(
                      backgroundColor: const Color(0xFF0F4C81),
                      padding: const EdgeInsets.symmetric(vertical: 16),
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(30)),
                    ),
                    icon: controller.isProcessing.value
                        ? SizedBox(
                            width: 20,
                            height: 20,
                            child: CircularProgressIndicator(
                                value: controller.uploadProgress.value > 0
                                    ? controller.uploadProgress.value
                                    : null,
                                color: Colors.white,
                                strokeWidth: 2))
                        : const Icon(Icons.cloud_upload, color: Colors.white),
                    label: Text(
                      'رفع ${controller.capturedImages.length} فواتير لـ ${controller.selectedCompanyName.value}',
                      style: const TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                          color: Colors.white),
                    ),
                  )),
          ),
        ],
      ),
    );
  }


}
