import 'dart:math';

import 'package:diplomaticquarterapp/core/service/medical/vital_sign_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/medical_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/MyAppointments/MyAppointments.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/pages/medical/patient_sick_leave_page.dart';
import 'package:diplomaticquarterapp/pages/medical/reports/monthly_reports.dart';
import 'package:diplomaticquarterapp/pages/vaccine/my_vaccines_screen.dart';
import 'package:diplomaticquarterapp/pages/medical/prescriptions/prescriptions_home_page.dart';
import 'package:diplomaticquarterapp/pages/medical/radiology/radiology_home_page.dart';
import 'package:diplomaticquarterapp/pages/medical/vital_sign/vital_sign_details_screen.dart';
import 'package:diplomaticquarterapp/pages/medical/reports/report_home_page.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/data_display/medical/medical_profile_item.dart';
import 'package:diplomaticquarterapp/widgets/data_display/medical/time_line_widget.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/others/sliver_app_bar_delegate.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:diplomaticquarterapp/pages/insurance/insurance_card_screen.dart';
import 'package:provider/provider.dart';
import '../../locator.dart';
import 'active_medications/ActiveMedicationsPage.dart';
import 'allergies_page.dart';
import 'ask_doctor/ask_doctor_home_page.dart';
import 'balance/my_balance_page.dart';
import 'doctor/doctor_home_page.dart';
import 'package:diplomaticquarterapp/pages/insurance/insurance_update_screen.dart';
import 'package:diplomaticquarterapp/pages/insurance/insurance_approval_screen.dart';
import 'eye/EyeHomePage.dart';
import 'eye/EyeMeasurementsPage.dart';
import 'labs/labs_home_page.dart';
import 'my_trackers/my_trackers.dart';

class MedicalProfilePage extends StatefulWidget {
  @override
  _MedicalProfilePageState createState() => _MedicalProfilePageState();
}

