mirrored_repositories/flutter_a_z/lib
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_a_z/controll/RouteGenerator.dart';
import 'package:flutter_a_z/view/AgendaTab.dart';
import 'package:flutter_a_z/view/ChatTab.dart';

class HomeScreen extends StatefulWidget {
  HomeScreen({Key key}) : super(key: key);

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

class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateMixin {
  
  TabController _tabController;
  List<String> itemsMenu = [
    "Settings",
    "Sign Out"
  ];

  //String _emailUser = "";
  String _nameUser = "";

  Future _recoveryDataUser() async {
    FirebaseAuth auth = FirebaseAuth.instance;
    FirebaseUser userLogIn = await auth.currentUser();

    //Firestore db = Firestore.instance;
    var document = Firestore.instance.document('user/'+userLogIn.uid);
    document.get().then((value){
      setState(() {
        _nameUser = value.data["name"];
      });
    });
       

  }
  
  
  @override
  void initState() {    
    super.initState();

    _recoveryDataUser();

    _tabController = TabController(
      length: 2,
      vsync: this
    );
  }

  _optionMenuItem(String option){
    switch (option) {
      case "Settings":
        print("Going to Settings");
        Navigator.pushReplacementNamed(context, RouteGenerator.ROUTE_SETTINGS);
        break;
      case "Sign Out":
        print("Sign Out");
        _signOut();
        break;
    }
  }

  _signOut() async{
    FirebaseAuth auth = FirebaseAuth.instance;
    await auth.signOut();

    Navigator.pushReplacementNamed(context, RouteGenerator.ROUTE_LOGIN);
  }

  
  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
      appBar: AppBar(        
        title: Text("I'm: " + _nameUser),
        bottom: TabBar(
          indicatorWeight: 4,
          labelStyle: TextStyle(
            fontSize: 17,
            fontWeight: FontWeight.bold
          ),
          controller: _tabController,
          indicatorColor: Colors.white,
          tabs: [
            Tab(text: "Chat",),
            Tab(text: "Agenda",)
          ],
        ),
        actions: [
          PopupMenuButton<String>(
            onSelected: _optionMenuItem,
            itemBuilder: (context){
              return itemsMenu.map((String item){
                return PopupMenuItem<String>(
                  value: item,
                  child: Text(item),
                );
              }).toList();
            },
          ),
        ],
      ),
      body: TabBarView(
        controller: _tabController,
        children: [
          ChatTab(),
          AgendaTab()
        ],
      ),
    );
  }
}