import 'package:flutter/material.dart';
import 'package:test_sa/controllers/providers/api/gas_refill_comments.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/enums/recurrent_task_inspection_data_type.dart';
import 'package:test_sa/models/ppm/recurrent_wo.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';

class RoomInspectionCard extends StatefulWidget {
  final PlanRecurrentMedicalTaskRoomTabs? inspectionModel;

  RoomInspectionCard({super.key, required this.inspectionModel});

  @override
  State<RoomInspectionCard> createState() => _RoomInspectionCardState();
}

class _RoomInspectionCardState extends State<RoomInspectionCard> {
  int selectedIndex = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        widget.inspectionModel!.tabName!.bodyText(context).custom(color:AppColor.textColor(context), fontWeight: FontWeight.w600),
        8.height,
        Container(
          color: context.isDark?null: Colors.white10,
          child: Column(
            children: widget.inspectionModel!.planRecurrentMedicalTaskRoomTabAttributes?.asMap().entries.map<Widget>((entry) {
                  final model = entry.value;
                  final attribute = model.attribute;
                  switch (attribute?.dataTypeEnum) {
                    case RecurrentTaskInspectionDataTypeEnum.bool:
                      return inspectionStatusRadioWidget(index: entry.key, model: model, context: context);
                    case RecurrentTaskInspectionDataTypeEnum.number:
                      return inspectionStatusNumberWidget(index: entry.key, model: model, context: context);
                    default:
                      return const SizedBox.shrink(); // Handles any unexpected cases gracefully
                  }
                }).toList() ??
                [],
          ),
        )
      ],
    ).toShadowContainer(context).paddingOnly(top: 12);
  }

  Widget inspectionStatusRadioWidget({required int index, required PlanRecurrentMedicalTaskRoomTabAttributes model, required BuildContext context}) {
    bool status = (model.attribute == null || model.attributeValue == null) ? true : (model.attribute != null ? (model.attributeValue == 'true' ? true : false) : false);

    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              model.attribute?.name ?? '',
              overflow: TextOverflow.ellipsis,
              maxLines: 1,
              style: AppTextStyles.bodyText2.copyWith(color: context.isDark?Colors.white:AppColor.white936),
            ),
            (status ? 'Pass' : 'Fail').bodyText2(context).custom(color: AppColor.neutral120, fontWeight: FontWeight.w500),
          ],
        ).expanded,
        GestureDetector(
          onTap: () {
            setState(() {
              status = !status;
              model.attributeValue = status.toString();
            });
          },
          child: Container(
            width: 99.toScreenWidth,
            height: 30.toScreenHeight,
            padding: EdgeInsetsDirectional.all(4.toScreenHeight),
            decoration: BoxDecoration(
              color: AppColor.fieldBgColor(context),
              borderRadius: BorderRadius.circular(5),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                buildToggleOption(
                  label: "PASS",
                  isActive: status,
                  activeColor: AppColor.green20,
                  inactiveColor: Colors.transparent,
                  textColor: status ? AppColor.green50 : context.isDark?Colors.white: AppColor.black20,
                ),
                buildToggleOption(
                  label: "FAIL",
                  isActive: !status,
                  activeColor: AppColor.red20,
                  inactiveColor: Colors.transparent,
                  textColor: status ? context.isDark?Colors.white: AppColor.black20 : AppColor.red30,
                ),
              ],
            ),
          ),
        ),
      ],
    ).paddingOnly(top: 6, bottom: 6);
  }

  Widget buildToggleOption({
    required String label,
    required bool isActive,
    required Color activeColor,
    required Color inactiveColor,
    required Color textColor,
  }) {
    return Container(
      width: 44.toScreenWidth,
      height: 22.toScreenHeight,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: isActive ? activeColor : inactiveColor,
        borderRadius: BorderRadius.circular(3),
      ),
      child: label.bodyText2(context).custom(color: textColor),
    );
  }

  // Widget inspectionStatusRadioWidget({
  //   required int index,
  //   required PlanRecurrentMedicalTaskRoomTabAttributes model,
  //   required BuildContext context,
  // }) {
  //
  //   bool status = model.attribute != null
  //       ? model.attributeValue == 'true'
  //           ? true
  //           : false
  //       : false;
  //   String statusString = status ? 'Pass' : 'Fail';
  //   List<String>  state = ['PASS','FAIL'];
  //
  //   return Row(
  //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //     crossAxisAlignment: CrossAxisAlignment.center,
  //     children: [
  //       Column(
  //         crossAxisAlignment: CrossAxisAlignment.start,
  //         children: [
  //           model.attribute!.name!.bodyText2(context).custom(color: AppColor.white936, fontWeight: FontWeight.w500),
  //           statusString.bodyText2(context).custom(color: AppColor.neutral120, fontWeight: FontWeight.w500),
  //         ],
  //       ),
  //
  //       SizedBox(
  //         height: 24.toScreenHeight,
  //         width: 41.toScreenWidth,
  //         child: CupertinoSwitch(
  //           thumbColor: status ? AppColor.green50 : AppColor.red30,
  //           activeColor: AppColor.green30,
  //           trackColor: AppColor.red80,
  //           value: status,
  //           onChanged: (state) {
  //             setState(() {
  //               model.attributeValue = state.toString();
  //               status = state;
  //             });
  //           },
  //         ),
  //       )
  //     ],
  //   ).paddingOnly(bottom: 12);
  // }

  Widget inspectionStatusNumberWidget({required int index, required PlanRecurrentMedicalTaskRoomTabAttributes model, required BuildContext context}) {
    final border = OutlineInputBorder(
      borderRadius: BorderRadius.circular(4), // Optional: Slight rounding
      borderSide: const BorderSide(
        color: AppColor.white70, // Border color
        width: 0, // 1px border
      ),
    );
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        model.attribute!.name!.bodyText2(context).custom(color: context.isDark?Colors.white:AppColor.white936, fontWeight: FontWeight.w500),
        TextFormField(
          keyboardType: TextInputType.number,
          initialValue: model.attributeValue ?? '',
          decoration: InputDecoration(
            contentPadding: EdgeInsets.symmetric(horizontal: 5.toScreenWidth),
            filled: true,
            fillColor: AppColor.fieldBgColor(context),
            constraints: BoxConstraints(
              maxWidth: 99.toScreenWidth,
              maxHeight: 30.toScreenHeight,
            ),
            border: border,
            enabledBorder: border,
            focusedBorder: border,
          ),
          onChanged: (value) {
            model.attributeValue = value;
          },
        ),
      ],
    ).paddingOnly(bottom: 6, top: 6);
  }
}

