import 'dart:io';
import 'package:flutter/material.dart';
import 'package:quickquiz/main.dart';
import 'package:quickquiz/pages/home.dart';
import 'package:quickquiz/profile/profile_one_page.dart';
import 'package:quickquiz/global.dart';
import 'package:quickquiz/ui/widgets/CustomShowDialog.dart';

class CommonDrawer extends StatefulWidget {
  CommonDrawer(
      {Key? key,
      required this.name,
      required this.email,
      required this.nameInitial})
      : super(key: key);
  final String name;
  final String email;
  final String nameInitial;
  @override
  CommonDrawerState createState() => CommonDrawerState();
}

class CommonDrawerState extends State<CommonDrawer> {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        children: <Widget>[
          if (widget.name != '' ||
              widget.email != '' ||
              widget.nameInitial != '')
            UserAccountsDrawerHeader(
              accountName:
                  widget.name != '' ? Text(widget.name) : SizedBox.shrink(),
              accountEmail:
                  widget.email != '' ? Text(widget.email) : SizedBox.shrink(),
              currentAccountPicture: widget.nameInitial != ''
                  ? CircleAvatar(
                      backgroundColor: Colors.green,
                      child: Text(widget.nameInitial),
                    )
                  : SizedBox.shrink(),
              decoration: BoxDecoration(color: Color.fromRGBO(58, 66, 86, 1.0)),
              margin: EdgeInsets.all(0.0),
            ),
          ListTile(
            title: Text("Home"),
            trailing: Icon(Icons.home),
            onTap: () {
              Navigator.pop(context);
              var router = MaterialPageRoute(
                  builder: (BuildContext context) => QuizHome(
                        name: widget.name,
                        email: widget.email,
                        nameInitial: widget.nameInitial,
                      ));
              Navigator.of(context).push(router);
            },
          ),
          Divider(
            height: 5.0,
          ),
          ListTile(
            title: Text("My Profile"),
            trailing: Icon(Icons.person),
            onTap: () {
              var router = MaterialPageRoute(
                  builder: (BuildContext context) => ProfileOnePage(
                        name: widget.name,
                        email: widget.email,
                        nameInitial: widget.nameInitial,
                      ));
              Navigator.of(context).push(router);
            },
          ),
          ListTile(
            title: Text("Sign Out"),
            trailing: Icon(Icons.settings_power),
            onTap: () {
              deleteFile();
              var router =
                  MaterialPageRoute(builder: (BuildContext context) => Home());
              Navigator.of(context).push(router);
            },
          ),
          ListTile(
            title: Text("Quit"),
            trailing: Icon(Icons.close),
            onTap: () {
              showDialog(
                context: context,
                builder: (context) => CustomAlertDialog(
                  title: Text(
                    'Are you sure?',
                    textAlign: TextAlign.center,
                  ),
                  content: Container(
                    width: 260.0,
                    height: 30.0,
                    decoration: BoxDecoration(
                      shape: BoxShape.rectangle,
                      color: const Color(0xFFFFFF),
                      borderRadius: BorderRadius.all(Radius.circular(30.0)),
                    ),
                    child: Column(
                      children: <Widget>[
                        Text('Do you want to exit'),
                      ],
                    ),
                  ),
                  actions: <Widget>[
                    TextButton(
                      onPressed: () => Navigator.of(context).pop(false),
                      child: Text('No'),
                    ),
                    TextButton(
                      onPressed: () => exit(0),
                      child: Text('Yes'),
                    ),
                  ],
                  titlePadding: EdgeInsets.symmetric(),
                  semanticLabel: '',
                ),
              );
            },
          ),
        ],
      ),
    );
  }

  void deleteFile() {
    print("Deleting file!");
    File file = File(dir.path + "/" + fileName);
    file.delete();
    setState(() {
      fileExists = false;
    });
  }
}
