import 'dart:async';

import 'package:doctor_app_flutter/config/config.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../lookups/auth_lookup.dart';
import '../../util/dr_app_shared_pref.dart';
import '../../widgets/auth/auth_header.dart';
import '../../widgets/auth/known_user_login.dart';
import '../../widgets/auth/login_form.dart';
import '../../widgets/shared/app_scaffold_widget.dart';
import '../../widgets/shared/dr_app_circular_progress_Indeicator.dart';

DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();

class Loginsreen extends StatefulWidget {
  @override
  _LoginsreenState createState() => _LoginsreenState();
}

class _LoginsreenState extends State<Loginsreen> {
  Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

  String platformImei;
  Future<String> platformImeiFuture;

  bool _isLoading = false;

  Future<void> getSharedPref() async {
    sharedPref.getString('platformImei').then((imei) {
      platformImei = imei;
    });
  }

  @override
  void initState() {
    super.initState();
    platformImeiFuture = _prefs.then((SharedPreferences prefs) {
      return (prefs.getString('platformImei'));
    });
  }

/*
 *@author: Elham Rababah
 *@Date:19/4/2020
 *@param: isLoading
 *@return:
 *@desc:   Change Isloading attribute in order to show or hide loader
 */
  void changeLoadingStata(isLoading) {
    setState(() {
      _isLoading = isLoading;
    });
  }

  @override
  Widget build(BuildContext context) {
    getSharedPref();
    return AppScaffold(
      isLoading: _isLoading,
      isShowAppBar: false,
      body: SafeArea(
        child: ListView(children: <Widget>[
          FutureBuilder(
              future: platformImeiFuture,
              builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
                switch (snapshot.connectionState) {
                  case ConnectionState.waiting:
                    return DrAppCircularProgressIndeicator();
                  default:
                    if (snapshot.hasError) {
                      return Text('Error: ${snapshot.error}');
                    } else {
                      return Container(
                          margin: EdgeInsetsDirectional.fromSTEB(30, 0, 30, 30),
                          alignment: Alignment.topLeft,
                          child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                (platformImei == null)
                                    ? Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          AuthHeader(loginType.knownUser),
                                          LoginForm(
                                            changeLoadingStata:
                                                changeLoadingStata,
                                          ),
                                        ],
                                      )
                                    : Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          AuthHeader(loginType.unknownUser),
                                          KnownUserLogin(),
                                        ],
                                      ),
                              ]));
                    }
                }
              }),
        ]),
      ),
    );
  }
}
