import 'dart:io';

import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:hum/core/constants/app_text.dart';
import 'package:hum/services/firebase_functions.dart';
import 'package:hum/views/listings/widgets/widget_listings.dart';
import 'package:image_picker/image_picker.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';

class DrawerStateSelection extends StatefulWidget {
  const DrawerStateSelection({super.key});

  @override
  State<DrawerStateSelection> createState() => DrawerStateSelectionState();
}

class DrawerStateSelectionState extends State<DrawerStateSelection> {
  bool _isUploading = false;
  bool _shouldParseWithAI = true;

  Future<String> _pickAndUploadImage(ImageSource source) async {
    String downloadURL = '';
    final ImagePicker picker = ImagePicker();
    try {
      final XFile? image = await picker.pickImage(source: source);
      if (image == null) return downloadURL;

      setState(() => _isUploading = true);

      // Create a reference to the location you want to upload to in firebase
      final storageRef = FirebaseStorage.instance.ref().child(
        'uploads/${DateTime.now().millisecondsSinceEpoch}.jpg',
      );

      // Upload the file
      final uploadTask = storageRef.putFile(File(image.path));
      final snapshot = await uploadTask.whenComplete(() => null);

      // Get the download URL
      downloadURL = await snapshot.ref.getDownloadURL();
    } catch (e) {
      print('Error uploading image: $e');
    } finally {
      if (mounted) {
        setState(() => _isUploading = false);
      }
    }
    return downloadURL;
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 40.0, bottom: 20),
              child: Stack(
                alignment: AlignmentDirectional.center,
                children: [
                  PhosphorIcon(
                    PhosphorIconsDuotone.camera,
                    // color: Colors.green,
                    size: 80.0,
                  ),
                  Opacity(
                    opacity: .3,
                    child: PhosphorIcon(
                      PhosphorIconsLight.cornersOut,
                      color: CupertinoColors.activeBlue,
                      size: 130.0,
                    ),
                  ),
                ],
              ),
            ),

            Text(listingAddTitle, style: TextStyle(fontSize: 36, fontWeight: FontWeight.bold)),

            Padding(
              padding: const EdgeInsets.only(top: 16.0, bottom: 80),
              child: Opacity(
                opacity: .5,
                child: Text(
                  listingAddMessage,
                  textAlign: TextAlign.center,
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
                ),
              ),
            ),

            Expanded(
              child: Column(
                spacing: 20,
                children: [
                  WDGListingButton(
                    icon: PhosphorIconsDuotone.camera,
                    text: listingTakePhoto,
                    description: listingTakePhotoDescription,
                    action: () async {
                      String imageURL = await _pickAndUploadImage(ImageSource.camera);
                      if (imageURL.isNotEmpty) {
                        print(imageURL);
                        print('^^^^^^^^');
                        await callHumProcessImage(imageURL);
                      }
                    },
                  ),
                  WDGListingButton(
                    icon: PhosphorIconsDuotone.image,
                    iconColor: CupertinoColors.activeGreen,
                    text: listingChoosePhoto,
                    description: listingChoosePhotoDescription,
                    action: () async {
                      String imageURL = await _pickAndUploadImage(ImageSource.gallery);
                      if (imageURL.isNotEmpty) {
                        print(imageURL);
                        print('^^^^^^^^');
                        await callHumProcessImage(imageURL);
                      }
                    },
                  ),
                  // WDGListingButton(icon: PhosphorIconsDuotone.cornersOut, action: () {}),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
