import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/enum/master_lookup_key.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_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/screens/base/base_view.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/buttons/app_buttons_widget.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';

import 'examinations_list_search_widget.dart';

class AddExaminationPage extends StatefulWidget {
  final List<MySelectedExamination> mySelectedExamination;
  final Function addSelectedExamination;
  final Function(MasterKeyModel) removeExamination;

  AddExaminationPage(
      {this.mySelectedExamination,
      this.addSelectedExamination,
      this.removeExamination});

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

class _AddExaminationPageState extends State<AddExaminationPage> {
  @override
  Widget build(BuildContext context) {
    return BaseView<SOAPViewModel>(
      onModelReady: (model) async {
        if (model.physicalExaminationList.length == 0) {
          await model.getMasterLookup(MasterKeysService.PhysicalExamination);
        }
      },
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        isShowAppBar: false,
        backgroundColor: Color.fromRGBO(248, 248, 248, 1),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Container(
              padding:
                  EdgeInsets.only(left: 16, top: 70, right: 16, bottom: 16),
              color: Colors.white,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                    child: AppText(
                      "${TranslationBase.of(context).addExamination}",
                      fontSize: SizeConfig.textMultiplier * 3.3,
                      color: Colors.black,
                      fontWeight: FontWeight.w700,
                    ),
                  ),
                  InkWell(
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                    child: Icon(
                      Icons.clear,
                      size: 40,
                    ),
                  )
                ],
              ),
            ),
            Expanded(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Container(
                      margin: EdgeInsets.all(16.0),
                      padding: EdgeInsets.all(0.0),
                      decoration: BoxDecoration(
                        shape: BoxShape.rectangle,
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(12),
                        border: Border.fromBorderSide(BorderSide(
                          color: Colors.grey.shade400,
                          width: 0.4,
                        )),
                      ),
                      child: Column(
                        children: [
                          ExaminationsListSearchWidget(
                            masterList: model.physicalExaminationList,
                            isServiceSelected: (master) =>
                                isServiceSelected(master),
                            removeHistory: (history) {
                              setState(() {
                                widget.removeExamination(history);
                              });
                            },
                            addHistory: (selectedExamination) {
                              setState(() {
                                widget.mySelectedExamination
                                    .add(selectedExamination);
                              });
                            },
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Container(
              padding: EdgeInsets.all(16),
              color: Colors.white,
              child: AppButton(
                title:"${TranslationBase.of(context).addExamination}",
                color: HexColor("#359846"),
                fontColor: Colors.white,
                vPadding: 12,
                radius: 12,
                fontWeight: FontWeight.w600,
                fontSize: SizeConfig.textMultiplier * 2.5,
                onPressed: () {
                  widget.addSelectedExamination();
                },
              ),
            )
          ],
        ),
      ),
    );
  }

  isServiceSelected(MasterKeyModel masterKey) {
    Iterable<MySelectedExamination> exam = widget.mySelectedExamination.where(
        (element) =>
            masterKey.id == element.selectedExamination.id &&
            masterKey.typeId == element.selectedExamination.typeId);
    if (exam.length > 0) {
      return true;
    }
    return false;
  }
}
