
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rasadyar_chicken/data/models/response/iran_province_city/iran_province_city_model.dart';
import 'package:rasadyar_chicken/data/models/response/roles_products/roles_products.dart';
import 'package:rasadyar_chicken/data/models/response/steward_free_bar/steward_free_bar.dart';
import 'package:rasadyar_core/core.dart';

import 'logic.dart';
class BuysOutOfProvincePage extends GetView<BuysOutOfProvinceLogic> {
  const BuysOutOfProvincePage({super.key});


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: RAppBar(
        titleTextStyle: AppFonts.yekan16Bold.copyWith(color: Colors.white),
        centerTitle: true,
        hasBack: true,
        onBackPressed: () {
          Get.back(id: 1);

        },
        leadingWidth: 155,
        leading: Row(
          mainAxisSize: MainAxisSize.min,
          spacing: 6,
          children: [
            Assets.vec.chickenSvg.svg(
              width: 24,
              height: 24,
              colorFilter: const ColorFilter.mode(Colors.white, BlendMode.srcIn),
            ),
            Text('رصدطیور', style: AppFonts.yekan16Bold.copyWith(color: Colors.white)),
          ],
        ),
        additionalActions: [
          GestureDetector(
            onTap: () {
              controller.searchIsSelected.value = !controller.searchIsSelected.value;
            },
            child: Assets.vec.searchSvg.svg(
              width: 24,
              height: 24,
              colorFilter: const ColorFilter.mode(Colors.white, BlendMode.srcIn),
            ),
          ),
          SizedBox(width: 8),
          GestureDetector(
            onTap: () {
              Get.bottomSheet(filterBottomSheet());
            },
            child: Assets.vec.filterOutlineSvg.svg(
              width: 20,
              height: 20,
              colorFilter: const ColorFilter.mode(Colors.white, BlendMode.srcIn),
            ),
          ),
          SizedBox(width: 8),
        ],
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          routePageWidget(),
          _buildSearchWidget(),
          Expanded(child: saleListWidget()),
        ],
      ),
      floatingActionButton: RFab.add(
        onPressed: () {
          Get.bottomSheet(addPurchasedInformationBottomSheet(), isScrollControlled: true);
        },
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.startFloat,
    );
  }

  Widget saleListWidget() {
    return ObxValue((data) {
      switch (data.value.status) {
        case Status.initial:
        case Status.loading:
          return Center(child: CupertinoActivityIndicator());
        case Status.success:
          return ListView.separated(
            shrinkWrap: true,
            physics: BouncingScrollPhysics(),
            padding: EdgeInsets.fromLTRB(8, 8, 18, 80),
            itemBuilder: (context, index) {
              return ObxValue(
                    (expandList) => saleListItem(expandList: expandList, index: index, item: data.value.data![index]),
                controller.isExpandedList,
              );
            },
            separatorBuilder: (context, index) => SizedBox(height: 8),
            itemCount: data.value.data?.length ?? 0,
          );
        case Status.error:
          return Center(
            child: Text(
              data.value.message ?? 'خطا در دریافت اطلاعات',
              style: AppFonts.yekan16.copyWith(color: AppColor.error),
            ),
          );
        case Status.empty:
          return emptyWidget();
      }
    }, controller.purchaseOutOfProvinceList);
  }

  Widget emptyWidget() {
    return Center(
      child: Text('داده ای دریافت نشد!', style: AppFonts.yekan16.copyWith(color: AppColor.darkGreyDarkHover)),
    );
  }

  GestureDetector saleListItem({required RxList<int> expandList, required int index, required StewardFreeBar item}) {
    return GestureDetector(
      onTap: () {
        if (expandList.contains(index)) {
          controller.isExpandedList.remove(index);
        } else {
          controller.isExpandedList.add(index);
        }
      },
      child: AnimatedSize(
        duration: Duration(milliseconds: 400),
        alignment: Alignment.center,
        child: Stack(
          clipBehavior: Clip.none,
          alignment: Alignment.centerRight,
          children: [
            AnimatedSize(
              duration: Duration(milliseconds: 300),
              child: Container(
                width: Get.width - 30,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Colors.transparent,
                  borderRadius: BorderRadius.circular(8),
                  border: Border.all(width: 0.5, color: AppColor.darkGreyNormal),
                ),
                child: AnimatedCrossFade(
                  alignment: Alignment.center,
                  firstChild: Padding(
                    padding: const EdgeInsets.symmetric(vertical: 10),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        SizedBox(width: 12),
                        Expanded(
                          flex: 2,
                          child: Text(
                            item.date?.formattedJalaliDate ?? 'N/A',
                            textAlign: TextAlign.center,
                            style: AppFonts.yekan14.copyWith(color: AppColor.bgDark),
                          ),
                        ),
                        Expanded(
                          flex: 3,
                          child: Text(
                            item.killHouseName ?? 'N/A',
                            textAlign: TextAlign.center,
                            style: AppFonts.yekan12.copyWith(color: AppColor.blueNormal),
                          ),
                        ),
                        SizedBox(width: 8,),
                        Expanded(
                          flex: 2,
                          child: Column(
                            children: [
                              Text(
                                '${item.weightOfCarcasses?.separatedByComma}kg',
                                textAlign: TextAlign.left,
                                style: AppFonts.yekan12.copyWith(color: AppColor.blueNormal),
                              ),

                              SizedBox(height: 2,),
                              Visibility(
                                  visible: item.product?.name?.contains('مرغ گرم') ?? false,
                                  child: Assets.vec.hotChickenSvg.svg(width: 24,height: 24, colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn))),

                            ],
                          ),
                        ),
                        Expanded(
                          flex: 2,
                          child: Text(
                            '${item.province}\n${item.city}',
                            textAlign: TextAlign.center,
                            style: AppFonts.yekan12.copyWith(color: AppColor.bgDark),
                          ),
                        ),
                        Icon(CupertinoIcons.chevron_down, size: 12),
                        SizedBox(width: 8),
                      ],
                    ),
                  ),
                  secondChild: Container(
                    padding: EdgeInsets.fromLTRB(8, 12, 14, 12),

                    decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8)),
                    child: Column(
                      spacing: 8,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            GestureDetector(
                              onTap: () {
                                controller.setEditData(item);
                                Get.bottomSheet(
                                  addPurchasedInformationBottomSheet(true),
                                  isScrollControlled: true,
                                ).whenComplete(() {
                                  controller.resetSubmitForm();
                                });
                              },
                              child: Assets.vec.editSvg.svg(
                                width: 20,
                                height: 20,
                                colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
                              ),
                            ),

                            Text(
                              '${item.province}-${item.city}',
                              textAlign: TextAlign.center,
                              style: AppFonts.yekan16.copyWith(color: AppColor.greenDark),
                            ),

                            GestureDetector(
                              onTap: () {
                                buildDeleteDialog(
                                  onConfirm: () => controller.deleteStewardPurchaseOutOfProvince(item.key!),
                                );
                              },
                              child: Assets.vec.trashSvg.svg(
                                width: 20,
                                height: 20,
                                colorFilter: ColorFilter.mode(AppColor.error, BlendMode.srcIn),
                              ),
                            ),
                          ],
                        ),
                        Container(
                          height: 32,
                          padding: EdgeInsets.symmetric(horizontal: 4),
                          decoration: ShapeDecoration(
                            color: AppColor.blueLight,
                            shape: RoundedRectangleBorder(
                              side: BorderSide(width: 1, color: AppColor.blueLightHover),
                              borderRadius: BorderRadius.circular(8),
                            ),
                          ),
                          child: buildRow('تاریخ', item.date?.formattedJalaliDateYHMS ?? 'N/A'),
                        ),

                        buildRow('مشخصات فروشنده', '${item.killHouseName} - ${item.killHouseMobile ?? 'N/A'}'),
                        buildRow('محصول', item.product?.name  ??'N/A'),
                        buildRow('وزن خریداری شده', '${item.weightOfCarcasses?.separatedByComma} کیلوگرم'),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            GestureDetector(
                              onTap: () {
                                Get.bottomSheet(
                                  BaseBottomSheet(
                                    height: 400,
                                    child: Column(
                                      spacing: 16,
                                      children: [
                                        Text(
                                          'بارنامه',
                                          style: AppFonts.yekan16Bold.copyWith(color: AppColor.darkGreyDarkHover),
                                        ),

                                        Image.network(
                                          item.barImage ?? '',
                                          fit: BoxFit.cover,
                                          height: 300,
                                          errorBuilder: (context, error, stackTrace) {
                                            eLog(error.toString());
                                            return Center(child: Text('خطایی پیش آمده!'));
                                          },
                                          loadingBuilder: (context, child, loadingProgress) {
                                            if (loadingProgress == null) return child;
                                            print('Loading progress: $loadingProgress');
                                            return CupertinoActivityIndicator();
                                          },
                                        ),
                                      ],
                                    ),
                                  ),
                                );
                              },
                              child: Text('مشاهده بارنامه', style: AppFonts.yekan14.copyWith(color: AppColor.blueNormal)),
                            ),
                            Icon(CupertinoIcons.chevron_up, size: 12),
                          ],
                        ),
                      ],
                    ),
                  ),
                  crossFadeState: expandList.contains(index) ? CrossFadeState.showSecond : CrossFadeState.showFirst,
                  duration: Duration(milliseconds: 300),
                ),
              ),
            ),
            Positioned(
              right: -12,
              child: Container(
                width: index < 999 ? 24 : null,
                height: index < 999 ? 24 : null,
                padding: EdgeInsets.all(2),
                decoration: BoxDecoration(
                  color: AppColor.greenLightHover,
                  borderRadius: BorderRadius.circular(4),
                  border: Border.all(width: 0.50, color: AppColor.greenDarkActive),
                ),
                alignment: Alignment.center,
                child: Text((index + 1).toString(), style: AppFonts.yekan12.copyWith(color: Colors.black)),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Row routePageWidget() {
    return Row(
      children: [
        SizedBox(width: 8),
        RichText(
          text: TextSpan(
            style: AppFonts.yekan14.copyWith(color: AppColor.blueNormal),
            children: [
              WidgetSpan(
                child: Row(
                  children: [
                    Assets.vec.cubeSearchSvg.svg(
                      width: 24,
                      height: 24,
                      colorFilter: const ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
                    ),
                    SizedBox(width: 6),
                  ],
                ),
              ),

              TextSpan(text: 'خارج استان'),
              TextSpan(text: '/'),
              TextSpan(text: 'خرید'),
            ],
          ),
        ),
      ],
    );
  }

  Widget buildRow(String title, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Flexible(
            flex: 2,
            child: Text(
              title,
              textAlign: TextAlign.right,
              style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDarkHover),
            ),
          ),
          Flexible(
            flex: 2,
            child: Text(
              value,
              textAlign: TextAlign.left,

              style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDarkHover),
            ),
          ),
        ],
      ),
    );
  }

  Widget addPurchasedInformationBottomSheet([bool isOnEdit = false]) {
    return BaseBottomSheet(
      child: SingleChildScrollView(
        child: Form(
          key: controller.formKey,
          child: Column(
            spacing: 16,
            children: [
              Text(
                isOnEdit ? 'ویرایش اطلاعات خرید' : 'ثبت اطلاعات خرید',
                style: AppFonts.yekan16Bold.copyWith(color: AppColor.darkGreyDarkHover),
              ),
              _productTypeWidget(),
              RTextField(
                controller: controller.sellerNameController,
                label: 'نام فروشنده',
                borderColor: AppColor.darkGreyLight,
              ),
              RTextField(
                controller: controller.sellerPhoneController,
                label: 'تلفن فروشنده',
                keyboardType: TextInputType.phone,
                borderColor: AppColor.darkGreyLight,
                maxLength: 11,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'لطفاً شماره موبایل را وارد کنید';
                  }
                  // حذف کاماها برای اعتبارسنجی
                  String cleaned = value.replaceAll(',', '');
                  if (cleaned.length != 11) {
                    return 'شماره موبایل باید ۱۱ رقم باشد';
                  }
                  if (!cleaned.startsWith('09')) {
                    return 'شماره موبایل باید با 09 شروع شود';
                  }
                  return null;
                },
              ),
              _provinceWidget(),
              _cityWidget(),
              RTextField(
                controller: controller.carcassWeightController,
                label: 'وزن لاشه',
                keyboardType: TextInputType.number,
                borderColor: AppColor.darkGreyLight,
                inputFormatters: [FilteringTextInputFormatter.digitsOnly, SeparatorInputFormatter()],
              ),
              _imageCarcasesWidget(isOnEdit),
              submitButtonWidget(isOnEdit),
              SizedBox(),
            ],
          ),
        ),
      ),
    );
  }

  Widget submitButtonWidget(bool isOnEdit) {
    return ObxValue((data) {
      return RElevated(
        text: isOnEdit ? 'ویرایش' : 'ثبت خرید',
        onPressed: data.value
            ? () async {
          var res = await controller.createStewardPurchaseOutOfProvince();
          if (res) {
            Get.back();
          }
        }
            : null,
        height: 40,
      );
    }, controller.isSubmitButtonEnabled);
  }

  Widget _productTypeWidget() {
    return Obx(() {
      return OverlayDropdownWidget<ProductModel>(
        items: controller.outOfTheProvinceLogic.rolesProductsModel,
        onChanged: (value) {
          controller.selectedProduct.value = value;
        },
        selectedItem: controller.selectedProduct.value,
        initialValue: controller.selectedProduct.value,
        itemBuilder: (item) => Text(item.name ?? 'بدون نام'),
        labelBuilder: (item) => Text(item?.name ?? 'انتخاب محصول'),
      );
    });
  }

  Widget _provinceWidget() {
    return Obx(() {
      return OverlayDropdownWidget<IranProvinceCityModel>(
        items: controller.rootLogic.provinces,
        onChanged: (value) {
          controller.selectedProvince.value = value;
          print('Selected Product: ${value.name}');
        },
        selectedItem: controller.selectedProvince.value,
        itemBuilder: (item) => Text(item.name ?? 'بدون نام'),
        labelBuilder: (item) => Text(item?.name ?? 'انتخاب استان'),
      );
    });
  }

  Widget _cityWidget() {
    return ObxValue((data) {
      return OverlayDropdownWidget<IranProvinceCityModel>(
        items: data,
        onChanged: (value) {
          controller.selectedCity.value = value;
          print('Selected Product: ${value.name}');
        },
        selectedItem: controller.selectedCity.value,
        itemBuilder: (item) => Text(item.name ?? 'بدون نام'),
        labelBuilder: (item) => Text(item?.name ?? 'انتخاب شهر'),
      );
    }, controller.cites);
  }

  SizedBox _imageCarcasesWidget(bool isOnEdit) {
    return SizedBox(
      width: Get.width,
      height: 250,
      child: Card(
        color: Colors.white,
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            children: [
              Expanded(
                child: ObxValue((data) {
                  return Container(
                    width: Get.width,
                    decoration: BoxDecoration(color: AppColor.lightGreyNormal, borderRadius: BorderRadius.circular(8)),
                    child: Center(
                      child: isOnEdit
                          ? Image.network(controller.editImageUrl.value ?? '')
                          : data.value == null
                          ? Assets.images.placeHolder.image(height: 150, width: 200)
                          : Image.file(File(data.value!.path), fit: BoxFit.cover),
                    ),
                  );
                }, controller.selectedImage),
              ),
              SizedBox(height: 15),
              Container(
                width: Get.width,
                height: 40,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text('تصویر بار', style: AppFonts.yekan16Bold.copyWith(color: AppColor.blueNormal)),
                    Spacer(),
                    GestureDetector(
                      onTap: () async {
                        controller.selectedImage.value = await controller.imagePicker.pickImage(
                          source: ImageSource.camera,
                          imageQuality: 60,
                          maxWidth: 1080,
                          maxHeight: 720,
                        );
                      },
                      child: Container(
                        decoration: ShapeDecoration(
                          color: AppColor.blueNormal,
                          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
                        ),
                        padding: EdgeInsetsGeometry.symmetric(horizontal: 6, vertical: 4),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text('دوربین', style: AppFonts.yekan14.copyWith(color: Colors.white)),
                            SizedBox(width: 8),
                            Icon(CupertinoIcons.camera, color: Colors.white),
                          ],
                        ),
                      ),
                    ),
                    SizedBox(width: 16),
                    GestureDetector(
                      onTap: () async {
                        controller.selectedImage.value = await controller.imagePicker.pickImage(
                          source: ImageSource.gallery,
                          imageQuality: 60,
                          maxWidth: 1080,
                          maxHeight: 720,
                        );
                      },
                      child: Container(
                        decoration: ShapeDecoration(
                          color: AppColor.blueNormal,
                          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
                        ),
                        padding: EdgeInsetsGeometry.symmetric(horizontal: 6, vertical: 4),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text('گالری', style: AppFonts.yekan14.copyWith(color: Colors.white)),
                            SizedBox(width: 8),
                            Icon(CupertinoIcons.photo, color: Colors.white),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> buildDeleteDialog({required Future<void> Function() onConfirm}) async {
    await Get.defaultDialog(
      title: 'حذف خرید',
      middleText: 'آیا از حذف این خرید مطمئن هستید؟',
      confirm: ElevatedButton(
        style: ElevatedButton.styleFrom(backgroundColor: AppColor.error, foregroundColor: Colors.white),
        onPressed: () async {
          await onConfirm();
          Get.back();
        },
        child: Text('بله'),
      ),
      cancel: ElevatedButton(
        onPressed: () {
          Get.back();
        },
        child: Text('خیر'),
      ),
    ).whenComplete(() => controller.getStewardPurchaseOutOfProvince());
  }

  Widget filterBottomSheet() {
    return BaseBottomSheet(
      height: 250,
      child: Column(
        spacing: 16,
        children: [
          Text('فیلترها', style: AppFonts.yekan16Bold.copyWith(color: AppColor.darkGreyDarkHover)),
          Row(
            spacing: 8,
            children: [
              Expanded(
                child: timeFilterWidget(
                  date: controller.fromDateFilter,
                  onChanged: (jalali) => controller.fromDateFilter.value = jalali,
                ),
              ),
              Expanded(
                child: timeFilterWidget(
                  isFrom: false,
                  date: controller.toDateFilter,
                  onChanged: (jalali) => controller.toDateFilter.value = jalali,
                ),
              ),
            ],
          ),
          SizedBox(height: 2),
          RElevated(
            text: 'اعمال فیلتر',
            onPressed: () {
              controller.getStewardPurchaseOutOfProvince();
              Get.back();
            },
            height: 40,
          ),
          SizedBox(height: 16),
        ],
      ),
    );
  }

  GestureDetector timeFilterWidget({
    isFrom = true,
    required Rx<Jalali> date,
    required Function(Jalali jalali) onChanged,
  }) {
    return GestureDetector(
      onTap: () {
        Get.bottomSheet(modalDatePicker((value) => onChanged(value)));
      },
      child: Container(
        height: 35,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8),
          border: Border.all(width: 1, color: AppColor.blueNormal),
        ),
        padding: EdgeInsets.symmetric(horizontal: 11, vertical: 4),
        child: Row(
          spacing: 8,
          children: [
            Assets.vec.calendarSvg.svg(
              width: 24,
              height: 24,
              colorFilter: const ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
            ),
            Text(isFrom ? 'از' : 'تا', style: AppFonts.yekan16.copyWith(color: AppColor.blueNormal)),
            Expanded(
              child: ObxValue((data) {
                return Text(
                  date.value.formatCompactDate(),
                  textAlign: TextAlign.center,
                  style: AppFonts.yekan16.copyWith(color: AppColor.lightGreyNormalActive),
                );
              }, date),
            ),
          ],
        ),
      ),
    );
  }

  Container modalDatePicker(ValueChanged<Jalali> onDateSelected) {
    Jalali? tempPickedDate;
    return Container(
      height: 250,
      color: Colors.white,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(
            child: Row(
              children: [
                SizedBox(width: 20),
                RElevated(
                  height: 35,
                  width: 70,
                  textStyle: AppFonts.yekan14.copyWith(color: Colors.white),
                  onPressed: () {
                    onDateSelected(tempPickedDate ?? Jalali.now());
                    Get.back();
                  },
                  text: 'تایید',
                ),
                Spacer(),
                RElevated(
                  height: 35,
                  width: 70,
                  backgroundColor: AppColor.error,
                  textStyle: AppFonts.yekan14.copyWith(color: Colors.white),
                  onPressed: () {
                    onDateSelected(tempPickedDate ?? Jalali.now());
                    Get.back();
                  },
                  text: 'لغو',
                ),
                SizedBox(width: 20),
              ],
            ),
          ),
          Divider(height: 0, thickness: 1),
          Expanded(
            child: Container(
              child: PersianCupertinoDatePicker(
                initialDateTime: Jalali.now(),
                mode: PersianCupertinoDatePickerMode.date,
                onDateTimeChanged: (dateTime) {
                  tempPickedDate = dateTime;
                },
              ),
            ),
          ),
        ],
      ),
    );
  }

  ObxValue<RxBool> _buildSearchWidget() {
    return ObxValue((data) {
      return AnimatedContainer(
        duration: Duration(milliseconds: 300),
        padding: EdgeInsets.only(top: 5),
        curve: Curves.easeInOut,
        height: data.value ? 50 : 0,
        child: Visibility(
          visible: data.value,
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8),
            child: RTextField(
              suffixIcon: Padding(
                padding: const EdgeInsets.all(12.0),
                child: Assets.vec.searchSvg.svg(
                  width: 10,
                  height: 10,
                  colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
                ),
              ),
              hintText: 'جستجو',
              onChanged: (value) {
                controller.searchedValue.value = value;
              },
              controller: TextEditingController(),
            ),
          ),
        ),
      );
    }, controller.searchIsSelected);
  }
}
