import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_core/presentation/widget/buttons/fab.dart';

import 'logic.dart';

class TaggingPage extends GetView<TaggingLogic> {
  const TaggingPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
     /* appBar: RAppBar(
        title: 'پلاک کوبی',
        leadingWidth: 40,
        leading: Assets.vec.messageAddSvg.svg(width: 12, height: 12),
        additionalActions: [
          GestureDetector(
            onTap: () {
              if (controller.searchIsSelected.value) {
                controller.searchIsSelected.value = !controller.searchIsSelected.value;
              }
              controller.filterIsSelected.value = !controller.filterIsSelected.value;
            },
            child: Assets.icons.filter.svg(width: 20, height: 20),
          ),
          SizedBox(width: 16),
          GestureDetector(
            onTap: () {
              if (controller.filterIsSelected.value) {
                controller.filterIsSelected.value = !controller.filterIsSelected.value;
              }
              controller.searchIsSelected.value = !controller.searchIsSelected.value;
            },
            child: Assets.icons.search.svg(width: 20, height: 20),
          ),
        ],
      ),*/
      body: Stack(
        children: [
          Column(
            children: [
              _buildFilterWidget(),
              _buildSearchWidget(),
              _buildInfoDetails(),
              _buildListContent(),
              SizedBox(height: 10),

              Padding(
                padding: const EdgeInsets.fromLTRB(35, 0, 35, 20),
                child: RElevated(
                  text: 'ثبت نهایی و ارسال به اتحادیه',
                  textStyle: AppFonts.yekan18,
                  height: 40,
                  backgroundColor: AppColor.greenNormal,
                  onPressed: () {},
                  isFullWidth: true,
                ),
              ),
            ],
          ),
          Positioned(
            right: 10,
            bottom: 75,
            child: RFab.add(
              onPressed: () {
                Get.bottomSheet(_buildBottomSheet(), isScrollControlled: true);
              },
            ),
          ),
        ],
      ),
    );
  }

  Container _buildInfoDetails() {
    return Container(
      height: 40,
      margin: EdgeInsets.fromLTRB(15, 10, 15, 2),
      decoration: BoxDecoration(
        color: AppColor.greenLightHover,
        borderRadius: BorderRadius.circular(8),
        border: Border.all(color: AppColor.darkGreyLight),
      ),
      alignment: Alignment.center,
      child: Text('پلاک شده : سبک 5 و سنگین 8 راس'),
    );
  }

  Expanded _buildListContent() {
    return Expanded(
      child: ListView.separated(
        padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
        itemCount: 20,
        physics: BouncingScrollPhysics(),
        cacheExtent: 280,
        addRepaintBoundaries: true,
        itemBuilder: (context, index) {
          return _buildItemList(index);
        },
        separatorBuilder: (BuildContext context, int index) => SizedBox(height: 6),
      ),
    );
  }

  Stack _buildItemList(int index) {
    return Stack(
      clipBehavior: Clip.none,
      children: [
        Container(
          width: Get.width,
          height: 75,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8),
            border: Border.all(
              width: 2,
              color:
                  index < 5
                      ? AppColor.yellowNormal
                      : index < 12
                      ? AppColor.greenLightActive
                      : AppColor.blueLight,
            ),
          ),
          child: Row(
            children: [
              SizedBox(width: 30),
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  Get.bottomSheet(_buildInfoBottomSheet(), isScrollControlled: true);
                },
                child: Stack(
                  clipBehavior: Clip.none,
                  children: [
                    Text('123456789012346', style: AppFonts.yekan14.copyWith(color: AppColor.blueNormal)),
                    Positioned(top: -20, right: -10, child: Assets.icons.virtual.svg(width: 20, height: 20)),
                  ],
                ),
              ),
              Spacer(),
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('گوسفند ماده', style: AppFonts.yekan14.copyWith(color: AppColor.blueNormal)),
                  SizedBox(height: 4),
                  Text('نوع نژاد', style: AppFonts.yekan14.copyWith(color: AppColor.bgDark)),
                ],
              ),
              Spacer(),
              Text('18 ماه', style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark)),
              Spacer(),
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 6),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Assets.vec.editSvg.svg(
                      width: 20,
                      height: 20,
                      colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
                    ),
                    Assets.vec.trashSvg.svg(
                      width: 20,
                      height: 20,
                      colorFilter: ColorFilter.mode(AppColor.error, BlendMode.srcIn),
                    ),
                  ],
                ),
              ),
              SizedBox(width: 8),
            ],
          ),
        ),
        Positioned(
          right: -12,
          top: 0,
          bottom: 0,
          child: Container(
            width: 30,
            height: 30,
            child: Stack(
              alignment: Alignment.center,
              children: [
                Assets.vec.tagLabelSvg.svg(width: 30, height: 30),
                Positioned(
                  bottom: 25,
                  right: 2,
                  left: 2,
                  child: Text(
                    (index > 10 ? index * 1000 : index + 1).toString(),
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan10.copyWith(
                      fontSize: ((index > 10 ? index * 1000 : index + 1).toString()).length > 3 ? 6 : 8,
                      color: AppColor.blueDarkActive,
                    ),
                    textScaler: TextScaler.linear(1.5),
                  ),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildOldPage() {
    return Expanded(
      child: Card(
        clipBehavior: Clip.hardEdge,
        color: Colors.white,
        child: GridView.builder(
          padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
          itemCount: 20,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            crossAxisSpacing: 12,
            mainAxisSpacing: 8,
          ),
          itemBuilder: (context, index) {
            return Container(
              decoration: ShapeDecoration(
                color: AppColor.lightGreyLightHover,
                shape: RoundedRectangleBorder(
                  side: BorderSide(width: 1, color: AppColor.blackLightHover),
                  borderRadius: BorderRadius.circular(8),
                ),
              ),
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Assets.vec.editSvg.svg(
                          width: 16,
                          height: 16,
                          colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
                        ),
                        Assets.vec.trashSvg.svg(
                          width: 16,
                          height: 16,
                          colorFilter: ColorFilter.mode(AppColor.error, BlendMode.srcIn),
                        ),
                      ],
                    ),
                    Text(
                      'گوسفند ماده',
                      textAlign: TextAlign.center,
                      style: AppFonts.yekan14.copyWith(color: AppColor.blueNormal),
                    ),
                    SizedBox(height: 10),
                    Text(
                      'سن : 18 ماه',
                      textAlign: TextAlign.center,
                      style: AppFonts.yekan12.copyWith(color: AppColor.darkGreyNormal),
                    ),
                    Text(
                      'نوع نژاد',
                      textAlign: TextAlign.center,
                      style: AppFonts.yekan14.copyWith(color: AppColor.bgDark),
                    ),
                    Text(
                      '1212115112512',
                      textAlign: TextAlign.center,
                      style: AppFonts.yekan14.copyWith(color: AppColor.blueNormal),
                    ),
                    Text(
                      'نوع پلاک',
                      textAlign: TextAlign.center,
                      style: AppFonts.yekan14.copyWith(color: AppColor.bgDark),
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  Widget _buildBottomSheet() {
    return BaseBottomSheet(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        spacing: 8,
        children: [
          _buildLiveStockSpecies(),
          _buildLivestockBirthday(),
          _buildLivestockGender(),

          _buildLivestockBreed(),

          _buildMotherTagNumber(),
          _buildFatherTagNumber(),
          _buildTagNumber(),
          _buildTagType(),

          _buildLiveStockImage(),

          Padding(
            padding: const EdgeInsets.only(top: 8.0),
            child: RElevated(
              text: 'افزودن  دام به گله',
              textStyle: AppFonts.yekan18.copyWith(color: Colors.white),
              width: Get.width,
              backgroundColor: AppColor.greenNormal,
              height: 40,
              onPressed: () {
                Get.back();
              },
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildInfoBottomSheet() {
    return BaseBottomSheet(
      height: Get.height * 0.5,
      bgColor: const Color(0xFFF5F5F5),
      child: Card(
        color: Colors.white,
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.center,
            spacing: 15,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Assets.vec.editSvg.svg(
                    width: 16,
                    height: 16,
                    colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
                  ),
                  Text('محمد احمدی', style: AppFonts.yekan14),

                  GestureDetector(
                    onTap: () {
                      _buildDeleteDialog(onConfirm: () {

                      });
                    },
                    child: Assets.vec.trashSvg.svg(
                      width: 16,
                      height: 16,
                      colorFilter: ColorFilter.mode(AppColor.error, BlendMode.srcIn),
                    ),
                  ),
                ],
              ),
              Container(
                height: 32,
                padding: EdgeInsets.all(8),
                decoration: BoxDecoration(color: AppColor.blueLightHover),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      'تاریخ ثبت',
                      textAlign: TextAlign.center,
                      style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                    ),
                    Text(
                      '1404/2/2',
                      textAlign: TextAlign.center,
                      style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                    ),
                  ],
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    'شماره پلاک',
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                  ),
                  Text(
                    '123456789012346',
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    'جنسیت ',
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                  ),
                  Text(
                    'ماده',
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    'نوع نژاد',
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                  ),
                  Text(
                    'افشاری',
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    'نوع گله',
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                  ),
                  Text(
                    'روستایی',
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    'شهر',
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                  ),
                  Text(
                    'کرج',
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    'سن',
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                  ),
                  Text(
                    '18 ماه',
                    textAlign: TextAlign.center,
                    style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyDark),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _buildDeleteDialog({required VoidCallback onConfirm}) {
    Get.defaultDialog(
      title: 'حذف دام',
      middleText: 'آیا از حذف این دام مطمئن هستید؟',
      confirm: ElevatedButton(
        style: ElevatedButton.styleFrom(backgroundColor: AppColor.error, foregroundColor: Colors.white),
        onPressed: onConfirm,
        child: Text('بله'),
      ),
      cancel: ElevatedButton(
        onPressed: () {
          Get.back();
        },
        child: Text('خیر'),
      ),
    );
  }

  SizedBox _buildLiveStockImage() {
    return SizedBox(
      width: Get.width,
      height: 350,
      child: Card(
        color: Colors.white,
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            children: [
              Expanded(
                child: Container(
                  width: Get.width,
                  decoration: BoxDecoration(color: AppColor.lightGreyNormal, borderRadius: BorderRadius.circular(8)),
                  child: Center(child: Assets.images.placeHolder.image(height: 150, width: 200)),
                ),
              ),
              SizedBox(height: 15),
              Container(
                width: Get.width,
                height: 40,
                clipBehavior: Clip.antiAlias,
                decoration: ShapeDecoration(
                  color: AppColor.blueNormal,
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
                ),
                child: Padding(
                  padding: const EdgeInsets.all(10.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('تصویر دام', style: AppFonts.yekan14.copyWith(color: Colors.white)),
                      Icon(CupertinoIcons.arrow_up_doc, color: Colors.white),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  OverlayDropdownWidget<String> _buildTagType() {
    return OverlayDropdownWidget(
      items: ['نوع پلاک 1', 'نوع پلاک 2', 'نوع پلاک 3'],
      onChanged: (value) {
        print('Selected Breed: $value');
      },
      itemBuilder: (item) => Text(item),
      labelBuilder: (item) => Text(item ?? 'نوع پلاک'),
    );
  }

  TextFiledFixedHint _buildTagNumber() {
    return TextFiledFixedHint(
      inputType: InputType.number,
      hintText: 'پلاک دام',
      onChanged: (String value) {
        eLog('father Tag: $value');
      },
    );
  }

  TextFiledFixedHint _buildFatherTagNumber() {
    return TextFiledFixedHint(
      inputType: InputType.number,
      hintText: 'پلاک پدر ',
      onChanged: (String value) {
        eLog('father Tag: $value');
      },
    );
  }

  TextFiledFixedHint _buildMotherTagNumber() {
    return TextFiledFixedHint(
      inputType: InputType.number,
      hintText: 'پلاک مادر ',
      onChanged: (String value) {
        eLog('Mother Tag: $value');
      },
    );
  }

  OverlayDropdownWidget<String> _buildLivestockBreed() {
    return OverlayDropdownWidget(
      items: ['نوع نژاد 1', 'نوع نژاد 2', 'نوع نژاد 3'],
      onChanged: (value) {
        print('Selected Breed: $value');
      },
      itemBuilder: (item) => Text(item),
      labelBuilder: (item) => Text(item ?? 'نژاد'),
    );
  }

  ObxValue<RxInt> _buildLivestockGender() {
    return ObxValue(
      (data) => CupertinoSlidingSegmentedControl<int>(
        children: {
          0: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 12),
            child: Text(
              'نر',
              style: AppFonts.yekan14.copyWith(
                color: data.value == 0 ? AppColor.whiteGreyLight : AppColor.darkGreyNormalActive,
              ),
            ),
          ),
          1: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 10),
            child: Text(
              'ماده',
              style: AppFonts.yekan14.copyWith(
                color: data.value == 1 ? AppColor.whiteGreyLight : AppColor.darkGreyNormalActive,
              ),
            ),
          ),
        },
        onValueChanged: (int? value) {
          if (value != null) {
            controller.selectedSegment.value = value;
          }
        },

        proportionalWidth: true,
        groupValue: data.value,
        thumbColor: AppColor.blueNormal,
        backgroundColor: CupertinoColors.systemGrey6,
      ),
      controller.selectedSegment,
    );
  }

  Container _buildLivestockBirthday() {
    return Container(
      height: 40,
      width: Get.width,
      padding: const EdgeInsets.symmetric(horizontal: 12),
      decoration: BoxDecoration(
        border: Border.all(color: AppColor.darkGreyLight),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text('تاریخ تولد', style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyNormalActive)),
          Text('1404/5/5', style: AppFonts.yekan14.copyWith(color: AppColor.darkGreyNormalActive)),
        ],
      ),
    );
  }

  OverlayDropdownWidget<String> _buildLiveStockSpecies() {
    return OverlayDropdownWidget(
      items: ['گوسفند ماده', 'گوسفند نر', 'بز ماده', 'بز نر', 'گوساله ماده', 'گوساله نر'],
      onChanged: (value) {
        print('Selected: $value');
      },
      itemBuilder: (item) => Text(item),
      labelBuilder: (item) => Text(item ?? 'گونه دام'),
    );
  }

  ObxValue<RxBool> _buildFilterWidget() {
    return ObxValue((data) {
      return AnimatedContainer(
        duration: Duration(milliseconds: 300),
        padding: EdgeInsets.only(top: 5),
        curve: Curves.easeInOut,
        height: data.value ? 50 : 0,
        child: SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          padding: EdgeInsets.symmetric(horizontal: 12),
          child: ObxValue((data) {
            return Row(
              spacing: 12,
              children: [
                CustomChip(
                  title: 'انتخاب فیلتر',
                  index: 0,
                  isSelected: true,
                  selectedColor: AppColor.blueNormal,
                  onTap: (index) {},
                ),

                RFilterChips(
                  title: 'درخواست‌های من',
                  index: 1,
                  isSelected: data.contains(1),
                  selectedColor: AppColor.yellowNormal,
                  onTap: (index) {
                    if (data.contains(1)) {
                      data.remove(1);
                    } else {
                      data.add(1);
                    }
                  },
                ),

                RFilterChips(
                  title: 'در انتظار ثبت ',
                  index: 2,
                  selectedColor: AppColor.greenLightActive,
                  isSelected: data.contains(2),
                  onTap: (index) {
                    if (data.contains(2)) {
                      data.remove(2);
                    } else {
                      data.add(2);
                    }
                  },
                ),

                RFilterChips(
                  title: 'ارجاع به تعاونی',
                  index: 3,
                  selectedColor: AppColor.blueLightHover,
                  isSelected: data.contains(3),
                  onTap: (index) {
                    if (data.contains(3)) {
                      data.remove(3);
                    } else {
                      data.add(3);
                    }
                  },
                ),
              ],
            );
          }, controller.filterSelected),
        ),
      );
    }, controller.filterIsSelected);
  }

  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.only(right: 8.0, left: 20.0),
            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.search(value);
              },
              controller: TextEditingController(),
            ),
          ),
        ),
      );
    }, controller.searchIsSelected);
  }
}
