import 'package:doctor_app_flutter/core/service/NavigationService.dart';
import 'package:doctor_app_flutter/core/viewModel/authentication_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/locator.dart';
import 'package:doctor_app_flutter/routes.dart';
import 'package:doctor_app_flutter/screens/doctor_schedule/doctor_schedule.dart';
import 'package:doctor_app_flutter/screens/reschedule_leaves/add_reschedule_leave.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../utils/dr_app_shared_pref.dart';
import '../../widgets/shared/drawer_item_widget.dart';
import 'app_texts_widget.dart';

DrAppSharedPreferances sharedPref = DrAppSharedPreferances();

class AppDrawer extends StatefulWidget {
  @override
  _AppDrawerState createState() => _AppDrawerState();
}

class _AppDrawerState extends State<AppDrawer> {
  Utils helpers = Utils();
  late ProjectViewModel projectsProvider;

  @override
  Widget build(BuildContext context) {
    AuthenticationViewModel authenticationViewModel = Provider.of(context);
    projectsProvider = Provider.of(context);
    return Container(
      color: Colors.white,
      child: Drawer(
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 7,
              child: ListView(padding: EdgeInsets.zero, children: <Widget>[
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 15),
                  // height: SizeConfig.heightMultiplier! * 50,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          Container(
                            child: Image.asset(
                              'assets/images/dr_app_logo.png',
                              width: MediaQuery.of(context).size.width * 0.16,
                              height: MediaQuery.of(context).size.height * 0.16,
                            ),
                            margin: EdgeInsets.only(top: 10, bottom: 10),
                          ),
                          Container(
                            child: InkWell(
                              onTap: () {
                                Navigator.pop(context);
                              },
                              child: Icon(
                                DoctorApp.close_1,
                                size: 20,
                                color: Color(0xff2B353E),
                              ),
                            ),
                            margin: EdgeInsets.only(top: 20, bottom: 10),
                          )
                        ],
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      ),
                      SizedBox(height: 5),
                      if (authenticationViewModel.doctorProfile != null)
                        InkWell(
                          onTap: () {
                            // TODO: return it back when its needed
                            // Navigator.of(context).pushNamed(PROFILE, arguments: {
                            //   'title': authProvider.doctorProfile.doctorName,
                            //   "doctorProfileall": authProvider.doctorProfile
                            // });
                          },
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Padding(
                                padding: EdgeInsets.only(top: 8.0),
                                child: AppText(
                                  TranslationBase.of(context).dr + capitalizeOnlyFirstLater(authenticationViewModel.doctorProfile!.doctorName!.replaceAll("DR.", "").toLowerCase()),
                                  fontWeight: FontWeight.w700,
                                  color: Color(0xFF2E303A),
                                  fontFamily: 'Poppins',
                                  fontSize: 25.0,
                                  letterSpacing: -1.5,
                                ),
                              ),
                              Padding(
                                  padding: EdgeInsets.only(top: 0),
                                  child: AppText(
                                    authenticationViewModel.doctorProfile!.clinicDescription!,
                                    fontWeight: FontWeight.w500,
                                    color: Color(0xFF2E303A),
                                    fontSize: 16,
                                    fontFamily: 'Poppins',
                                    letterSpacing: -0.96,
                                    //textAlign: TextAlign.left,
                                  ))
                            ],
                          ),
                        ),
                      SizedBox(height: 40),
                      InkWell(
                        child: DrawerItem(
                          TranslationBase.of(context).applyOrRescheduleLeave,
                          icon: DoctorApp.reschedule__1,

                          // subTitle: ,
                        ),
                        onTap: () {
                          Navigator.pop(context);
                          Navigator.push(
                              context,
                              MaterialPageRoute(builder: (context) => AddRescheduleLeaveScreen(), settings: RouteSettings(name: 'AddRescheduleLeaveScreen')
                                  // MyReferredPatient(),
                                  ));
                        },
                      ),
                      SizedBox(height: 15),
                      InkWell(
                        child: DrawerItem(
                          TranslationBase.of(context).doctorRota,
                          icon: Icons.punch_clock,

                          // subTitle: ,
                        ),
                        onTap: () {
                          Navigator.pop(context);
                          Navigator.push(
                              context,
                              MaterialPageRoute(builder: (context) => DoctorSchedulePage(), settings: RouteSettings(name: 'doctorSchedule')
                                  // MyReferredPatient(),
                                  ));
                        },
                      ),
                      SizedBox(height: 15),
                      InkWell(
                        child: DrawerItem(
                          TranslationBase.of(context).myQRCode,
                          icon: DoctorApp.qr_code_3,
                          // subTitle: ,
                        ),
                      ),
                      SizedBox(height: MediaQuery.of(context).size.height * 0.02),
                      InkWell(
                        child: Container(
                          height: MediaQuery.of(context).size.height * 0.16,
                          width: MediaQuery.of(context).size.width * 0.16,
                          child: Image.asset('assets/images/qr_code.png'),
                        ),
                        onTap: () {},
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  height: MediaQuery.of(context).size.height * 0.02,
                ),
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 20),
                  child: Column(
                    children: [
                      InkWell(
                        child: DrawerItem(
                          projectsProvider.isArabic ? TranslationBase.of(context).lanEnglish : TranslationBase.of(context).lanArabic,
                          // icon: DoctorApp.qr_code,
                          assetLink: projectsProvider.isArabic ? 'assets/images/usa-flag.png' : 'assets/images/saudi-arabia-flag.png',
                        ),
                        onTap: () {
                          if (projectsProvider.isArabic)
                            projectsProvider.changeLanguage('en');
                          else
                            projectsProvider.changeLanguage('ar');
                        },
                      ),
                      SizedBox(height: 10),
                      InkWell(
                        child: DrawerItem(
                          TranslationBase.of(context).logout,
                          icon: DoctorApp.logout_1,
                        ),
                        onTap: () async {
                          Navigator.pop(context);
                          await authenticationViewModel.logout(isFromLogin: false);
                          locator<NavigationService>().pushNamedAndRemoveUntil(ROOT);
                        },
                      ),
                    ],
                  ),
                ),
              ]),
            ),
            Expanded(
              flex: 1,
              child: Column(
                children: <Widget>[
                  Container(
                    // This align moves the children to the bottom
                    child: Align(
                      alignment: FractionalOffset.bottomCenter,
                      child: Container(
                        child: Padding(
                          padding: EdgeInsets.only(left: projectsProvider.isArabic ? 0 : 15.0, right: projectsProvider.isArabic ? 15.0 : 0),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: <Widget>[
                              Container(
                                width: MediaQuery.of(context).size.width * 0.5,
                                child: RichText(
                                  text: TextSpan(
                                    text: 'Powered by',
                                    style: TextStyle(
                                      color: Color(0xFF989898),
                                      fontWeight: FontWeight.w600,
                                      fontSize: 14,
                                      fontFamily: 'Poppins',
                                      letterSpacing: -0.56,
                                    ),
                                    children: <TextSpan>[
                                      TextSpan(
                                        text: ' Cloud Solutions',
                                        style: TextStyle(color: Color(0xFF2E303A), fontSize: 14, fontFamily: 'Poppins', letterSpacing: -0.56, fontWeight: FontWeight.w700),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                              // Text("Powered by"),
                              Image.asset('assets/images/cs_logo_container.png', width: MediaQuery.of(context).size.width * 0.13)
                            ],
                          ),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  drawerNavigator(context, routeName) {
    Navigator.of(context).pushNamed(routeName);
  }
}

String capitalizeOnlyFirstLater(String text) {
  if (text.trim().isEmpty) return "";

  return "${text[0].toUpperCase()}${text.substring(1)}";
}
