import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/data/models/response/hatching/hatching_models.dart';
import 'package:rasadyar_chicken/presentation/pages/poultry_science/active_hatching/logic.dart';
import 'package:rasadyar_chicken/presentation/utils/nested_keys_utils.dart';
import 'package:rasadyar_chicken/presentation/widget/base_page/view.dart';
import 'package:rasadyar_core/core.dart';

class ActiveHatchingPage extends GetView<ActiveHatchingLogic> {
  const ActiveHatchingPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ChickenBasePage(
      hasSearch: true,
      hasFilter: false,
      backId: poultryFirstKey,
      routes: controller.routesName,
      onSearchChanged: (data) {
        controller.searchedValue.value = data;
        controller.getHatchingList();
      },
      child: hatchingWidget(),
      /*widgets: [
        hatchingWidget()
      ],*/
    );
  }

  Widget hatchingWidget() {
    return 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.activeFramSvg.path,
            );
          }, controller.expandedIndex);
        },
        itemCount: data.value.data?.results?.length ?? 0,
        separatorBuilder: (context, index) => SizedBox(height: 8.h),
        onLoadMore: () async => controller.getHatchingList(true),

      );
    }, controller.activeHatchingList);
  }

  Container itemListExpandedWidget(HatchingModel 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.poultry?.user?.fullname ?? 'N/A',
                textAlign: TextAlign.center,
                style: AppFonts.yekan16.copyWith(color: AppColor.greenDark),
              ),
              Spacer(),

              Visibility(
                child: Text(
                  item.violation == true ? 'پیگیری' : 'عادی',
                  textAlign: TextAlign.center,
                  style: AppFonts.yekan10.copyWith(color: AppColor.redDark),
                ),
              ),
            ],
          ),
          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.breed?.first.breed ?? 'N/A'}',
                  style: AppFonts.yekan14.copyWith(color: AppColor.textColor),
                ),
                Text(
                  ' سن${item.age} (روز)',

                  style: AppFonts.yekan14.copyWith(color: AppColor.blueNormal),
                ),
                Text(
                  ' دوره جوجه ریزی:${item.period}',
                  style: AppFonts.yekan14.copyWith(color: AppColor.textColor),
                ),
              ],
            ),
          ),

          buildRow(title: 'شماره مجوز جوجه ریزی', value: item.licenceNumber ?? 'N/A'),
          buildUnitRow(
            title: 'حجم جوجه ریزی',
            value: item.quantity.separatedByCommaFa,
            unit: '(قطعه)',
          ),
          buildUnitRow(
            title: 'مانده در سالن',
            value: item.leftOver.separatedByCommaFa,
            unit: '(قطعه)',
          ),
          buildUnitRow(title: 'تلفات', value: item.losses.separatedByCommaFa, unit: '(قطعه)'),
          buildRow(
            title: 'دامپزشک فارم',
            value: '${item.vetFarm?.vetFarmFullName}(${item.vetFarm?.vetFarmMobile})',
          ),
          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,
            ),
          ),
        ],
      ),
    );
  }

  Widget itemListWidget(HatchingModel item) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        SizedBox(width: 20),
        Expanded(
          flex: 2,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            spacing: 3,
            children: [
              Text(
                item.poultry?.user?.fullname ?? 'N/A',
                textAlign: TextAlign.start,
                style: AppFonts.yekan12.copyWith(color: AppColor.blueNormal),
              ),
              Text(
                item.poultry?.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.poultry?.unitName ?? 'N/Aaq',
                textAlign: TextAlign.start,
                style: AppFonts.yekan12.copyWith(color: AppColor.bgDark),
              ),
              Text(
                item.poultry?.licenceNumber ?? 'N/A',
                textAlign: TextAlign.left,
                style: AppFonts.yekan12.copyWith(color: AppColor.blueNormal),
              ),
            ],
          ),
        ),
        Expanded(
          flex: 1,
          child: Assets.vec.scanSvg.svg(
            width: 32.w,
            height: 32.h,
            colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
          ),
        ),
      ],
    );
  }
}
