import 'package:charts_flutter/flutter.dart' as charts;
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/enum/patient_type.dart';
import 'package:doctor_app_flutter/core/model/PatientSearchRequestModel.dart';
import 'package:doctor_app_flutter/core/viewModel/auth_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/dashboard_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/hospital_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/icons_app/doctor_app_icons.dart';
import 'package:doctor_app_flutter/models/dashboard/dashboard_model.dart';
import 'package:doctor_app_flutter/models/doctor/doctor_profile_model.dart';
import 'package:doctor_app_flutter/models/doctor/profile_req_Model.dart';
import 'package:doctor_app_flutter/models/patient/patient_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/medicine/medicine_search_screen.dart';
import 'package:doctor_app_flutter/screens/patients/DischargedPatientPage.dart';
import 'package:doctor_app_flutter/screens/patients/PatientsInPatientScreen.dart';
import 'package:doctor_app_flutter/screens/patients/ReferralDischargedPatientPage.dart';
import 'package:doctor_app_flutter/screens/patients/patient_search/patient_search_screen_new.dart';
import 'package:doctor_app_flutter/screens/patients/patient_search/patients_screen_new.dart';
import 'package:doctor_app_flutter/screens/patients/patient_search_screen.dart';
import 'package:doctor_app_flutter/screens/patients/profile/referral/patient_referral_screen.dart';
import 'package:doctor_app_flutter/util/date-utils.dart';
import 'package:doctor_app_flutter/util/dr_app_shared_pref.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/dashboard/activity_button.dart';
import 'package:doctor_app_flutter/widgets/dashboard/guage_chart.dart';
import 'package:doctor_app_flutter/widgets/dashboard/out_patient_stack.dart';
import 'package:doctor_app_flutter/widgets/dashboard/row_count.dart';
import 'package:doctor_app_flutter/widgets/dashboard/swiper_rounded_pagination.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/profile-welcome-widget.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/transitions/fade_page.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:provider/provider.dart';
import 'package:sticky_headers/sticky_headers/widget.dart';

import '../../widgets/shared/app_texts_widget.dart';
import '../../widgets/shared/rounded_container_widget.dart';
import 'home_page_card.dart';

DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();
Helpers helpers = Helpers();

class HomeScreen extends StatefulWidget {
  HomeScreen({Key key, this.title}) : super(key: key);

  final String title;
  final String iconURL = 'assets/images/dashboard_icon/';

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

class _HomeScreenState extends State<HomeScreen> {
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  HospitalViewModel hospitalProvider;
  AuthViewModel authProvider;
  bool isLoading = false;
  ProjectViewModel projectsProvider;
  var _isInit = true;
  DoctorProfileModel profile;
  bool isExpanded = false;
  bool isInpatient = false;
  var clinicName = [];
  int sliderActiveIndex = 0;
  var clinicId;
  var _patientSearchFormValues;

  void didChangeDependencies() async {
    super.didChangeDependencies();
    if (_isInit) {
      projectsProvider = Provider.of<ProjectViewModel>(context);
      projectsProvider.getDoctorClinicsList();

      // _firebaseMessaging.setAutoInitEnabled(true);
      _firebaseMessaging.requestNotificationPermissions(
          const IosNotificationSettings(
              sound: true, badge: true, alert: true, provisional: true));
      _firebaseMessaging.onIosSettingsRegistered
          .listen((IosNotificationSettings settings) {
        print("Settings registered: $settings");
      });
      clinicName = await sharedPref.getObj(CLINIC_NAME);
      print(clinicName);
      _firebaseMessaging.getToken().then((String token) async {
        if (token != '') {
          DEVICE_TOKEN = token;
          var request = await sharedPref.getObj(DOCTOR_PROFILE);
          authProvider.insertDeviceImei(request).then((value) {
            // print(value);
            changeIsLoading(false);
          });
        }
      });
    }
    _isInit = false;
  }

