import 'dart:io'; // Used for working with files
import 'package:cheminova/provider/collect_kyc_provider.dart';
import 'package:cheminova/screens/retailer_details_screen.dart';
import 'package:cheminova/screens/upload_documents_screen.dart';
import 'package:cheminova/screens/verification_documents_screen.dart';
import 'package:image_picker/image_picker.dart'; // For capturing and picking images
import 'package:cheminova/widgets/common_drawer.dart';
import 'package:flutter/material.dart';
import 'package:cheminova/widgets/common_background.dart';
import 'package:provider/provider.dart'; // For state management with Provider
import '../widgets/common_app_bar.dart';

class CollectKycScreen extends StatefulWidget {
  final String id; // ID passed as a required parameter
  const CollectKycScreen({super.key, required this.id});

  @override
  State<CollectKycScreen> createState() => CollectKycScreenState();
}

class CollectKycScreenState extends State<CollectKycScreen>
    with SingleTickerProviderStateMixin {
  late CollectKycProvider collectKycProvider; // Provider to handle business logic and state

  final _pages = [
    const RetailerDetailsScreen(), // First tab for Retailer Details
    const UploadDocumentScreen(), // Second tab for uploading documents
    const VerifyDocumentsScreen() // Third tab for verifying documents
  ];

  @override
  void initState() {
    collectKycProvider = CollectKycProvider(); // Initialize the provider
    collectKycProvider.setId(widget.id); // Set the retailer ID in the provider
    collectKycProvider.tabController = TabController(length: 3, vsync: this); // Initialize TabController for 3 tabs
    super.initState();
  }

  // Controllers for form fields
  final locationController = TextEditingController();
  final notesController = TextEditingController();
  final dealerController = TextEditingController();
  final productController = TextEditingController();
  final qualityController = TextEditingController();
  final marketNameController = TextEditingController();
  final districtController = TextEditingController();
  final stateController = TextEditingController();
  final pincodeController = TextEditingController();
  final aadharcardController = TextEditingController();
  final pancardController = TextEditingController();

  // File variables for image picking
  File? _selfieImage;
  File? _pesticideLicenseImage;
  File? _fertilizerLicenseImage;

  final ImagePicker _picker = ImagePicker(); // Image picker instance

  // Function to pick image for selfie or documents
  Future<void> _pickImage(ImageSource source, bool isSelfie) async {
    final pickedFile = await _picker.pickImage(source: source); // Pick image from camera or gallery

    setState(() {
      if (pickedFile != null) {
        if (isSelfie) {
          _selfieImage = File(pickedFile.path); // Set the picked file to the selfie image variable
        }
      }
    });
  }

  // Function to pick images for pesticide or fertilizer licenses
  Future<void> _pickLicenseImage(ImageSource source, bool isPesticide) async {
    final pickedFile = await _picker.pickImage(source: source);

    setState(() {
      if (pickedFile != null) {
        if (isPesticide) {
          _pesticideLicenseImage = File(pickedFile.path); // Set the pesticide license image
        } else {
          _fertilizerLicenseImage = File(pickedFile.path); // Set the fertilizer license image
        }
      }
    });
  }

  // Function to show image picker for selfie or document
  void _showPicker(BuildContext context, bool isSelfie) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext bc) {
          return SafeArea(
            child: Wrap(
              children: <Widget>[
                ListTile(
                    leading: const Icon(Icons.photo_library),
                    title: const Text('Photo Library'),
                    onTap: () {
                      _pickImage(ImageSource.gallery, isSelfie); // Pick image from gallery
                      Navigator.of(context).pop();
                    }),
                ListTile(
                  leading: const Icon(Icons.photo_camera),
                  title: const Text('Camera'),
                  onTap: () {
                    _pickImage(ImageSource.camera, isSelfie); // Pick image from camera
                    Navigator.of(context).pop();
                  },
                ),
              ],
            ),
          );
        });
  }

  // Function to show picker for licenses
  void _showLicensePicker(BuildContext context, bool isPesticide) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext bc) {
          return SafeArea(
            child: Wrap(
              children: <Widget>[
                ListTile(
                    leading: const Icon(Icons.photo_library),
                    title: const Text('Photo Library'),
                    onTap: () {
                      _pickLicenseImage(ImageSource.gallery, isPesticide); // Pick image for license from gallery
                      Navigator.of(context).pop();
                    }),
                ListTile(
                  leading: const Icon(Icons.photo_camera),
                  title: const Text('Camera'),
                  onTap: () {
                    _pickLicenseImage(ImageSource.camera, isPesticide); // Pick image for license from camera
                    Navigator.of(context).pop();
                  },
                ),
              ],
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => collectKycProvider, // Provide the state management
      child: Consumer<CollectKycProvider>(
        builder: (BuildContext context, value, Widget? child) =>
            Stack(children: [
              CommonBackground(
                  child: DefaultTabController(
                      length: 3, // Number of tabs
                      child: Scaffold(
                          backgroundColor: Colors.transparent,
                          appBar: PreferredSize(
                              preferredSize: const Size.fromHeight(100), // Custom AppBar with Tabs
                              child: CommonAppBar(
                                  actions: [
                                    IconButton(
                                      onPressed: () {
                                        Navigator.pop(context); // Back button to navigate back
                                      },
                                      icon: Image.asset('assets/Back_attendance.png'),
                                      padding: const EdgeInsets.only(right: 20),
                                    ),
                                  ],
                                  title: const Text('Collect KYC Data', // Title for the AppBar
                                      style: TextStyle(
                                          fontSize: 20,
                                          color: Colors.black,
                                          fontWeight: FontWeight.w400,
                                          fontFamily: 'Anek')),
                                  backgroundColor: Colors.transparent,
                                  elevation: 0,
                                  bottom: TabBar( // TabBar for navigation between the pages
                                      controller: value.tabController, // Use the TabController from provider
                                      padding: const EdgeInsets.symmetric(horizontal: 10),
                                      dividerColor: Colors.transparent,
                                      indicatorColor: Colors.yellow,
                                      labelColor: Colors.white,
                                      unselectedLabelColor: Colors.black,
                                      indicatorSize: TabBarIndicatorSize.tab,
                                      indicator: BoxDecoration(
                                          color: Colors.blue,
                                          borderRadius: BorderRadius.circular(10)), // Custom styling for selected tab
                                      tabs: const [
                                        Tab(text: 'Details'), // First tab
                                        Tab(text: 'Documents'), // Second tab
                                        Tab(text: 'Verify') // Third tab
                                      ]))),

                          drawer: const CommonDrawer(), // Drawer for navigation
                          body: TabBarView( // View for each tab
                              controller: value.tabController,
                              physics: const NeverScrollableScrollPhysics(), // Disable swipe gesture for tab switch
                              children: _pages)))),

              // Loading indicator overlay when `isLoading` is true
              if (value.isLoading)
                Container(
                    color: Colors.black.withOpacity(0.5), // Semi-transparent overlay
                    child: const Center(child: CircularProgressIndicator())) // Show loading spinner
            ]),
      ),
    );
  }
}
