import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:pharmacy_mobile/auth/auth_service.dart';
import 'package:pharmacy_mobile/tables/cart.dart';
import 'package:pharmacy_mobile/tables/stocks.dart';
import 'package:pharmacy_mobile/tables/storage.dart';
import 'package:pharmacy_mobile/widgets/customer_pagebackground_widget.dart';
import 'package:pharmacy_mobile/widgets/customer_title_widget.dart';
import 'package:pharmacy_mobile/widgets/indicator_widget.dart';
import 'package:pharmacy_mobile/widgets/input_widget.dart';
import 'package:pharmacy_mobile/widgets/item_card_widget.dart';

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

  @override
  State<CustomerSearchPage> createState() => _CustomerSearchPageState();
}

class _CustomerSearchPageState extends State<CustomerSearchPage> {
  final _searchController = TextEditingController();
  final _authService = AuthService();
  final _storage = Storage();
  // final _refMedicines = RefMedicines();
  final _stocks = Stocks();
  final _carts = Carts();
  // final _refMedicines = RefMedicines();
  late String imageUrl = '';
  late List _stockList = [];
  late List _cartList = [];

  Future<String> _getURL(String imageName) async {
    final image = await _storage.getImageURL('ref_medicines_images', '$imageName.webp');
    return image;
  }

  Future<void> _getAllStocks() async {
    _stockList = await _stocks.getListWithUUID();
    if (_stockList.isNotEmpty) {
      for (int i = 0; i < _stockList.length; i++) {
        _stockList[i]['uuid'] = await _getURL(_stockList[i]['uuid']);
      }
      setState(() {});
    }
  }

  Future<String> _getCurrentUserId() async {
    final result = _authService.getCurrentUserId();
    return result.toString();
  }

  Future<void> _getCarts() async {
    final currentUser = await _getCurrentUserId();
    _cartList = await _carts.getCart(currentUser);
  }

  // Future<void> _getAllMedicines() async {
  //   _medicinesList = await _refMedicines.getList();
  // }

  // void getURLs() async {
  //   try {demo@
  //     for (int i = 1; i <= 4; i++) {
  //       final image = await _storage.getPublicURL(
  //         context,
  //         'ref_medicines_images',
  //         'ca3e2949-4964-4d25-a274-2a18608b7bdb.webp', // Replace with your actual image path
  //       );
  //       log(image);
  //       setState(() {
  //         imageUrl.add(image);
  //       });
  //     }
  //   } catch (e, stackTrace) {
  //     log('Error getting URLs: $e', stackTrace: stackTrace);
  //   }
  // }

  // void _filterList() {}

  void autoRun() async {
    await _getAllStocks();
    await _getCarts();
    // // // await _getAllMedicines();
    // await _getURL();
  }

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