class _MedicalProfilePageState extends State<MedicalProfilePage> {
  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return BaseView<MedicalViewModel>(
      onModelReady: (model) => model.getAppointmentHistory(),
      builder: (_, model, widget) => AppScaffold(
        baseViewModel: model,
        body: Container(
          child: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                Stack(
                  children: <Widget>[
                    Column(
                      children: <Widget>[
                        Container(
                          width: double.infinity,
                          height: 210,
                          decoration: BoxDecoration(
                            image: DecorationImage(
                                image: ExactAssetImage(
                                    'assets/images/timeline_bg.png'),
                                fit: BoxFit.cover),
                          ),
                          child: Stack(
                            children: <Widget>[
                              Image.asset(
                                'assets/images/timeline_bg.png',
                                fit: BoxFit.cover,
                                width: double.infinity,
                              ),
                              ListView.builder(
                                itemBuilder: (context, index) => TimeLineWidget(
                                  isUp: index % 2 == 1,
                                  appoitmentAllHistoryResul: model
                                      .appoitmentAllHistoryResultList[index],
                                ),
                                itemCount:
                                    model.appoitmentAllHistoryResultList.length,
                                scrollDirection: Axis.horizontal,
                                reverse: !projectViewModel.isArabic,
                              ),
                            ],
                          ),
                        ),
                        Padding(
                          padding: EdgeInsets.symmetric(vertical: 5.0),
                          child: Column(
                            children: <Widget>[
                              Container(
                                width: double.infinity,
                                height: 30,
                              ),
                              Row(
                                children: <Widget>[
                                  Expanded(
                                    flex: 1,
                                    child: InkWell(
                                      onTap: () {
                                        Navigator.push(
                                          context,
                                          FadePage(
                                            page: MyAppointments(),
                                          ),
                                        );
                                      },
                                      child: MedicalProfileItem(
                                        title: TranslationBase.of(context)
                                            .myAppointments,
                                        imagePath: 'my_appointment_icon.png',
                                        subTitle: TranslationBase.of(context)
                                            .myAppointmentsList,
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: InkWell(
                                      onTap: () => Navigator.push(context,
                                          FadePage(page: LabsHomePage())),
                                      child: MedicalProfileItem(
                                        title: TranslationBase.of(context).lab,
                                        imagePath: 'lab_result_icon.png',
                                        subTitle:
                                            TranslationBase.of(context).lab,
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: InkWell(
                                      onTap: () => Navigator.push(context,
                                          FadePage(page: RadiologyHomePage())),
                                      child: MedicalProfileItem(
                                        title: TranslationBase.of(context)
                                            .radiology,
                                        imagePath: 'radiology_icon.png',
                                        subTitle: TranslationBase.of(context)
                                            .radiologySubtitle,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              Row(
                                children: <Widget>[
                                  Expanded(
                                    flex: 1,
                                    child: InkWell(
                                      onTap: () {
                                        Navigator.push(
                                          context,
                                          FadePage(
                                            page: HomePrescriptionsPage(),
                                          ),
                                        );
                                      },
                                      child: MedicalProfileItem(
                                        title: TranslationBase.of(context)
                                            .medicines,
                                        imagePath: 'prescription_icon.png',
                                        subTitle: TranslationBase.of(context)
                                            .medicinesSubtitle,
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: InkWell(
                                      onTap: () => Navigator.push(
                                        context,
                                        FadePage(
                                          page: VitalSignDetailsScreen(),
                                        ),
                                      ),
                                      child: MedicalProfileItem(
                                        title: TranslationBase.of(context)
                                            .vitalSigns,
                                        imagePath: 'vital_signs.png',
                                        subTitle: TranslationBase.of(context)
                                            .vitalSignsSubtitle,
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: InkWell(
                                      onTap: () => Navigator.push(context,
                                          FadePage(page: ActiveMedicationsPage())),
                                      child: MedicalProfileItem(
                                        title: TranslationBase.of(context)
                                            .myMedical,
                                        imagePath: 'active_medications.png',
                                        subTitle: TranslationBase.of(context)
                                            .myMedicalSubtitle,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              Row(
                                children: <Widget>[
                                  Expanded(
                                    flex: 1,
                                    child: InkWell(
                                      onTap: () {
                                        Navigator.push(
                                          context,
                                          FadePage(
                                            page: DoctorHomePage(),
                                          ),
                                        );
                                      },
                                      child: MedicalProfileItem(
                                        title: TranslationBase.of(context)
                                            .myDoctor,
                                        imagePath: 'doctor_icon.png',
                                        subTitle: TranslationBase.of(context)
                                            .myDoctorSubtitle,
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: InkWell(
                                      onTap: () {
                                        Navigator.push(
                                            context,
                                            FadePage(
                                                page: EyeMeasurementsPage()));
                                      },
                                      child: MedicalProfileItem(
                                        title: TranslationBase.of(context).eye,
                                        imagePath: 'eye_measurement_icon.png',
                                        subTitle: TranslationBase.of(context)
                                            .eyeSubtitle,
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: InkWell(
                                      onTap: () {
                                        Navigator.push(context,
                                            FadePage(page: InsuranceCard()));
                                      },
                                      child: MedicalProfileItem(
                                        title: TranslationBase.of(context)
                                            .insurance,
                                        imagePath: 'insurance_card_icon.png',
                                        subTitle: TranslationBase.of(context)
                                            .insuranceSubtitle,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              Row(children: <Widget>[
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    onTap: () {
                                      Navigator.push(context,
                                          FadePage(page: InsuranceUpdate()));
                                    },
                                    child: MedicalProfileItem(
                                      title: TranslationBase.of(context)
                                          .updateInsurance,
                                      imagePath: 'insurance_update_icon_.png',
                                      subTitle: TranslationBase.of(context)
                                          .updateInsuranceSubtitle,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    onTap: () {
                                      Navigator.push(context,
                                          FadePage(page: InsuranceApproval()));
                                    },
                                    child: MedicalProfileItem(
                                      title: TranslationBase.of(context)
                                          .insuranceApproval,
                                      imagePath: 'insurance_approvals_icon.png',
                                      subTitle: TranslationBase.of(context)
                                          .insuranceApprovalSubtitle,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    onTap:()=> Navigator.push(context, FadePage(page: AllergiesPage())) ,
                                    child: MedicalProfileItem(
                                      title: TranslationBase.of(context).allergies,
                                      imagePath: 'my_allergies_icon.png',
                                      subTitle: TranslationBase.of(context)
                                          .allergiesSubtitle,
                                    ),
                                  ),
                                ),
                              ]),
                              Row(children: <Widget>[
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    onTap: () {
                                      Navigator.push(context,
                                          FadePage(page: MyVaccines()));
                                    },
                                    child: MedicalProfileItem(
                                      title: TranslationBase.of(context)
                                          .myVaccines,
                                      imagePath: 'my_vaccines_icon.png',
                                      subTitle: TranslationBase.of(context)
                                          .myVaccinesSubtitle,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    onTap: () {
                                      Navigator.push(context,
                                          FadePage(page: HomeReportPage()));
                                    },
                                    child: MedicalProfileItem(
                                      title:
                                          TranslationBase.of(context).medical,
                                      imagePath: 'medical_reports_icon.png',
                                      subTitle: TranslationBase.of(context)
                                          .medicalSubtitle,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    onTap: () {
                                      Navigator.push(context,
                                          FadePage(page: MonthlyReportsPage()));
                                    },
                                    child: MedicalProfileItem(
                                      title:
                                          TranslationBase.of(context).monthly,
                                      imagePath: 'monthly_reports_icon.png',
                                      subTitle: TranslationBase.of(context)
                                          .monthlySubtitle,
                                    ),
                                  ),
                                ),
                              ]),
                              Row(children: <Widget>[
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    onTap: () {
                                      Navigator.push(
                                          context,
                                          FadePage(
                                              page: PatientSickLeavePage()));
                                    },
                                    child: MedicalProfileItem(
                                      title: TranslationBase.of(context).sick,
                                      imagePath: 'sick_leaves_icons.png',
                                      subTitle: TranslationBase.of(context)
                                          .sickSubtitle,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    onTap: () {
                                      Navigator.push(context,
                                          FadePage(page: MyBalancePage()));
                                    },
                                    child: MedicalProfileItem(
                                      title:
                                          TranslationBase.of(context).myBalance,
                                      imagePath: 'check-in.png',
                                      subTitle: TranslationBase.of(context)
                                          .myBalanceSubtitle,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: MedicalProfileItem(
                                    title:
                                        TranslationBase.of(context).patientCall,
                                    imagePath: 'medical_history_icon.png',
                                    subTitle: TranslationBase.of(context)
                                        .patientCallSubtitle,
                                  ),
                                ),
                              ]),
                              Row(children: <Widget>[
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    //TODO
//                            onTap: () {
//                              Navigator.push(
//                                  context, FadePage(page: DoctorHomePage()));
//                            },
                                    child: MedicalProfileItem(
                                      title: TranslationBase.of(context)
                                          .smartWatches,
                                      imagePath: 'smartwatch_icon.png',
                                      subTitle: TranslationBase.of(context)
                                          .smartWatchesSubtitle,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    onTap: () {
                                      Navigator.push(context,
                                          FadePage(page: MyTrackers()));
                                    },
                                    child: MedicalProfileItem(
                                      title: TranslationBase.of(context)
                                          .myTrackers,
                                      imagePath: 'my_tracker_icon.png',
                                      subTitle: TranslationBase.of(context)
                                          .myTrackersSubtitle,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    onTap: (){
                                      Navigator.push(context,
                                          FadePage(page: AskDoctorHomPage()));
                                    },
                                    child: MedicalProfileItem(
                                      title: TranslationBase.of(context).askYour,
                                      imagePath: 'ask_doctor_icon.png',
                                      subTitle: TranslationBase.of(context)
                                          .askYourSubtitle,
                                    ),
                                  ),
                                ),
                              ]),
                              Row(children: <Widget>[
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    //TODO
//                            onTap: () {
//                              Navigator.push(
//                                  context, FadePage(page: DoctorHomePage()));
//                            },
                                    child: MedicalProfileItem(
                                      title:
                                          TranslationBase.of(context).internet,
                                      imagePath: 'insurance_card_icon.png',
                                      subTitle: TranslationBase.of(context)
                                          .internetSubtitle,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: InkWell(
//                            onTap: () {
//                              Navigator.push(
//                                  context, FadePage(page: InsuranceApproval()));
//                            },
                                    child: MedicalProfileItem(
                                      title:
                                          TranslationBase.of(context).chatbot,
                                      imagePath: 'insurance_approvals_icon.png',
                                      subTitle: TranslationBase.of(context)
                                          .chatbotSubtitle,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: Container(),
                                ),
                              ]),
                            ],
                          ),
                        )
                      ],
                    ),
                    if (model.user != null)
                      Positioned(
                        top: 175,
                        left: 20,
                        right: 20,
                        child: Container(
                          width: double.infinity,
                          height: 70,
                          decoration: BoxDecoration(
                              color: Colors.grey[600],
                              shape: BoxShape.rectangle,
                              border: Border.all(
                                  color: Colors.transparent, width: 0.5),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(9)),
                              image: DecorationImage(
                                  image: ExactAssetImage(
                                      'assets/images/bg_graphic.png'),
                                  fit: BoxFit.cover)),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              SizedBox(
                                height: 8,
                              ),
                              Texts(
                                model.user.firstName +
                                    " " +
                                    model.user.lastName,
                                color: Colors.white,
                              ),
                              SizedBox(
                                height: 8,
                              ),
                              Texts(
                                '${model.user.patientID}',
                                color: Colors.white,
                              ),
                            ],
                          ),
                        ),
                      ),
                    Positioned(
                      top: 2,
                      left: projectViewModel.isArabic ? 0 : 10,
                      right: projectViewModel.isArabic ? 10 : 0,
                      child: Texts(
                        TranslationBase.of(context).timeLine,
                        color: Colors.white,
                        fontSize: 14,
                        fontWeight: FontWeight.normal,
                      ),
                    )
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
