import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/models/Authentication/authenticated_user.dart';
import 'package:diplomaticquarterapp/models/Authentication/select_device_imei_res.dart';
import 'package:diplomaticquarterapp/pages/ToDoList/ToDo.dart';
import 'package:diplomaticquarterapp/services/authentication/auth_provider.dart';
import 'package:diplomaticquarterapp/pages/medical/medical_profile_page.dart';
import 'package:diplomaticquarterapp/pages/medical/my_admissions_page.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/bottom_navigation/bottom_nav_bar.dart';
import 'package:diplomaticquarterapp/widgets/drawer/app_drawer_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:permission_handler/permission_handler.dart';
import 'home_page.dart';

class LandingPage extends StatefulWidget {
  @override
  _LandingPageState createState() => _LandingPageState();
}

class _LandingPageState extends State<LandingPage> {
  int currentTab = 0;
  PageController pageController;
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  final authService = new AuthProvider();
  _changeCurrentTab(int tab) {
    setState(() {
      currentTab = tab;
      pageController.jumpToPage(tab);
    });
  }

  @override
  void initState() {
    super.initState();
    pageController = PageController(keepPage: true);
    _firebaseMessaging.getToken().then((String token) {
      sharedPref.setString(PUSH_TOKEN, token);
      if (token != null) {
        checkUserStatus(token);
      }
      requestPermissions();
      //assert(token != null);
    });
  }
  void requestPermissions() async {
    await [
      Permission.location,
      Permission.storage,
      Permission.camera,
      Permission.photos,
      Permission.accessMediaLocation
    ].request();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        textTheme: TextTheme(
          headline6:
              TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
        ),
        title: Text(getText(currentTab).toUpperCase()),
        leading: Builder(
          builder: (BuildContext context) {
            return IconButton(
              icon: Icon(Icons.menu),
              color: Colors.white,
              onPressed: () => Scaffold.of(context).openDrawer(),
            );
          },
        ),
        centerTitle: true,
      ),
      drawer: SafeArea(child: AppDrawer()),
      extendBody: true,
      body: PageView(
        physics: NeverScrollableScrollPhysics(),
        controller: pageController,
        children: [
          HomePage(), MedicalProfilePage(), MyAdmissionsPage(), ToDo()],
      ),
      bottomNavigationBar: BottomNavBar(changeIndex: _changeCurrentTab),
    );
  }

  getText(currentTab) {
    switch (currentTab) {
      case 0:
        return TranslationBase.of(context).home;
      case 1:
        return 'Search Medicine';
      case 2:
        return TranslationBase.of(context).mySchedule;
      case 3:
        return TranslationBase.of(context).services;
      case 4:
        return TranslationBase.of(context).bookAppo;
    }
  }

  void checkUserStatus(token) async {
    authService
        .selectDeviceImei(token)
        .then((SelectDeviceIMEIRES value) => setUserValues(value));
    if (await sharedPref.getObject(USER_PROFILE) != null) {
      var data =
          AuthenticatedUser.fromJson(await sharedPref.getObject(USER_PROFILE));
      if (data != null)
        authService.registeredAuthenticatedUser(data, token, 0, 0);
      // setState(() {
      //   this.user = data;
      //   print(this.user);
      // });
    }
  }

  void setUserValues(value) async {
    sharedPref.setObject(IMEI_USER_DATA, value);
  }
}
