import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_inspection/presentation/routes/app_routes.dart';

import 'logic.dart';

class AddSupervisionPage extends GetView<AddSupervisionLogic> {
  const AddSupervisionPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.lightGreyLight,
      appBar: RAppBar(
        title: 'ایجاد بازرسی',
        leading:  Assets.vec.messageAddSvg.svg(
          width: 16,
          height: 16,
          colorFilter: ColorFilter.mode(
            Colors.white,
            BlendMode.srcIn,
          ),
        ),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,

        children: [
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 20,
                      vertical: 10,
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,

                      children: [
                        Text(
                          'نوع پروانه کسب',
                          textAlign: TextAlign.center,
                          style: AppFonts.yekan12.copyWith(
                            color: AppColor.blueNormal,
                          ),
                        ),
                        SizedBox(height: 16),
                        ObxValue((data) {
                          return NewCupertinoSegmentedControl<int>(
                            padding: EdgeInsets.zero,
                            children: controller.segments,
                            groupValue: data.value,
                            selectedColor: AppColor.blueNormal,
                            unselectedColor: Colors.white,
                            borderColor: Colors.grey.shade300,
                            onValueChanged: (int value) {
                              data.value = value;
                            },
                          );
                        }, controller.selectedSegment),
                        SizedBox(height: 16),
                        Text(
                          'تخلف',
                          textAlign: TextAlign.center,
                          style: AppFonts.yekan12.copyWith(
                            color: AppColor.blueNormal,
                          ),
                        ),
                        SizedBox(height: 16),
                        ObxValue((data) {
                          return NewCupertinoSegmentedControl<int>(
                            padding: EdgeInsets.zero,
                            children: controller.violationSegments,
                            groupValue: data.value,
                            selectedColor: AppColor.blueNormal,
                            unselectedColor: Colors.white,
                            borderColor: Colors.grey.shade300,
                            onValueChanged: (int value) {
                              if(value == 0) {
                                controller.routes.ensureContainsAtStart(InspectionRoutes.inspectionRegistrationOfViolation);
                              } else {
                                controller.routes.remove(InspectionRoutes.inspectionRegistrationOfViolation);
                              }
                              data.value = value;
                            },
                          );
                        }, controller.violationSegmentsSelected),
                        SizedBox(height: 8),
                       RTextField(
          controller: TextEditingController(),label: 'صادر کننده پروانه'),
                        SizedBox(height: 8),
                       RTextField(
          controller: TextEditingController(),label: 'شماره مجوز'),
                        SizedBox(height: 8),
                       RTextField(
          controller: TextEditingController(),label: 'شماره ثبت'),
                        SizedBox(height: 8),
                       RTextField(
          controller: TextEditingController(),label: 'کد اقتصادی'),
                      ],
                    ),
                  ),
                  SizedBox(height: 10),
                  optionWidget(
                    selected: controller.selectedTypeOfOwnership,
                    options: controller.tmpData.entries.elementAt(0),
                    onSelected:
                        (index) =>
                    controller.selectedTypeOfOwnership.value = index,
                  ),
                  SizedBox(height: 18),
                  optionWidget(
                    selected: controller.selectedUnitType,
                    options: controller.tmpData.entries.elementAt(1),
                    onSelected:
                        (index) => controller.selectedUnitType.value = index,
                  ),
                  SizedBox(height: 18),
                  optionWidget(
                    selectedList: controller.selectedAccompanyingInspectors,
                    options: controller.tmpData.entries.elementAt(2),
                    onSelected: (data) {
                      final selected = controller.selectedAccompanyingInspectors;
                      final route = InspectionRoutes.inspectionAddMobileInspector;

                      if (data == 0) {
                        selected.resetWith(0);
                        controller.routes.remove(route);
                        return;
                      }

                      controller.routes.ensureContainsAtStart(route);
                      selected.removeIfPresent(0);
                      selected.toggle(data);
                    },
                  ),
                ],
              ),
            ),
          ),

          Padding(
            padding: const EdgeInsets.fromLTRB(20, 0, 20, 25),
            child: RElevated(
              text: 'مرحله بعد',
              onPressed: () {
                  Get.toNamed(controller.routes.first);

              },
              height: 40,
              isFullWidth: true,
              backgroundColor: AppColor.greenNormal,
              textStyle: AppFonts.yekan16.copyWith(color: Colors.white),
            ),
          ),
        ],
      ),
    );
  }

  Column optionWidget({
    RxInt? selected,
    RxList<int>? selectedList,
    required MapEntry<String, List<String>> options,
    required void Function(int index) onSelected,
  }) {
    assert(
    (selected != null) != (selectedList != null),
    'Exactly one of selected or selectedList must be provided',
    );

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 20),
          child: Text(
            options.key,
            textAlign: TextAlign.center,
            style: AppFonts.yekan12.copyWith(color: AppColor.blueNormal),
          ),
        ),

        SizedBox(
          height: 50,
          child: ListView.separated(
            shrinkWrap: true,
            padding: EdgeInsets.symmetric(horizontal: 20),
            scrollDirection: Axis.horizontal,
            itemBuilder:
                (context, index) =>
            selected != null
                ? ObxValue((data) {
              return ChoiceChip(
                onSelected: (_) => onSelected(index),
                color: WidgetStateProperty.resolveWith<Color?>((data,) {
                  if (selected.value == index) {
                    return AppColor.blueNormal;
                  } else {
                    return Colors.white;
                  }
                }),
                labelStyle:
                data.value == index
                    ? AppFonts.yekan13.copyWith(
                  color: AppColor.whiteLight,
                )
                    : AppFonts.yekan12.copyWith(
                  color: AppColor.darkGreyNormalActive,
                ),
                checkmarkColor: Colors.white,
                label: Text(options.value[index]),
                selected: index == data.value,
              );
            }, selected)
                : ObxValue((data) {
              return ChoiceChip(
                onSelected: (value) => onSelected.call(index),
                color: WidgetStateProperty.resolveWith<Color?>((states,) {
                  if (data.contains(index)) {
                    return AppColor.blueNormal;
                  } else {
                    return Colors.white;
                  }
                }),
                labelStyle:
                data.contains(index)
                    ? AppFonts.yekan13.copyWith(
                  color: AppColor.whiteLight,
                )
                    : AppFonts.yekan12.copyWith(
                  color: AppColor.darkGreyNormalActive,
                ),
                checkmarkColor: Colors.white,
                label: Text(options.value[index]),
                selected: data.contains(index),
              );
            }, selectedList!),

            separatorBuilder: (context, index) => SizedBox(width: 8),
            itemCount: options.value.length,
          ),
        ),
      ],
    );
  }
}