  BuildContext myContext;
  @override
  Widget build(BuildContext context) {
    myContext = context;
    hospitalProvider = Provider.of(context);
    authProvider = Provider.of(context);
    projectsProvider = Provider.of(context);
    FocusScopeNode currentFocus = FocusScope.of(context);

    if (!currentFocus.hasPrimaryFocus) {
      currentFocus.unfocus();
    }

    return BaseView<DashboardViewModel>(
      onModelReady: (model) => model.getDashboard(),
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        isShowAppBar: false,
        body: ListView(children: [
          Column(children: <Widget>[
            StickyHeader(
              header: Container(
                  color: Colors.grey[100],
                  padding: EdgeInsets.only(top: 10),
                  child: Stack(children: [
                    IconButton(
                      icon: Image.asset('assets/images/menu.png',
                          height: 50, width: 50),
                      iconSize: 18,
                      color: Colors.black,
                      onPressed: () => Scaffold.of(context).openDrawer(),
                    ),
                    Column(children: <Widget>[
                      ProfileWelcomeWidget(
                        Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            Container(
                              width: MediaQuery.of(context).size.width * .6,
                              // // height: 100,
                              child: projectsProvider.doctorClinicsList.length >
                                      0
                                  ? Stack(
                                      children: [
                                        DropdownButtonHideUnderline(
                                            child: DropdownButton(
                                          dropdownColor: Colors.white,
                                          iconEnabledColor: Colors.black,
                                          isExpanded: true,
                                          value: clinicId == null
                                              ? projectsProvider
                                                  .doctorClinicsList[0].clinicID
                                              : clinicId,
                                          iconSize: 25,
                                          elevation: 16,
                                          selectedItemBuilder:
                                              (BuildContext context) {
                                            return projectsProvider
                                                .doctorClinicsList
                                                .map((item) {
                                              return Row(
                                                mainAxisSize: MainAxisSize.max,
                                                mainAxisAlignment:
                                                    MainAxisAlignment.end,
                                                children: <Widget>[
                                                  Container(
                                                    padding: EdgeInsets.all(2),
                                                    margin: EdgeInsets.all(2),
                                                    decoration:
                                                        new BoxDecoration(
                                                      color: Colors.red[800],
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              20),
                                                    ),
                                                    constraints: BoxConstraints(
                                                      minWidth: 20,
                                                      minHeight: 20,
                                                    ),
                                                    child: new Text(
                                                      projectsProvider
                                                          .doctorClinicsList
                                                          .length
                                                          .toString(),
                                                      style: new TextStyle(
                                                        color: Colors.white,
                                                        fontSize:
                                                            projectsProvider
                                                                    .isArabic
                                                                ? 10
                                                                : 11,
                                                      ),
                                                      textAlign:
                                                          TextAlign.center,
                                                    ),
                                                  ),
                                                  AppText(item.clinicName,
                                                      fontSize: 12,
                                                      color: Colors.black,
                                                      fontWeight:
                                                          FontWeight.bold,
                                                      textAlign: TextAlign.end),
                                                ],
                                              );
                                            }).toList();
                                          },
                                          onChanged: (newValue) {
                                            clinicId = newValue;
                                            changeClinic(
                                                newValue, context, model);
                                          },
                                          items: projectsProvider
                                              .doctorClinicsList
                                              .map((item) {
                                            return DropdownMenuItem(
                                              child: AppText(
                                                item.clinicName,
                                                textAlign: TextAlign.right,
                                              ),
                                              value: item.clinicID,
                                            );
                                          }).toList(),
                                        )),
                                      ],
                                    )
                                  : AppText(
                                      TranslationBase.of(context).noClinic),
                            ),
                          ],
                        ),
                        isClilic: true,
                        height: 50,
                      ),
                    ])
                  ])),
              content: Column(
                children: [
                  Container(
                      height: MediaQuery.of(context).size.height * 0.35,
                      child: model.dashboardItemsList.length > 0
                          ? new Swiper(
                              onIndexChanged: (index) {
                                if (mounted) {
                                  setState(() {
                                    sliderActiveIndex = index;
                                  });
                                }
                              },
                              itemBuilder: (BuildContext context, int index) {
                                return getSwipeWidget(model)[index];
                              },
                              itemCount: 3,
                              itemHeight: 300,
                              pagination: new SwiperCustomPagination(builder:
                                  (BuildContext context,
                                      SwiperPluginConfig config) {
                                return new Stack(
                                    alignment: Alignment.bottomCenter,
                                    children: [
                                      Positioned(
                                          bottom: 0,
                                          child: Center(
                                              child: InkWell(
                                                  onTap: () {},
                                                  child: Container(
                                                      child: Row(
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .spaceBetween,
                                                    children: [
                                                      config.activeIndex == 0
                                                          ? SwiperRoundedPagination(
                                                              true)
                                                          : SwiperRoundedPagination(
                                                              false),
                                                      config.activeIndex == 1
                                                          ? SwiperRoundedPagination(
                                                              true)
                                                          : SwiperRoundedPagination(
                                                              false),
                                                      config.activeIndex == 2
                                                          ? SwiperRoundedPagination(
                                                              true)
                                                          : SwiperRoundedPagination(
                                                              false),
                                                    ],
                                                  )))))
                                    ]);
                              }),
                              viewportFraction: 0.9,
                              // scale: 0.9,
                              // control: new SwiperControl(),
                            )
                          : SizedBox()),
                  model.dashboardItemsList.length > 0
                      ? FractionallySizedBox(
                          widthFactor: 0.90,
                          child: Container(
                              child: Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: <Widget>[
                                SizedBox(
                                  height: 10,
                                ),
                                sliderActiveIndex == 1
                                    ? Column(
                                        children: [
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.start,
                                            children: <Widget>[
                                              AppText(
                                                model.dashboardItemsList[3]
                                                    .kPIName,
                                                fontSize:
                                                    SizeConfig.textMultiplier *
                                                        2.2,
                                                fontWeight: FontWeight.bold,
                                              ),
                                            ],
                                          ),
                                          new Container(
                                              height: 130,
                                              child: new ListView(
                                                  scrollDirection:
                                                      Axis.horizontal,
                                                  children: new List.generate(
                                                      model
                                                          .dashboardItemsList[3]
                                                          .summaryoptions
                                                          .length, (int index) {
                                                    return GetActivityButton(model
                                                        .dashboardItemsList[3]
                                                        .summaryoptions[index]);
                                                  })))
                                        ],
                                      )
                                    : sliderActiveIndex == 0
                                        ? Column(
                                            children: [
                                              Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment.start,
                                                children: <Widget>[
                                                  AppText(
                                                    model.dashboardItemsList[6]
                                                        .kPIName,
                                                    fontSize: SizeConfig
                                                            .textMultiplier *
                                                        2.2,
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ],
                                              ),
                                              new Container(
                                                  height: 130,
                                                  child: new ListView(
                                                      scrollDirection: Axis
                                                          .horizontal,
                                                      children: new List
                                                              .generate(
                                                          model
                                                              .dashboardItemsList[
                                                                  6]
                                                              .summaryoptions
                                                              .length,
                                                          (int index) {
                                                        return GetActivityButton(model
                                                            .dashboardItemsList[
                                                                6]
                                                            .summaryoptions[index]);
                                                      })))
                                            ],
                                          )
                                        : Column(
                                            children: [
                                              Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment.start,
                                                children: <Widget>[
                                                  AppText(
                                                    model.dashboardItemsList[4]
                                                        .kPIName,
                                                    fontSize: SizeConfig
                                                            .textMultiplier *
                                                        2.2,
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ],
                                              ),
                                              new Container(
                                                  height: 130,
                                                  child: new ListView(
                                                      scrollDirection: Axis
                                                          .horizontal,
                                                      children: new List
                                                              .generate(
                                                          model
                                                              .dashboardItemsList[
                                                                  4]
                                                              .summaryoptions
                                                              .length,
                                                          (int index) {
                                                        return GetActivityButton(model
                                                            .dashboardItemsList[
                                                                4]
                                                            .summaryoptions[index]);
                                                      })))
                                            ],
                                          ),
                              ])))
                      : SizedBox(),
                  FractionallySizedBox(
                    // widthFactor: 0.90,
                    child: Container(
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.only(
                            topRight: Radius.circular(50),
                          )),
                      padding: EdgeInsets.only(left: 20, top: 10, right: 20),
                      margin: EdgeInsets.only(top: 10),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          SizedBox(
                            height: 10,
                          ),
                          Row(
                            children: <Widget>[
                              Container(
                                  width: 150,
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                        TranslationBase.of(context).patients,
                                        style: TextStyle(
                                            fontSize: 12,
                                            height: .5,
                                            fontWeight: FontWeight.bold,
                                            fontFamily: 'Poppins'),
                                      ),
                                      Text(
                                        TranslationBase.of(context).services,
                                        style: TextStyle(
                                            fontSize: 22,
                                            fontWeight: FontWeight.bold,
                                            fontFamily: 'Poppins'),
                                      ),
                                    ],
                                  )),
                            ],
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          new Container(
                              height: 130,
                              child: new ListView(
                                  scrollDirection: Axis.horizontal,
                                  children: [
                                    HomePageCard(
                                      color: Colors.red[800],
                                      margin: EdgeInsets.all(5),
                                      child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Padding(
                                              padding: EdgeInsets.only(
                                                  top: 10, left: 10, right: 0),
                                              child: Icon(
                                                DoctorApp.inpatient,
                                                size: 32,
                                                color: Colors.white,
                                              )),
                                          Container(
                                              padding: EdgeInsets.all(10),
                                              child: AppText(
                                                TranslationBase.of(context)
                                                    .inPatient,
                                                color: Colors.white,
                                                textAlign: TextAlign.start,
                                                fontSize: 15,
                                              ))
                                        ],
                                      ),
                                      hasBorder: false,
                                      onTap: () {
                                        Navigator.push(
                                          context,
                                          FadePage(
                                            page: PatientInPatientScreen(),
                                          ),
                                        );
                                        // getRequestHeader(true);
                                        // Navigator.of(context)
                                        //     .pushNamed(PATIENTS, arguments: {
                                        //   "patientSearchForm":
                                        //       _patientSearchFormValues,
                                        //   "selectedType": "1",
                                        //   "arrivalType": "1",
                                        //   "isInpatient": true
                                        // });
                                      },
                                    ),
                                    HomePageCard(
                                      color: Colors.grey[300],
                                      margin: EdgeInsets.all(5),
                                      child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Padding(
                                              padding: EdgeInsets.only(
                                                  top: 10, left: 10, right: 0),
                                              child: Icon(
                                                DoctorApp.arrival_patients,
                                                size: 35,
                                                color: Colors.black,
                                              )),
                                          Container(
                                              padding: EdgeInsets.all(10),
                                              child: AppText(
                                                TranslationBase.of(context)
                                                    .myOutPatient,
                                                color: Colors.black,
                                                textAlign: TextAlign.start,
                                                fontSize: 15,
                                              ))
                                        ],
                                      ),
                                      hasBorder: false,
                                      onTap: () {

                                        String date = DateUtils.convertDateToFormat(
                                            DateTime(DateTime.now().year, DateTime.now().month,
                                                DateTime.now().day ),
                                            'yyyy-MM-dd');

                                        Navigator.push(
                                            context,
                                            MaterialPageRoute(
                                              builder: (context) =>
                                                  PatientsScreenNew(
                                                isSearch: false,
                                                isInpatient: false,
                                                patientSearchRequestModel:
                                                    PatientSearchRequestModel(from: date, to: date),
                                                selectedPatientType:
                                                    PatientType.OutPatient,
                                                    isSearchWithKeyInfo: false,
                                              ),
                                            ));
                                      },
                                    ),
                                    HomePageCard(
                                      color: Colors.black,
                                      margin: EdgeInsets.all(5),
                                      child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Padding(
                                              padding: EdgeInsets.only(
                                                  top: 10, left: 10, right: 0),
                                              child: Icon(
                                                DoctorApp.referral_1,
                                                size: 35,
                                                color: Colors.white,
                                              )),
                                          Container(
                                              padding: EdgeInsets.all(10),
                                              child: AppText(
                                                TranslationBase.of(context)
                                                    .patientsreferral,
                                                color: Colors.white,
                                                textAlign: TextAlign.start,
                                                fontSize: 15,
                                              ))
                                        ],
                                      ),
                                      hasBorder: false,
                                      onTap: () {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder: (context) =>
                                                PatientReferralScreen(),
                                            // MyReferredPatient(),
                                          ),
                                        );
                                      },
                                    ),
                                    HomePageCard(
                                      color: Colors.red[800],
                                      margin: EdgeInsets.all(5),
                                      child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Padding(
                                              padding: EdgeInsets.only(
                                                  top: 10, left: 10, right: 0),
                                              child: Icon(
                                                DoctorApp.discharge_patients,
                                                size: 35,
                                                color: Colors.white,
                                              )),
                                          Container(
                                              padding: EdgeInsets.all(10),
                                              child: AppText(
                                                'Discharged Patients',
                                                color: Colors.white,
                                                textAlign: TextAlign.start,
                                                fontSize: 15,
                                              ))
                                        ],
                                      ),
                                      hasBorder: false,
                                      onTap: () {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder: (context) =>
                                                DischargedPatient(),
                                            // MyReferredPatient(),
                                          ),
                                        );
                                      },
                                    ),
                                    HomePageCard(
                                      color: Colors.grey[300],
                                      margin: EdgeInsets.all(5),
                                      child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Padding(
                                              padding: EdgeInsets.only(
                                                  top: 10, left: 10, right: 0),
                                              child: Icon(
                                                DoctorApp.referral_discharge,
                                                size: 35,
                                                color: Colors.black,
                                              )),
                                          Container(
                                              padding: EdgeInsets.all(10),
                                              child: AppText(
                                                'Referral Discharged',
                                                color: Colors.black,
                                                textAlign: TextAlign.start,
                                                fontSize: 15,
                                              ))
                                        ],
                                      ),
                                      hasBorder: false,
                                      onTap: () {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder: (context) =>
                                                ReferralDischargedPatientPage(),
                                            // MyReferredPatient(),
                                          ),
                                        );
                                      },
                                    ),
                                    HomePageCard(
                                      color: Colors.black,
                                      margin: EdgeInsets.all(5),
                                      child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Padding(
                                              padding: EdgeInsets.only(
                                                  top: 10, left: 10, right: 0),
                                              child: Icon(
                                                DoctorApp.search,
                                                size: 32,
                                                color: Colors.white,
                                              )),
                                          Container(
                                              padding: EdgeInsets.all(10),
                                              child: AppText(
                                                TranslationBase.of(context)
                                                    .searchPatient,
                                                color: Colors.white,
                                                textAlign: TextAlign.start,
                                                fontSize: 13,
                                              ))
                                        ],
                                      ),
                                      hasBorder: false,
                                      onTap: () {
                                        Navigator.push(
                                            context,
                                            MaterialPageRoute(
                                              builder: (context) =>
                                                  PatientSearchScreenNew(),
                                            ));
                                      },
                                    ),
                                    HomePageCard(
                                      color: Colors.red[800],
                                      margin: EdgeInsets.all(5),
                                      child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Padding(
                                              padding: EdgeInsets.only(
                                                  top: 10, left: 10, right: 0),
                                              child: Icon(
                                                DoctorApp.search_medicines,
                                                size: 32,
                                                color: Colors.white,
                                              )),
                                          Container(
                                              padding: EdgeInsets.all(10),
                                              child: AppText(
                                                TranslationBase.of(context)
                                                    .searchMedicine,
                                                color: Colors.white,
                                                textAlign: TextAlign.start,
                                                fontSize: 13,
                                              ))
                                        ],
                                      ),
                                      hasBorder: false,
                                      onTap: () {
                                        Navigator.push(
                                            context,
                                            MaterialPageRoute(
                                              builder: (context) =>
                                                  MedicineSearchScreen(),
                                            ));
                                      },
                                    )
                                  ])),
                          Row(
                            // mainAxisAlignment: MainAxisAlignment.spaceAround,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              SizedBox(
                                width: 8,
                              ),
                            ],
                          ),
                          SizedBox(
                            height: 20,
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            )
          ]),
        ]),
      ),
    );
  }

  static List<charts.Series<GaugeSegment, String>> _createReferralData(model) {
    final data = [
      new GaugeSegment(
          model.dashboardItemsList[2].summaryoptions[0].kPIParameter,
          getValue(model.dashboardItemsList[1].summaryoptions[0].value),
          charts.MaterialPalette.black),
      new GaugeSegment(
          model.dashboardItemsList[2].summaryoptions[1].kPIParameter,
          getValue(model.dashboardItemsList[1].summaryoptions[1].value),
          charts.MaterialPalette.gray.shadeDefault),
      new GaugeSegment(
          model.dashboardItemsList[2].summaryoptions[2].kPIParameter,
          getValue(model.dashboardItemsList[1].summaryoptions[2].value),
          charts.MaterialPalette.red.shadeDefault),
    ];

    return [
      new charts.Series<GaugeSegment, String>(
        id: 'Segments',
        domainFn: (GaugeSegment segment, _) => segment.segment,
        measureFn: (GaugeSegment segment, _) => segment.size,
        data: data,
        colorFn: (GaugeSegment segment, _) => segment.color,
      )
    ];
  }

  static int getValue(value) {
    return value == 0 ? 1 : value;
  }

  changeClinic(clinicId, BuildContext context, model) async {
    // Navigator.pop(context);
    changeIsLoading(true);
    Map profile = await sharedPref.getObj(DOCTOR_PROFILE);
    DoctorProfileModel doctorProfile = new DoctorProfileModel.fromJson(profile);
    ProfileReqModel docInfo = new ProfileReqModel(
        doctorID: doctorProfile.doctorID,
        clinicID: clinicId,
        license: true,
        projectID: doctorProfile.projectID,
        tokenID: '',
        languageID: 2);
    authProvider.getDocProfiles(docInfo.toJson()).then((res) async {
      changeIsLoading(false);
      sharedPref.setObj(DOCTOR_PROFILE, res['DoctorProfileList'][0]);
    }).catchError((err) {
      changeIsLoading(false);
      Helpers.showErrorToast(err);
    });
  }

  changeIsLoading(bool val) {
    setState(() {
      this.isLoading = val;
    });
  }

  getPatientCount(DashboardModel inPatientCount) {
    int value = 0;
    inPatientCount.summaryoptions.forEach((result) => {value += result.value});

    return value.toString();
  }

  List<Widget> getSwipeWidget(model) {
    return [
      RoundedContainer(
          height: MediaQuery.of(context).size.height * 0.35,
          margin: EdgeInsets.only(top: 15, bottom: 15, left: 10, right: 10),
          child: Padding(
              padding: const EdgeInsets.all(5.0),
              child: GetOutPatientStack(model.dashboardItemsList[1]))),
      RoundedContainer(
          height: MediaQuery.of(context).size.height * 0.35,
          margin: EdgeInsets.only(top: 15, bottom: 15, left: 10, right: 10),
          child: Padding(
              padding: const EdgeInsets.all(5.0),
              child: GetOutPatientStack(model.dashboardItemsList[0]))),
      RoundedContainer(
          height: MediaQuery.of(context).size.height * 0.35,
          margin: EdgeInsets.only(top: 15, bottom: 15, left: 10, right: 10),
          child:
              Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            Expanded(
                flex: 1,
                child: Row(
                  children: [
                    Expanded(
                        flex: 4,
                        child: Padding(
                            padding: const EdgeInsets.all(5.0),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Expanded(
                                    flex: 1,
                                    child: Padding(
                                        padding: EdgeInsets.all(8),
                                        child: Column(
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              TranslationBase.of(context)
                                                  .patients,
                                              style: TextStyle(
                                                  fontSize: 12,
                                                  height: .5,
                                                  fontWeight: FontWeight.bold,
                                                  fontFamily: 'Poppins'),
                                            ),
                                            Text(
                                              TranslationBase.of(context)
                                                  .referral,
                                              style: TextStyle(
                                                  fontSize: 22,
                                                  fontWeight: FontWeight.bold,
                                                  fontFamily: 'Poppins'),
                                            ),
                                          ],
                                        ))),
                                Expanded(
                                  flex: 2,
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      RowCounts(
                                          model.dashboardItemsList[2]
                                              .summaryoptions[0].kPIParameter,
                                          model.dashboardItemsList[2]
                                              .summaryoptions[0].value,
                                          Colors.black),
                                      RowCounts(
                                          model.dashboardItemsList[2]
                                              .summaryoptions[1].kPIParameter,
                                          model.dashboardItemsList[2]
                                              .summaryoptions[1].value,
                                          Colors.grey),
                                      RowCounts(
                                          model.dashboardItemsList[2]
                                              .summaryoptions[2].kPIParameter,
                                          model.dashboardItemsList[2]
                                              .summaryoptions[2].value,
                                          Colors.red),
                                    ],
                                  ),
                                )
                              ],
                            ))),
                    Expanded(
                      flex: 3,
                      child: Stack(children: [
                        Container(
                            child: GaugeChart(_createReferralData(model))),
                        Positioned(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                AppText(
                                  getPatientCount(model.dashboardItemsList[2])
                                      .toString(),
                                  fontSize: 30,
                                  fontWeight: FontWeight.bold,
                                )
                              ],
                            ),
                            top: MediaQuery.of(context).size.height * 0.12,
                            left: 0,
                            right: 0)
                      ]),
                    ),
                  ],
                )),
          ])),
    ];
  }

  getRequestHeader(isInpatient) {
    _patientSearchFormValues = PatientModel(
        FirstName: "0",
        MiddleName: "0",
        LastName: "0",
        PatientMobileNumber: "0",
        PatientIdentificationID: "0",
        PatientID: 0,
        From: isInpatient == true
            ? '0'
            : DateUtils.convertDateToFormat(DateTime.now(), 'yyyy-MM-dd')
                .toString(),
        To: isInpatient == true
            ? '0'
            : DateUtils.convertDateToFormat(DateTime.now(), 'yyyy-MM-dd')
                .toString(),
        LanguageID: 2,
        stamp: "2020-03-02T13:56:39.170Z",
        IPAdress: "11.11.11.11",
        VersionID: 1.2,
        Channel: 9,
        TokenID: "2Fi7HoIHB0eDyekVa6tCJg==",
        SessionID: "5G0yXn0Jnq",
        IsLoginForDoctorApp: true,
        PatientOutSA: false);
  }
}
