import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/data/models/response/waiting_arrival/waiting_arrival.dart';
import 'package:rasadyar_chicken/presentation/utils/string_utils.dart';
import 'package:rasadyar_chicken/presentation/widget/list_item/list_item.dart' hide ListItem2;

import 'package:rasadyar_core/core.dart';

import 'logic.dart';

class BuyInProvinceWaitingPage extends GetView<BuyInProvinceWaitingLogic> {
  const BuyInProvinceWaitingPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 8),
        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.contains(index),
                  onTap: () => controller.isExpandedList.toggle(index),
                  index: index,
                  child: itemListWidget(item),
                  secondChild: itemListExpandedWidget(item),
                  labelColor: AppColor.blueLight,
                  labelIcon: Assets.vec.timerSvg.path,
                );
              }, controller.isExpandedList);
            },
            itemCount: data.value.data?.results?.length ?? 0,
            separatorBuilder: (context, index) => SizedBox(height: 8.h),
            onLoadMore: () async => controller.getWaitingArrivals(true),
            onRefresh: () async {
              controller.currentPage.value = 1;
              await controller.getWaitingArrivals();
            },
          );
        }, controller.waitingProduct),
      ),
      /* floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
      floatingActionButton: ObxValue((data) {
        if ((data.value.data?.results?.length ?? 0) > 1) {
          return AnimatedFab(
            onPressed: () {
             //TODO FAB
            },
            message: 'تایید یکجا',
            icon: Assets.vec.clipboardTaskSvg.svg(width: 45.w, height: 42.h),
            backgroundColor: controller.bgConfirmAllColor.value,
          );
        } else {
          return SizedBox.shrink();
        }
      }, controller.waitingProduct),*/
    );
  }

  Row itemListWidget(WaitingArrivalModel 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.steward?.user?.fullname ?? 'N/A',
                textAlign: TextAlign.start,
                style: AppFonts.yekan12.copyWith(color: AppColor.blueNormal),
              ),
              Text(
                item.date?.formattedJalaliDate ?? '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: [
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                spacing: 6,
                children: [
                  Visibility(
                    visible: item.product?.name?.contains('مرغ گرم') ?? false,
                    child: Assets.vec.hotChickenSvg.svg(
                      width: 24,
                      height: 24,
                      colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
                    ),
                  ),
                  Text(
                    '${item.weightOfCarcasses?.separatedByCommaFa}kg',
                    textAlign: TextAlign.left,
                    style: AppFonts.yekan12.copyWith(color: AppColor.blueNormal),
                  ),
                ],
              ),

              Text(
                item.steward?.guildsName ?? 'N/A',
                textAlign: TextAlign.start,
                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),
          ),
        ),
      ],
    );
  }

  Container itemListExpandedWidget(WaitingArrivalModel 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.steward?.user?.fullname ?? 'N/A',
                textAlign: TextAlign.center,
                style: AppFonts.yekan16.copyWith(color: AppColor.greenDark),
              ),
              Spacer(),
              Text(
                'در انتظار',
                textAlign: TextAlign.center,
                style: AppFonts.yekan10.copyWith(color: AppColor.darkGreyDark),
              ),
              SizedBox(width: 7),
              Assets.vec.clockSvg.svg(width: 16.w, height: 16.h),
            ],
          ),
          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: [
                Row(
                  spacing: 3,
                  children: [
                    Text(
                      item.date?.toJalali.formatter.wN ?? 'N/A',
                      style: AppFonts.yekan14.copyWith(color: AppColor.textColor),
                    ),

                    Text(
                      '${item.date?.toJalali.formatter.d} ${item.date?.toJalali.formatter.mN ?? 'N/A'}',
                      style: AppFonts.yekan14.copyWith(color: AppColor.blueNormal),
                    ),
                  ],
                ),

                Text(
                  '${item.date?.toJalali.formatter.y}',
                  style: AppFonts.yekan20.copyWith(color: AppColor.textColor),
                ),

                Text(
                  '${item.date?.toJalali.formatter.tHH}:${item.date?.toJalali.formatter.tMM ?? 'N/A'}',
                  style: AppFonts.yekan14.copyWith(color: AppColor.textColor),
                ),
              ],
            ),
          ),

          buildRow(
            title: 'مشخصات فروشنده',
            value: item.steward?.user?.fullname ?? 'N/A',
          ),
          buildRow(
            title: 'تلفن فروشنده',
            value: item.steward?.user?.mobile ?? 'N/A',
            valueStyle: AppFonts.yekan14.copyWith(color: AppColor.blueNormal),
          ),
          buildRow(
            title: 'نوع تخصیص',
            value: item.allocationType?.faAllocationType ?? 'N/A',
          ),
          buildRow(title: 'محصول', value: item.product?.name ?? 'N/A'),
          buildRow(
            title: 'وزن خریداری شده',
            value: '${item.weightOfCarcasses?.separatedByCommaFa} کیلوگرم',
          ),
          buildRow(title: 'قیمت کل', value: '${item.totalAmount?.separatedByCommaFa} ریال'),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            spacing: 16.w,
            children: [
              ObxValue((data) {
                return RElevated(
                  text: 'تایید',
                  width: 150.w,
                  height: 40.h,
                  isLoading: data[item.key!] ?? false,
                  onPressed: () async {
                    data[item.key!] = !(data[item.key!] ?? false);
                    await controller.acceptEntries(item);
                    data.remove(item.key!);
                  },
                  textStyle: AppFonts.yekan20.copyWith(color: Colors.white),
                  backgroundColor: AppColor.greenNormal,
                );
              }, controller.isLoadingConfirmMap),
              ROutlinedElevated(
                text: 'رد',
                textStyle: AppFonts.yekan20.copyWith(color: AppColor.redNormal),
                width: 150.w,
                height: 40.h,
                onPressed: () {
                  buildWarningDialog(
                    title: 'اخطار',
                    middleText: 'آیا از رد شدن این مورد اطمینان دارید؟',
                    onConfirm: () => controller.denyEntries(item),
                    onRefresh: () => controller.getWaitingArrivals(),
                  );
                },
                borderColor: AppColor.redNormal,
              ),
            ],
          ),
        ],
      ),
    );
  }
}
