import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/data/models/response/poultry_farm/poultry_farm.dart';
import 'package:rasadyar_chicken/presentation/utils/nested_keys_utils.dart';
import 'package:rasadyar_chicken/presentation/widget/base_page/view.dart';
import 'package:rasadyar_chicken/presentation/widget/filter_bottom_sheet.dart';
import 'package:rasadyar_core/core.dart';

import 'logic.dart';

class FarmPage extends GetView<FarmLogic> {
  const FarmPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ChickenBasePage(
      hasFilter: false,
      hasSearch: true,
      onRefresh: controller.onRefresh,
      onFilterTap: () {
        Get.bottomSheet(
          isScrollControlled: true,
          backgroundColor: Colors.transparent,
          filterBottomSheet(),
        );
      },
      onSearchChanged: (data) {
        controller.searchedValue.value = data;
        controller.getFarmList();
      },
      routes: controller.routes,
      backId: poultryFirstKey,
      child: Column(children: [firstTagInformation(), farmListWidget()]),
    );
  }

  Widget firstTagInformation() {
    return Padding(
      padding: const EdgeInsets.fromLTRB(0, 8, 0, 13),
      child: ObxValue((data) {
        return Padding(
          padding: EdgeInsets.symmetric(horizontal: 20.w),
          child: Row(
            spacing: 8,
            children: List.generate(
              data.length,
              (index) => Expanded(child: InformationTag(data: data[index])),
            ),
          ),
        );
      }, controller.tagInfo),
    );
  }

  Widget farmListWidget() {
    return Expanded(
      child: ObxValue((data) {
        return RPaginatedListView(
          listType: ListType.separated,
          resource: data.value,
          hasMore: data.value.data?.next != null,
          padding: EdgeInsets.fromLTRB(8, 8, 8, 80),
          itemBuilder: (context, index) {
            var item = data.value.data!.results![index];
            return ObxValue((val) {
              return ExpandableListItem2(
                selected: val.value.isEqual(index),
                onTap: () => controller.toggleExpanded(index),
                index: index,
                child: itemListWidget(item),
                secondChild: itemListExpandedWidget(item),
                labelColor: AppColor.blueLight,
                labelIcon: Assets.vec.cubeScanSvg.path,
              );
            }, controller.expandedIndex);
          },
          itemCount: data.value.data?.results?.length ?? 0,
          separatorBuilder: (context, index) => SizedBox(height: 8.h),
          onLoadMore: () async => controller.getFarmList(true),

        );
      }, controller.farmList),
    );
  }

  Container itemListExpandedWidget(PoultryFarm item) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 8),
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8)),
      child: Column(
        spacing: 8,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(
                item.unitName ?? 'N/A',
                textAlign: TextAlign.center,
                style: AppFonts.yekan16.copyWith(color: AppColor.greenDark),
              ),
              Spacer(),

              Visibility(
                child: Text(
                  '${item.address?.province?.name} / ${item.address?.city?.name}',
                  textAlign: TextAlign.center,
                  style: AppFonts.yekan14.copyWith(color: AppColor.blueNormal),
                ),
              ),
            ],
          ),
          Container(
            height: 32,
            padding: EdgeInsets.symmetric(horizontal: 8),
            decoration: ShapeDecoration(
              color: AppColor.blueLight,
              shape: RoundedRectangleBorder(
                side: BorderSide(width: 1, color: AppColor.blueLightHover),
                borderRadius: BorderRadius.circular(8),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  'تعاونی : ${item.cityOperator ?? 'ندارد'}',
                  style: AppFonts.yekan14.copyWith(color: AppColor.textColor),
                ),

                Text(
                  ' تعداد سالن : ${item.numberOfHalls}',
                  style: AppFonts.yekan14.copyWith(color: AppColor.textColor),
                ),
              ],
            ),
          ),

          buildRow(title: 'مالک/ تلفن', value: '${item.user?.fullname} (${item.user?.mobile})'),
          buildRow(title: 'شناسه یکتا', value: item.breedingUniqueId ?? 'N/A'),
          buildRow(title: 'کد اپیدمیولوژیک', value: item.epidemiologicalCode ?? 'N/A'),
          buildRow(title: 'کد بهداشتی', value: item.healthCertificateNumber ?? 'N/A'),
          buildRow(
            title: 'دامپزشک فارم',
            value: '${item.vetFarm?.fullName} (${item.vetFarm?.mobile ?? '-'})',
          ),
          buildUnitRow(
            title: 'ظرفیت فارم',
            value: item.totalCapacity.separatedByCommaFa,
            unit: '(قطعه)',
          ),
          buildRow(
            title: 'جوجه ریزی فعال (تعداد دوره) ',
            value:
                '${(item.hatchingInfo?.activeHatching ?? false) ? 'دارد' : 'ندارد'} (${item.hatchingInfo?.period ?? 0})',
          ),

          /*          buildRow(
            title: 'شرح بازرسی',
            value: item.reportInfo?.image == false ? 'ارسال تصویر جوجه ریزی فارم ' : 'تکمیل شده',
            titleStyle: AppFonts.yekan14.copyWith(
              color: (item.reportInfo?.image ?? false) ? AppColor.greenNormal : AppColor.redDark,
            ),
            valueStyle: AppFonts.yekan14.copyWith(
              color: (item.reportInfo?.image ?? false) ? AppColor.greenNormal : AppColor.redDark,
            ),
          ),*/
          /*   Visibility(
            visible: (item.reportInfo?.image == false),
            child: RElevated(
              text: 'ثبت بازرسی',
              isFullWidth: true,
              width: 150.w,
              height: 40.h,
              onPressed: () {
                cameraBottomSheet(item.id!);
              },
              textStyle: AppFonts.yekan20.copyWith(color: Colors.white),
              backgroundColor: AppColor.greenNormal,
            ),
          ),*/
        ],
      ),
    );
  }

  Widget itemListWidget(PoultryFarm item) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        SizedBox(width: 20),
        Expanded(
          flex: 2,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            spacing: 3,
            children: [
              Text(
                item.unitName ?? 'N/A',
                textAlign: TextAlign.start,
                style: AppFonts.yekan12.copyWith(color: AppColor.blueNormal),
              ),
              Text(
                item.user?.mobile ?? 'N/A',
                textAlign: TextAlign.center,
                style: AppFonts.yekan14.copyWith(color: AppColor.bgDark),
              ),
            ],
          ),
        ),
        Expanded(
          flex: 3,
          child: Column(
            spacing: 3,
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                'شناسه یکتا: ${item.breedingUniqueId}',
                textAlign: TextAlign.start,
                style: AppFonts.yekan12.copyWith(color: AppColor.blueNormal),
              ),
              Text(
                '${item.address?.province?.name}/${item.address?.city?.name}',
                textAlign: TextAlign.left,

                style: AppFonts.yekan12.copyWith(color: AppColor.bgDark),
              ),
            ],
          ),
        ),
        Expanded(
          flex: 1,
          child: Assets.vec.scanSvg.svg(
            width: 32.w,
            height: 32.h,
            colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
          ),
        ),
      ],
    );
  }

  Widget filterBottomSheet() => filterBottomSheetWidget(
    fromDate: controller.fromDateFilter,
    onChangedFromDate: (jalali) => controller.fromDateFilter.value = jalali,
    toDate: controller.toDateFilter,
    onChangedToDate: (jalali) => controller.toDateFilter.value = jalali,
    onSubmit: () => controller.getFarmList(),
  );
}
