// ignore: must_be_immutable
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/models/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/models/SOAP/my_selected_examination.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.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(MasterKeyModel) removeHistory;
  final Function(MySelectedExamination) addHistory;
  final bool Function(MasterKeyModel) isServiceSelected;
  bool isExpand;
  final Function expandClick;

  AddExaminationWidget({
    this.item,
    this.removeHistory,
    this.addHistory,
    this.isServiceSelected,
    this.isExpand,
    this.expandClick,
  });

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

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

  @override
  void initState() {
    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: [
            Expanded(
              child: CheckboxListTile(
                title: AppText(
                  projectViewModel.isArabic
                      ? widget.item.nameAr != null && widget.item.nameAr != ""
                          ? widget.item.nameAr
                          : widget.item.nameEn
                      : widget.item.nameEn,
                  fontWeight: FontWeight.normal,
                  fontFamily: 'Poppins',
                  fontSize: SizeConfig.textMultiplier * 2.0,
                ),
                value: widget.isServiceSelected(widget.item),
                activeColor: HexColor("#D02127"),
                onChanged: (newValue) {
                  setState(() {
                    if (widget.isServiceSelected(widget.item)) {
                      widget.removeHistory(widget.item);
                      widget.expandClick();
                    } else {
                      examination.isNormal = status == 1;
                      examination.isAbnormal = status == 2;
                      examination.notExamined = status == 3;
                      examination.remark = remarksController.text;
                      widget.addHistory(examination);
                      widget.expandClick();
                    }
                  });
                },
                controlAffinity: ListTileControlAffinity.leading,
                contentPadding: EdgeInsets.all(0),
              ),
            ),
            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: Row(
                    children: [
                      InkWell(
                        onTap: () {
                          setState(() {
                            status = 1;
                          });
                          examination.isNormal = true;
                          examination.isAbnormal = false;
                          examination.notExamined = false;
                        },
                        child: 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: Row(
                    children: [
                      InkWell(
                        onTap: () {
                          setState(() {
                            status = 2;
                          });
                          examination.isNormal = false;
                          examination.isAbnormal = true;
                          examination.notExamined = false;
                        },
                        child: 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,
                      ),
                    ],
                  )),
                  Expanded(
                      child: Row(
                    children: [
                      InkWell(
                        onTap: () {
                          setState(() {
                            status = 3;
                          });
                          examination.isNormal = false;
                          examination.isAbnormal = false;
                          examination.notExamined = true;
                        },
                        child: 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;
                  },
                ),
              ),
            ],
          ),
        ),
        isExpand: widget.isExpand,
      ),
    );
  }
}