// // I need it here ....
// Container(
//   padding: EdgeInsets.all(6.toScreenHeight),
//   // height: 57.toScreenHeight,
//   // width: 120.toScreenWidth,
//   decoration: BoxDecoration(
//     color: context.isDark ? AppColor.neutral50 : AppColor.white80,
//     borderRadius: BorderRadius.circular(10),
//   ),
//   child: Row(
//    mainAxisSize: MainAxisSize.min,
//     // mainAxisAlignment: MainAxisAlignment.spaceAround,
//     children: state.asMap().entries.map((task) {
//       final int index = task.key;
//       final String label = task.value;
//       return GestureDetector(
//         onTap: () {
//           setState(() {
//             selectedIndex = index;
//           });
//         },
//         child: Container(
//           // margin: EdgeInsets.only(right: 8.toScreenWidth),
//           height: 49.toScreenHeight,
//           padding: EdgeInsets.symmetric(
//             // vertical: 20.toScreenHeight,
//             horizontal: 15.toScreenWidth,
//           ),
//           alignment: Alignment.center,
//           decoration: BoxDecoration(
//              color: selectedIndex == index ? (context.isDark ? AppColor.neutral60 : status  ? AppColor.green20 : AppColor.red20) : Colors.transparent,
//             // color: status  ? AppColor.green20 : AppColor.red20,
//             borderRadius: BorderRadius.circular(7),
//           ),
//           child: label.bodyText2(context).custom(
//             // color: status?AppColor.green50:AppColor.red30,
//             color: selectedIndex == index ? (context.isDark ? AppColor.neutral60 : status  ? AppColor.green50 : AppColor.red30) : AppColor.black20,
//           ),
//         ),
//       );
//     }).toList(),
//   ),
// ),
// // ToggleSwitch(
// //   minWidth: 41.toScreenWidth,
// //   minHeight: 50.toScreenHeight,
// //   fontSize: 12.0,
// //   initialLabelIndex: 1,
// //   activeBgColor: [Colors.green],
// //   activeFgColor: Colors.white,
// //   inactiveBgColor: Colors.grey,
// //   inactiveFgColor: Colors.grey[900],
// //   totalSwitches: 2,
// //   labels: ['PASS', 'FAIL'],
// //   onToggle: (index) {
// //     print('switched to: $index');
// //   },
// // ),
