import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:prosappco/src/authentication/authentication_repository.dart';
import 'package:prosappco/src/components/drawer_menu.dart';
import 'package:prosappco/src/controllers/add_name_email_city.dart';

import '../components/photo_view.dart';

class WelcomeScreen extends StatefulWidget {
  const WelcomeScreen({super.key});

  @override
  State<WelcomeScreen> createState() => _WelcomeScreenState();
}

class _WelcomeScreenState extends State<WelcomeScreen> {
  final FirebaseStorage storage = FirebaseStorage.instance;
  final uid = AuthenticationRepository.instance.getCurrentUserUid();
  final controller = Get.put(NameEmailCityController());
  var _ciudad = '';
  var _photo = '...';
  var _estado = '...';

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

    if (_estado == '...') {
      AuthenticationRepository.instance
          .getState(uid.toString())
          .then((String s) => setState(() {
                _estado = s;
              }));
    }

    if (_ciudad == '') {
      AuthenticationRepository.instance
          .getCity(uid.toString())
          .then((String s) => setState(() {
                _ciudad = s;
              }));
    }

    if (_photo == '...') {
      AuthenticationRepository.instance
          .getPhoto(uid.toString())
          .then((String s) => setState(() {
                _photo = s;
              }));
    }
  }

  @override
  Widget build(BuildContext context) {
    final User? user = FirebaseAuth.instance.currentUser;

    // String name = _nombre.toString();
    String city = _ciudad.toString();
    String state = _estado.toString();

    return SafeArea(
        child: Scaffold(
      drawer: DrawerMenu(
          ref: storage.ref().child(_photo),
          userName: user?.displayName ?? '',
          userPhoneNumber: user?.phoneNumber ?? '',
          userCity: city,
          userState: state),
      resizeToAvoidBottomInset: false,
      backgroundColor: const Color(0xFFD6F4FF),
      body: Stack(
        children: [
          Container(
            margin: const EdgeInsets.only(top: 130),
            width: double.infinity,
            height: 800,
            decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                    topRight: Radius.circular(50),
                    topLeft: Radius.circular(50))),
          ),
          Container(
            margin: const EdgeInsets.only(top: 20, left: 30),
            child: Align(
              alignment: Alignment.topCenter,
              child: Row(
                children: [
                  Builder(
                    builder: (context) => ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.white,
                        shape: const CircleBorder(),
                        elevation: 3,
                        minimumSize: const Size(50, 50),
                      ),
                      child: const Icon(
                        Icons.menu,
                        color: Colors.black,
                        size: 30,
                      ),
                      onPressed: () {
                        Scaffold.of(context).openDrawer();
                      },
                    ),
                  ),
                  const SizedBox(
                    width: 150,
                  ),
                  const Padding(
                    padding: EdgeInsets.only(top: 50),
                    child: Image(
                      image: AssetImage('images/logo_prosapp.png'),
                      width: 120,
                      height: 50,
                    ),
                  ),
                ],
              ),
            ),
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 20),
            margin: const EdgeInsets.only(top: 170),
            child: const Align(
              alignment: Alignment.topCenter,
              child: Text(
                'Bienvenido',
                style: TextStyle(
                  color: Color(0xFF262626),
                  fontSize: 38.0,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 20),
            margin: const EdgeInsets.only(top: 280, left: 50, right: 50),
            child: Column(children: [
              Padding(
                padding: const EdgeInsets.only(bottom: 40),
                child: Center(
                    child: ElevatedButton(
                  onPressed: () {
                    print(ModalRoute.of(context)?.settings.name);
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF2BA4EC),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(50),
                    ),
                    elevation: 0,
                    minimumSize: const Size(260, 60),
                  ),
                  child: const Text(
                    'Solicitar un servicio',
                    style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                      fontSize: 18,
                    ),
                  ),
                )),
              ),
            ]),
          ),
        ],
      ),
    ));
  }
}
