// ignore: must_be_immutable
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/model/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_examination.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/expandable-widget-header-body.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/app-textfield-custom.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:provider/provider.dart';

// ignore: must_be_immutable
class AddExaminationWidget extends StatefulWidget {
  MasterKeyModel? item;
  final Function(MySelectedExamination) removeExamination;
  final Function(MySelectedExamination) addExamination;
  final bool Function(MasterKeyModel) isServiceSelected;
  bool isExpand;

  final Function() expandClick;
  final List<MySelectedExamination>? mySelectedExamination;

  AddExaminationWidget({
    this.item,
    required this.removeExamination,
    required this.addExamination,
    required this.isServiceSelected,
    this.isExpand = false,
    required this.expandClick,
    this.mySelectedExamination,
  });

  @override
  _AddExaminationWidgetState createState() => _AddExaminationWidgetState();
}

class _AddExaminationWidgetState extends State<AddExaminationWidget> {
  int status = 1;
  TextEditingController remarksController = TextEditingController();
  MySelectedExamination examination = MySelectedExamination();

  @override
  void initState() {
    if (getSelectedExam(widget.item!) != null) {
      examination = getSelectedExam(widget.item!)!;
      status = examination.isNormal
          ? 1
          : examination.isAbnormal
              ? 2
              : 3;
      remarksController.text = examination.remark!;
    } else {
      examination.selectedExamination = widget.item;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);

    return Container(
      child: HeaderBodyExpandableNotifier(
        headerWidget: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            InkWell(
              onTap: () {
                onExamTap();
              },
              child: Row(
                children: [
                  Checkbox(
                      value: widget.isServiceSelected(widget.item!),
                      activeColor: Colors.red[800],
                      onChanged: (bool? newValue) {
                        onExamTap();
                      }),
                  Container(
                    child: AppText(
                      projectViewModel.isArabic
                          ? widget.item!.nameAr != null && widget.item!.nameAr != ""
                              ? widget.item!.nameAr!
                              : widget.item!.nameEn!
                          : widget.item!.nameEn!,
                      color: Color(0xFF575757),
                      fontSize: SizeConfig.getTextMultiplierBasedOnWidth() * (SizeConfig.isWidthLarge ? 3 : 3.8),
                      letterSpacing: -0.56,
                    ),
                  ),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.symmetric(horizontal: 8),
              child: InkWell(
                onTap: widget.expandClick,
                child: Icon(widget.isExpand ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down),
              ),
            ),
          ],
        ),
        bodyWidget: Container(
          padding: EdgeInsets.symmetric(horizontal: 12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                margin: EdgeInsets.only(bottom: 8),
                child: AppText(
                  TranslationBase.of(context).status,
                  fontWeight: FontWeight.bold,
                  fontFamily: 'Poppins',
                  fontSize: SizeConfig.textMultiplier! * 1.6,
                ),
              ),
              Row(
                children: [
                  Expanded(
                      child: InkWell(
                    onTap: () {
                      setState(() {
                        status = 1;
                      });
                      examination.isNormal = true;
                      examination.isAbnormal = false;
                      examination.notExamined = false;
                    },
                    child: Row(
                      children: [
                        Container(
                          padding: EdgeInsets.all(2.0),
                          margin: EdgeInsets.symmetric(horizontal: 6),
                          width: 20,
                          height: 20,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            shape: BoxShape.circle,
                            border: Border.all(color: Colors.grey, width: 1),
                          ),
                          child: Container(
                            decoration: BoxDecoration(
                              color: status == 1 ? HexColor("#D02127") : Colors.white,
                              shape: BoxShape.circle,
                            ),
                          ),
                        ),
                        AppText(
                          TranslationBase.of(context).normal,
                          fontWeight: FontWeight.normal,
                          fontFamily: 'Poppins',
                          fontSize: SizeConfig.textMultiplier! * 1.6,
                        ),
                      ],
                    ),
                  )),
                  Expanded(
                      child: InkWell(
                    onTap: () {
                      setState(() {
                        status = 2;
                      });
                      examination.isNormal = false;
                      examination.isAbnormal = true;
                      examination.notExamined = false;
                    },
                    child: Row(
                      children: [
                        Container(
                          padding: EdgeInsets.all(2.0),
                          margin: EdgeInsets.symmetric(horizontal: 6),
                          width: 20,
                          height: 20,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            shape: BoxShape.circle,
                            border: Border.all(color: Colors.grey, width: 1),
                          ),
                          child: Container(
                            decoration: BoxDecoration(
                              color: status == 2 ? HexColor("#D02127") : Colors.white,
                              shape: BoxShape.circle,
                            ),
                          ),
                        ),
                        AppText(
                          TranslationBase.of(context).abnormal,
                          fontWeight: FontWeight.normal,
                          fontFamily: 'Poppins',
                          fontSize: SizeConfig.textMultiplier! * 1.6,
                        ),
                      ],
                    ),
                  )),
                  if (!examination.isLocal)
                    Expanded(
                        child: InkWell(
                      onTap: () {
                        setState(() {
                          status = 3;
                        });
                        examination.isNormal = false;
                        examination.isAbnormal = false;
                        examination.notExamined = true;
                      },
                      child: Row(
                        children: [
                          Container(
                            padding: EdgeInsets.all(2.0),
                            margin: EdgeInsets.symmetric(horizontal: 6),
                            width: 20,
                            height: 20,
                            decoration: BoxDecoration(
                              color: Colors.white,
                              shape: BoxShape.circle,
                              border: Border.all(color: Colors.grey, width: 1),
                            ),
                            child: Container(
                              decoration: BoxDecoration(
                                color: status == 3 ? HexColor("#D02127") : Colors.white,
                                shape: BoxShape.circle,
                              ),
                            ),
                          ),
                          Expanded(
                            child: AppText(
                              TranslationBase.of(context).notExamined,
                              fontWeight: FontWeight.normal,
                              fontFamily: 'Poppins',
                              fontSize: SizeConfig.textMultiplier! * 1.6,
                            ),
                          ),
                        ],
                      ),
                    )),
                ],
              ),
              Container(
                margin: EdgeInsets.only(top: 8),
                child: AppTextFieldCustom(
                  hintText: TranslationBase.of(context).remarks,
                  controller: remarksController,
                  minLines: 2,
                  maxLines: 4,
                  inputType: TextInputType.multiline,
                  onChanged: (value) {
                    examination.remark = value;
                  },
                  onClick: () {},
                  onFieldSubmitted: () {},
                ),
              ),
            ],
          ),
        ),
        isExpand: widget.isExpand,
      ),
    );
  }

  onExamTap() {
    setState(() {
      if (widget.isServiceSelected(widget.item!)) {
        if (examination.isLocal) widget.removeExamination(examination);
        widget.expandClick();
      } else {
        examination.isNormal = status == 1;
        examination.isAbnormal = status == 2;
        examination.notExamined = status == 3;
        examination.remark = remarksController.text;
        widget.addExamination(examination);
        widget.expandClick();
      }
    });
  }

  MySelectedExamination? getSelectedExam(MasterKeyModel masterKey) {
    Iterable<MySelectedExamination> exam = widget.mySelectedExamination!.where((element) => masterKey.id == element.selectedExamination!.id && masterKey.typeId == element.selectedExamination!.typeId);
    if (exam.length > 0) {
      return exam.first;
    }
    return null;
  }
}
