import 'package:hmg_patient_app/core/model/contactus/get_hmg_locations.dart';
import 'package:hmg_patient_app/core/model/er/projectavgerwaitingtime.dart';
import 'package:hmg_patient_app/core/service/er/er_service.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/uitl/location_util.dart';
import 'package:hmg_patient_app/widgets/data_display/text.dart';
import 'package:hmg_patient_app/widgets/hospital_location.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../uitl/translations_delegate_base.dart';

class NearestEr extends StatefulWidget {
  static const String _url = "assets/images/";

  int? appointmentNo;
  int? projectID;

  NearestEr({ this.appointmentNo,  this.projectID});

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

class _NearestErState extends State<NearestEr> {
  List<ProjectAvgERWaitingTime> projectAvgERWaitingTimeModelList =[];

  late bool isDataLoaded;
  late LocationUtils locationUtils;

  @override
  void initState() {
    isDataLoaded = false;
    locationUtils = new LocationUtils(isShowConfirmDialog: true, context: context);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      locationUtils.getCurrentLocation();
      getERList();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    final double itemHeight = (size.height - kToolbarHeight - 24) / 2;
    final double itemWidth = size.width / 2;

    return AppScaffold(
      isShowAppBar: true,
      isShowDecPage: false,
      showNewAppBarTitle: true,
      showNewAppBar: true,
      appBarTitle: TranslationBase.of(context).NearestEr,
      body: isDataLoaded != null
          ? isDataLoaded
              ? ListView(
                  padding: EdgeInsets.all(21),
                  physics: BouncingScrollPhysics(),
                  children: <Widget>[
                    Text(
                      TranslationBase.of(context).NearestErDesc,
                      style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.64, height: 23 / 16),
                    ),
                    SizedBox(height: 21),
                    ListView.separated(
                      physics: NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      padding: EdgeInsets.zero,
                      separatorBuilder: (context, index) => SizedBox(height: 14),
                      itemBuilder: (context, index) {
                        GetHMGLocationsModel location = GetHMGLocationsModel();
                        location.locationName = projectAvgERWaitingTimeModelList[index].projectName;
                        location.cityName = projectAvgERWaitingTimeModelList[index].projectName;
                        location.projectImageURL = projectAvgERWaitingTimeModelList[index].projectImageURL;
                        print(projectAvgERWaitingTimeModelList[index].phonenumber);
                        location.phoneNumber = projectAvgERWaitingTimeModelList[index].phonenumber;
                        location.latitude = projectAvgERWaitingTimeModelList[index].latitude;
                        location.longitude = projectAvgERWaitingTimeModelList[index].longitude;
                        location.distanceInKilometers = projectAvgERWaitingTimeModelList[index].distanceInKilometers;
                        return HospitalLocation(location, waitingTime: projectAvgERWaitingTimeModelList[index].avgTimeInHHMM.toString());
                      },
                      itemCount: projectAvgERWaitingTimeModelList.length,
                    ),
                  ],
                )
              : Center(
                  child: Texts('No Data'),
                )
          : SizedBox(),
    );
  }

  getERList() async {
    GifLoaderDialogUtils.showMyDialog(context);
    ErService service = new ErService();
    service.getProjectAvgERWaitingTimeOrders().then((response) {
      if (response != null && response['MessageStatus'] == 1) {
        setState(() {
          projectAvgERWaitingTimeModelList.clear();
          response['List_ProjectAvgERWaitingTime'].forEach((vital) {
            projectAvgERWaitingTimeModelList.add(ProjectAvgERWaitingTime.fromJson(vital));
          });
          isDataLoaded = true;
        });
        GifLoaderDialogUtils.hideDialog(context);
      } else {
        GifLoaderDialogUtils.hideDialog(context);
        AppToast.showErrorToast(message: TranslationBase.of(context).serviceNotAvailable);
        isDataLoaded = false;
      }
    }).catchError((err) {
      print(err);
      GifLoaderDialogUtils.hideDialog(context);
      AppToast.showErrorToast(message: err.toString());
    });
  }
}
