import 'dart:ui';

import 'package:eva_icons_flutter/eva_icons_flutter.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:mask_text_input_formatter/mask_text_input_formatter.dart';

import '../app/constant.dart';
import '../model/registration_model.dart';

class TestsTableWidget extends HookConsumerWidget {
  const TestsTableWidget({
    super.key,
    required this.specimenLoading,
    required this.testScrollCtr,
    required this.tempRegistrationData,
    required this.addOrderLoading,
    required this.deletePaket,
    required this.delete,
    required this.pickDateTime,
  });

  final ValueNotifier<bool> specimenLoading;
  final ScrollController testScrollCtr;
  final RegistrationModel tempRegistrationData;
  final bool addOrderLoading;
  final Function(String id, String idtab) delete;
  final Function(String id) deletePaket;
  final Function(String idtab, String idtest, String dt) pickDateTime;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    var maskFormatter = MaskTextInputFormatter(
        initialText:
            DateFormat('dd-MM-yyyy HH:mm').format(DateTime.now()).toString(),
        mask: '##-##-#### ##:##',
        filter: {"#": RegExp(r'[0-9]')},
        type: MaskAutoCompletionType.eager);
    bool isDate(String str) {
      if (DateTime.tryParse(str) != null) {
        return true;
      } else {
        return false;
      }
      // try {
      //   DateTime.tryParse(str);
      //   return true;
      // } catch (e) {
      //   return false;
      // }
    }

    formatDate(String date) {
      String formated = "";
      if (isDate(date)) {
        formated = DateFormat('dd-MM-yyyy HH:mm')
            .format(DateTime.parse(date))
            .toString();
      } else {
        formated = date;
      }
      return formated;
    }

    String? validateDate(String? value) {
      if (value == null || value.isEmpty) {
        return "tidak boleh ksosng";
      }
      final splitted = value.split(" ");
      if (splitted.length == 2) {
        final date = splitted[0];
        final time = splitted[1];
        final splittedDate = date.split("-");
        final splittedTime = time.split(":");
        if (splittedDate.length == 3) {
          final day = int.tryParse(splittedDate[0]);
          final month = int.tryParse(splittedDate[1]);
          final year = int.tryParse(splittedDate[2]);
          if (day != null) {
            if (day > 31) {
              return "Format salah";
            }
          }
          if (month != null) {
            if (month > 12) {
              return "Format salah";
            }
          }
          // if (year == null) {
          //   return "Format salah";
          // }
        } else {
          return "Format salah";
        }
        if (splittedTime.length == 2) {
          var hour = int.tryParse(splittedTime[0]);
          var sec = int.tryParse(splittedTime[1]);
          if (hour != null) {
            if (hour > 24) {
              return "Format salah";
            }
          }
          if (sec != null) {
            if (sec > 60) {
              return "Format salah";
            }
          }
        } else {
          return "Format salah";
        }
      } else {
        return "Format salah";
      }
      return null;
    }

