import 'package:diplomaticquarterapp/core/model/er/TriageInformationRequest.dart';
import 'package:diplomaticquarterapp/core/model/hospitals/hospitals_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/er/EdOnlineViewModel.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';

class EdOnlineSelectedHospitalPage extends StatefulWidget {
  final Function changePageViewIndex;
  TriageInformationRequest triageInformationRequest;

  EdOnlineSelectedHospitalPage({Key key, this.changePageViewIndex, this.triageInformationRequest}) : super(key: key);

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

class _EdOnlineSelectedHospitalPageState extends State<EdOnlineSelectedHospitalPage> {
  HospitalsModel selectedProject;
  final GlobalKey locationDropdownKey = GlobalKey();
  ProjectViewModel projectViewModel;
  int _selected = 0;

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return BaseView<EdOnlineViewModel>(
      onModelReady: (model) => model.getHospitals(),
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        body: SingleChildScrollView(
          physics: BouncingScrollPhysics(),
          child: Column(
            children: [
              Container(
                margin: EdgeInsets.all(12.0),
                child: Text(TranslationBase.of(context).onlineCheckInAgreement, style: TextStyle(fontSize: 14.0, color: Colors.black, letterSpacing: -0.56)),
              ),
              Container(
                width: double.infinity,
                decoration: containerRadius(Colors.white, 12),
                margin: EdgeInsets.all(12.0),
                padding: EdgeInsets.only(left: 10, right: 10, top: 12, bottom: 12),
                child: Row(
                  children: [
                    Flexible(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            TranslationBase.of(context).selectLocation,
                            style: TextStyle(
                              fontSize: 11,
                              letterSpacing: -0.44,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                          Container(
                            height: 20,
                            child: DropdownButtonHideUnderline(
                              child: DropdownButton<HospitalsModel>(
                                onTap: () {
                                  print("Clicked");
                                },
                                key: locationDropdownKey,
                                hint: new Text(
                                  TranslationBase.of(context).selectHospital,
                                ),
                                value: selectedProject,
                                iconSize: 0,
                                isExpanded: true,
                                style: TextStyle(fontSize: 14, letterSpacing: -0.56, color: Colors.black, fontFamily: projectViewModel.isArabic ? 'Cairo' : 'Poppins'),
                                items: model.hospitals.map((HospitalsModel item) {
                                  return new DropdownMenuItem<HospitalsModel>(
                                    value: item,
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [Text(item.name + " - " + item.distanceInKilometers.toString() + " " + TranslationBase.of(context).km_)],
                                    ),
                                  );
                                }).toList(),
                                onChanged: (newValue) {
                                  setState(() {
                                    selectedProject = newValue;
                                    widget.triageInformationRequest.selectedHospital = selectedProject;
                                  });
                                },
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Icon(Icons.keyboard_arrow_down),
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 10.0),
                child: Row(
                  children: <Widget>[
                    Radio(
                      value: 1,
                      groupValue: _selected,
                      onChanged: onRadioChanged,
                    ),
                    Text(
                      TranslationBase.of(context).agreeTo,
                      style: new TextStyle(
                        fontSize: 12.0,
                        fontWeight: FontWeight.w600,
                        letterSpacing: -0.48,
                        color: CustomColors.textColor,
                      ),
                    ),
                    mWidth(4),
                    InkWell(
                      onTap: () {
                        launch("https://hmg.com/en/Pages/Privacy.aspx");
                      },
                      child: Text(
                        TranslationBase.of(context).termsConditoins,
                        style: new TextStyle(
                          fontSize: 12.0,
                          fontWeight: FontWeight.w600,
                          letterSpacing: -0.48,
                          color: CustomColors.accentColor,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
        bottomSheet: Container(
          color: CustomColors.appBackgroudGreyColor,
          padding: EdgeInsets.all(12.0),
          child: DefaultButton(
            TranslationBase.of(context).next,
            (widget.triageInformationRequest.selectedHospital == null || _selected == 0)
                ? null
                : () {
                    widget.changePageViewIndex(2);
                  },
            color: CustomColors.green,
            disabledColor: CustomColors.grey2,
          ),
        ),
      ),
    );
  }

  void onRadioChanged(int value) {
    setState(() {
      _selected = value;
    });
  }
}