  @override
  void dispose() {
    _searchController.dispose();
    imageUrl = '';
    _stockList = [];
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: CustomerPagebackgroundWidget(
            height: MediaQuery.of(context).size.height + 200,
            child: Column(
              children: [
                Column(
                  children: [
                    const Gap(68),
                    const CustomerTitleWidget(),
                    const Gap(8),
                    Container(
                      padding: const EdgeInsets.symmetric(horizontal: 8),
                      child: Column(
                        children: [
                          InputWidget(
                            label: '',
                            controller: _searchController,
                            placeholder: 'Search for medicine',
                          ),
                        ],
                      ),
                    ),
                    const Gap(32),
                    const IndicatorWidget(text: 'Prescribed Medicines'),
                    const Gap(8),
                    SingleChildScrollView(
                        scrollDirection: Axis.horizontal,
                        child: Row(
                          children: [
                            if (_stockList.isEmpty)
                              const Row(
                                children: [
                                  ItemCardWidget(
                                      imageUrl: 'imageUrl',
                                      text: 'item[medicine_name]',
                                      subtext: 'item[generic_name]',
                                      price: 123,
                                      quantity: 123,
                                      isLoading: true,
                                      cart: 0),
                                  ItemCardWidget(
                                      imageUrl: 'imageUrl',
                                      text: 'item[medicine_name]',
                                      subtext: 'item[generic_name]',
                                      price: 123,
                                      quantity: 123,
                                      isLoading: true,
                                      cart: 0),
                                  ItemCardWidget(
                                      imageUrl: 'imageUrl',
                                      text: 'item[medicine_name]',
                                      subtext: 'item[generic_name]',
                                      price: 123,
                                      quantity: 123,
                                      isLoading: true,
                                      cart: 0)
                                ],
                              )
                            else
                              for (var item in _stockList)
                                ItemCardWidget(
                                    imageUrl: item['uuid'],
                                    text: item['medicine_name'].toString(),
                                    subtext: item['generic_name'].toString(),
                                    price: item['price'].toDouble(),
                                    quantity: item['quantity'].toDouble(),
                                    isLoading: false,
                                    cart: 0),
                          ],
                        )),
                    const Gap(16),
                    const IndicatorWidget(text: 'All Stocks'),
                    const Gap(8),
                    SingleChildScrollView(
                        scrollDirection: Axis.horizontal,
                        child: Row(
                          children: [
                            if (_stockList.isEmpty)
                              const Row(
                                children: [
                                  ItemCardWidget(
                                      imageUrl: 'imageUrl',
                                      text: 'item[medicine_name]',
                                      subtext: 'item[generic_name]',
                                      price: 123,
                                      quantity: 123,
                                      isLoading: true,
                                      cart: 0),
                                  ItemCardWidget(
                                      imageUrl: 'imageUrl',
                                      text: 'item[medicine_name]',
                                      subtext: 'item[generic_name]',
                                      price: 123,
                                      quantity: 123,
                                      isLoading: true,
                                      cart: 0),
                                  ItemCardWidget(
                                      imageUrl: 'imageUrl',
                                      text: 'item[medicine_name]',
                                      subtext: 'item[generic_name]',
                                      price: 123,
                                      quantity: 123,
                                      isLoading: true,
                                      cart: 0)
                                ],
                              )
                            else
                              for (var item in _stockList)
                                ItemCardWidget(
                                    imageUrl: item['uuid'].toString(),
                                    text: item['medicine_name'].toString(),
                                    subtext: item['generic_name'].toString(),
                                    price: item['price'].toDouble(),
                                    quantity: item['quantity'].toDouble(),
                                    isLoading: false,
                                    cart: 0),
                          ],
                        )),
                    // const Gap(16),
                    // const IndicatorWidget(text: 'For Fever'),
                    // const Gap(8),
                    // SingleChildScrollView(
                    //     scrollDirection: Axis.horizontal,
                    //     child: Row(
                    //       children: [
                    //         ItemCardWidget(
                    //             imageUrl: imageUrl,
                    //             text: 'sample',
                    //             subtext: 'generic_name',
                    //             price: 500,
                    //             quantity: 0,
                    //             isLoading: imageUrl.isEmpty,
                    //             cart: 2),
                    //         ItemCardWidget(
                    //             imageUrl: imageUrl,
                    //             text: 'sample',
                    //             subtext: 'generic_name',
                    //             price: 500,
                    //             quantity: 15,
                    //             isLoading: imageUrl.isEmpty,
                    //             cart: 0),
                    //         ItemCardWidget(
                    //             imageUrl: imageUrl,
                    //             text: 'sample',
                    //             subtext: 'generic_name',
                    //             price: 500,
                    //             quantity: 15,
                    //             isLoading: imageUrl.isEmpty,
                    //             cart: 0),
                    //       ],
                    //     )),
                    // const Gap(16),
                    // const IndicatorWidget(text: 'For Headaches'),
                    // const Gap(8),
                    // SingleChildScrollView(
                    //     scrollDirection: Axis.horizontal,
                    //     child: Row(
                    //       children: [
                    //         ItemCardWidget(
                    //             imageUrl: imageUrl,
                    //             text: 'sample',
                    //             subtext: 'generic_name',
                    //             price: 500,
                    //             quantity: 15,
                    //             isLoading: imageUrl.isEmpty,
                    //             cart: 0),
                    //         ItemCardWidget(
                    //             imageUrl: imageUrl,
                    //             text: 'sample',
                    //             subtext: 'generic_name',
                    //             price: 500,
                    //             quantity: 15,
                    //             isLoading: imageUrl.isEmpty,
                    //             cart: 0),
                    //         ItemCardWidget(
                    //             imageUrl: imageUrl,
                    //             text: 'sample',
                    //             subtext: 'generic_name',
                    //             price: 500,
                    //             quantity: 15,
                    //             isLoading: imageUrl.isEmpty,
                    //             cart: 0),
                    //       ],
                    //     )),
                    // const Gap(16),
                    // const IndicatorWidget(text: 'For Colds and Coughes'),
                    // const Gap(8),
                    // SingleChildScrollView(
                    //     scrollDirection: Axis.horizontal,
                    //     child: Row(
                    //       children: [
                    //         ItemCardWidget(
                    //             imageUrl: imageUrl,
                    //             text: 'sample',
                    //             subtext: 'generic_name',
                    //             price: 500,
                    //             quantity: 15,
                    //             isLoading: imageUrl.isEmpty,
                    //             cart: 0),
                    //         ItemCardWidget(
                    //             imageUrl: imageUrl,
                    //             text: 'sample',
                    //             subtext: 'generic_name',
                    //             price: 500,
                    //             quantity: 15,
                    //             isLoading: imageUrl.isEmpty,
                    //             cart: 0),
                    //         ItemCardWidget(
                    //             imageUrl: imageUrl,
                    //             text: 'sample',
                    //             subtext: 'generic_name',
                    //             price: 500,
                    //             quantity: 15,
                    //             isLoading: imageUrl.isEmpty,
                    //             cart: 0),
                    //       ],
                    //     )),
                    // const Gap(16),
                    // const IndicatorWidget(text: 'For Infection and Irritations'),
                    // const Gap(8),
                    // SingleChildScrollView(
                    //     scrollDirection: Axis.horizontal,
                    //     child: Row(
                    //       children: [
                    //         ItemCardWidget(
                    //             imageUrl: imageUrl,
                    //             text: 'sample',
                    //             subtext: 'generic_name',
                    //             price: 500,
                    //             quantity: 15,
                    //             isLoading: imageUrl.isEmpty,
                    //             cart: 0),
                    //         ItemCardWidget(
                    //             imageUrl: imageUrl,
                    //             text: 'sample',
                    //             subtext: 'generic_name',
                    //             price: 500,
                    //             quantity: 15,
                    //             isLoading: imageUrl.isEmpty,
                    //             cart: 0),
                    //         ItemCardWidget(
                    //             imageUrl: imageUrl,
                    //             text: 'sample',
                    //             subtext: 'generic_name',
                    //             price: 500,
                    //             quantity: 15,
                    //             isLoading: imageUrl.isEmpty,
                    //             cart: 0),
                    //       ],
                    //     )),
                  ],
                )
              ],
            )));
  }
}