    return Column(
      children: [
        Row(
          children: [
            Container(
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.symmetric(
                  horizontal: Constant.getActualX(context: context, x: 20)),
              width: Constant.getActualX(context: context, x: 1160) * 0.9,
              height: Constant.getActualY(context: context, y: 64),
              color: Constant.grey_200,
              child: Text('Pemeriksaan',
                  style: Constant.body3_600(context: context)
                      .copyWith(color: Constant.textGrey)),
            ),
            // Container(
            //   alignment: Alignment.centerLeft,
            //   padding: EdgeInsets.symmetric(
            //       horizontal: Constant.getActualX(context: context, x: 20)),
            //   width: Constant.getActualX(context: context, x: 1160) * 0.4,
            //   height: Constant.getActualY(context: context, y: 64),
            //   color: Constant.grey_200,
            //   child: Text('Waktu Pengambilan Sample',
            //       style: Constant.body3_600(context: context)
            //           .copyWith(color: Constant.textGrey)),
            // ),
            Container(
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.symmetric(
                  horizontal: Constant.getActualX(context: context, x: 20)),
              width: Constant.getActualX(context: context, x: 1160) * 0.1,
              height: Constant.getActualY(context: context, y: 64),
              color: Constant.grey_200,
              child: Text('Aksi',
                  style: Constant.body3_600(context: context)
                      .copyWith(color: Constant.textGrey)),
            ),
          ],
        ),
        SizedBox(
          height: Constant.getActualY(context: context, y: 390),
          // color: Colors.red,
          child: specimenLoading.value
              ? Center(
                  child: LoadingAnimationWidget.discreteCircle(
                      color: Constant.primaryBlue, size: 40),
                )
              : Scrollbar(
                  thumbVisibility: true,
                  trackVisibility: true,
                  scrollbarOrientation: ScrollbarOrientation.right,
                  controller: testScrollCtr,
                  child: ScrollConfiguration(
                    behavior: ScrollBehavior().copyWith(dragDevices: {
                      PointerDeviceKind.mouse,
                      PointerDeviceKind.touch,
                      PointerDeviceKind.trackpad,
                      PointerDeviceKind.stylus,
                      PointerDeviceKind.unknown
                    }),
                    child: ListView(
                      shrinkWrap: true,
                      addAutomaticKeepAlives: true,
                      controller: testScrollCtr,
                      // semanticChildCount:
                      //     tempRegistrationData.tests?.length,
                      children: [
                        Column(
                          children: tempRegistrationData.paket!.map((e) {
                            var index = tempRegistrationData.paket?.indexOf(e);
                            return Column(
                              children: [
                                Row(
                                  children: [
                                    Container(
                                      margin: EdgeInsets.only(
                                          top: Constant.getActualY(
                                              context: context, y: 16),
                                          bottom: Constant.getActualY(
                                              context: context, y: 16)),
                                      alignment: Alignment.centerLeft,
                                      padding: EdgeInsets.symmetric(
                                          horizontal: Constant.getActualX(
                                              context: context, x: 20)),
                                      width: Constant.getActualX(
                                              context: context, x: 1160) *
                                          0.9,
                                      constraints: BoxConstraints(
                                          minHeight: Constant.getActualY(
                                              context: context, y: 64)),
                                      // height: Constant.getActualY(
                                      //     context: context, y: 64),
                                      color: Colors.white,
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Text(e.name ?? '',
                                              style: Constant.body3_400(
                                                      context: context)
                                                  .copyWith(
                                                      color:
                                                          Constant.textBlack)),
                                          SizedBox(
                                            height: 4,
                                          ),
                                          Text(e.detail ?? '',
                                              style: Constant.caption2_400(
                                                      context: context)
                                                  .copyWith(
                                                      color:
                                                          Constant.textGrey)),
                                        ],
                                      ),
                                    ),
                                    Container(
                                      margin: EdgeInsets.only(
                                          top: Constant.getActualY(
                                              context: context, y: 16),
                                          bottom: Constant.getActualY(
                                              context: context, y: 16)),

                                      alignment: Alignment.centerLeft,
                                      padding: EdgeInsets.symmetric(
                                          horizontal: Constant.getActualX(
                                              context: context, x: 20)),
                                      // color: Colors.red,
                                      width: Constant.getActualX(
                                              context: context, x: 1160) *
                                          0.1,
                                      child: IconButton(
                                          onPressed: !addOrderLoading
                                              ? () {
                                                  // delete(e.id!, e.tab!);
                                                  deletePaket(e.id!);
                                                }
                                              : null,
                                          icon: Icon(EvaIcons.trash2,
                                              size: 24,
                                              color: Constant.textGrey)),
                                    )
                                  ],
                                ),
                                Divider()
                              ],
                            );
                          }).toList(),
                        ),
                        Column(
                          children: tempRegistrationData.tests!.map((e) {
                            var index = tempRegistrationData.tests?.indexOf(e);
                            return Column(
                              children: [
                                Row(
                                  children: [
                                    Container(
                                      margin: EdgeInsets.only(
                                          top: Constant.getActualY(
                                              context: context, y: 16),
                                          bottom: Constant.getActualY(
                                              context: context, y: 16)),
                                      alignment: Alignment.centerLeft,
                                      padding: EdgeInsets.symmetric(
                                          horizontal: Constant.getActualX(
                                              context: context, x: 20)),
                                      width: Constant.getActualX(
                                              context: context, x: 1160) *
                                          0.9,
                                      constraints: BoxConstraints(
                                          minHeight: Constant.getActualY(
                                              context: context, y: 64)),
                                      // height: Constant.getActualY(
                                      //     context: context, y: 64),
                                      color: Colors.white,
                                      child: Text(e.name ?? '',
                                          style: Constant.body3_400(
                                                  context: context)
                                              .copyWith(
                                                  color: Constant.textBlack)),
                                    ),
                                    // Container(
                                    //   margin: EdgeInsets.only(
                                    //       bottom: Constant.getActualY(
                                    //           context: context, y: 20)),
                                    //   alignment: Alignment.centerLeft,
                                    //   padding: EdgeInsets.symmetric(
                                    //       horizontal: Constant.getActualX(
                                    //           context: context, x: 20),
                                    //       vertical: 10),
                                    //   width: Constant.getActualX(
                                    //           context: context, x: 1160) *
                                    //       0.4,
                                    //   color: Colors.white,
                                    //   child: SizedBox(
                                    //     width: Constant.getActualX(
                                    //         context: context, x: 250),
                                    //     child: CustomTextField(
                                    //       suffixIcon: Icon(EvaIcons.calendar),
                                    //       initialValue: formatDate(e.date!),
                                    //       onChange: (value) {
                                    //         pickDateTime(
                                    //           e.tab!,
                                    //           e.id!,
                                    //           value,
                                    //         );
                                    //       },
                                    //       validator: validateDate,
                                    //       inputFormater: [
                                    //         MaskTextInputFormatter(
                                    //             initialText: e.date,
                                    //             mask: '##-##-#### ##:##',
                                    //             filter: {"#": RegExp(r'[0-9]')},
                                    //             type: MaskAutoCompletionType.eager)
                                    //       ],

                                    //       // controller:
                                    //       //     TextEditingController(text: e.date),
                                    //       // controller: searchCtr,
                                    //       isPassword: false,
                                    //       hintText: "dd-mm-yyyy hh:mm",
                                    //       labelText: "dd-mm-yyyy hh:mm",
                                    //     ),
                                    //   ),
                                    // ),
                                    Container(
                                      margin: EdgeInsets.only(
                                          bottom: Constant.getActualY(
                                              context: context, y: 20)),

                                      alignment: Alignment.centerLeft,
                                      padding: EdgeInsets.symmetric(
                                          horizontal: Constant.getActualX(
                                              context: context, x: 20)),
                                      // color: Colors.red,
                                      width: Constant.getActualX(
                                              context: context, x: 1160) *
                                          0.1,
                                      child: IconButton(
                                          onPressed: !addOrderLoading
                                              ? () {
                                                  delete(e.id!, e.tab!);
                                                }
                                              : null,
                                          icon: Icon(EvaIcons.trash2,
                                              size: 24,
                                              color: Constant.textGrey)),
                                    )
                                  ],
                                ),
                                Divider()
                              ],
                            );
                          }).toList(),
                        ),
                        // Container(
                        //     padding: EdgeInsets.symmetric(
                        //         horizontal: Constant.getActualX(
                        //             context: context, x: 20)),
                        //     child: Divider()),
                        // Row(
                        //   children: [
                        //     Container(
                        //       padding: EdgeInsets.symmetric(
                        //           horizontal: Constant.getActualX(
                        //               context: context, x: 20),
                        //           vertical: Constant.getActualY(
                        //               context: context, y: 16)),
                        //       width: Constant.getActualX(
                        //               context: context, x: 1160) *
                        //           0.34,
                        //       child: Text(
                        //         "TOTAL",
                        //         style: Constant.body3_500(context: context),
                        //       ),
                        //     ),
                        //     Container(
                        //       alignment: Alignment.centerRight,
                        //       padding: EdgeInsets.symmetric(
                        //           horizontal: Constant.getActualX(
                        //               context: context, x: 20),
                        //           vertical: Constant.getActualY(
                        //               context: context, y: 16)),
                        //       width: Constant.getActualX(
                        //               context: context, x: 1160) *
                        //           0.13,
                        //       child: Text(
                        //         Constant.convertToIdr(
                        //             double.parse(
                        //                 tempRegistrationData.total ?? "0"),
                        //             0),
                        //         style: Constant.body3_500(context: context),
                        //       ),
                        //     )
                        //   ],
                        // )
                      ],
                    ),
                  ),
                ),
        ),
      ],
    );
  }
}
