import 'package:flutter/material.dart';
import 'package:mobile_portal_23/classes/common_classes.dart';
//import 'package:shared_preferences/shared_preferences.dart';

//late EmployeeArguments empArgs;
String? userApiKey; // = "XST8X8F-6Q9M1FG-PE9948X-SPFHVX9";
bool _error = false;
late EmployeePageArguments employeePageArguments;

class reportNav extends StatefulWidget {
  const reportNav({Key? key}) : super(key: key);

  @override
  State<reportNav> createState() => _reportNavState();
}

/*
Future<http.Response> fetchData(String userApiKey) async {
  var url = "http://" + appDomain + "/apiemployee/" + userApiKey;
  http.Response response = await http.get(Uri.parse(url));
  return response;

  //return resp.map<EmployeeLs>((m) => EmployeeLs.fromJson(m)).toList();
}
*/
Future<void> _showMyDialog(BuildContext cont) async {
  return showDialog<void>(
    context: cont,
    barrierDismissible: false, // user must tap button!
    builder: (BuildContext context) {
      return AlertDialog(
        title: const Text('Mobile Portal'),
        content: const Text("Nem található érvényes api Key."),
        actions: <Widget>[
          TextButton(
            child: const Text('Bezárás'),
            onPressed: () {
              Navigator.pop(cont);
            },
          ),
        ],
      );
    },
  );
}

class _reportNavState extends State<reportNav> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final args = ModalRoute.of(context)!.settings.arguments as Map;

      employeePageArguments = args['userArgs'];
      //    empArgs = (ModalRoute.of(context)?.settings.arguments ??
      //    <String, dynamic>{}) as EmployeeArguments;
      //  getSharedPrefs().then((value) {
    });
    //print("KEY: ${employeePageArguments.apiKey}");
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  /*Future<Null> getSharedPrefs() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    userApiKey = prefs.getString("apiKey");

    if (userApiKey == null) {
      _error = true;
      WidgetsBinding.instance!
          .addPostFrameCallback((timeStamp) => _showMyDialog(context));
    }
    if (userApiKey!.length != 31) {
      _error = true;
      WidgetsBinding.instance!
          .addPostFrameCallback((timeStamp) => _showMyDialog(context));
    }
  }*/

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("Jelentések"),
          /* actions: [
            IconButton(
              icon: Icon(Icons.search),
              onPressed: () {},
            ),
            IconButton(
              icon: Icon(
                Icons.more_vert,
              ),
              onPressed: () {},
            )
          ],*/
          actionsIconTheme: const IconThemeData(
            size: 32,
          ),
        ),
        //drawer: Drawer(),
        body: _error ? Container() : page(context));
  }
}

Container page(BuildContext context) {
  final args = ModalRoute.of(context)!.settings.arguments as Map;

  EmployeePageArguments employeePageArguments = args['userArgs'];
  if (employeePageArguments.apiKey == null) {
    _showMyDialog(context);
  }

  return Container(
    child: GridView.count(
      crossAxisSpacing: 10,
      mainAxisSpacing: 10,
      crossAxisCount: 2,
      shrinkWrap: true,
      padding: const EdgeInsets.only(top: 24, left: 24, right: 24),
      children: <Widget>[
        /*  MaterialButton(
          onPressed: () {
            Navigator.pushNamed(context, '/newDailyReport');
          },
          color: Colors.blue,
          textColor: Colors.white,
          child: Column(
            children: [
              Icon(
                Icons.add,
                size: 48,
              ),
              Text(
                "Új jelentés",
                style: TextStyle(
                    fontSize: 18,
                    color: Colors.white,
                    fontWeight: FontWeight.bold),
              )
            ],
          ),
          padding: EdgeInsets.only(top: 32.0),
          //shape:  RoundedRectangleBorder(side:,borderRadius: Radius())), // CircleBorder(),
        ),*/
        MaterialButton(
          onPressed: () {
            Navigator.pushNamed(context, '/reportList', arguments: {
              'userArgs': employeePageArguments,
              'dataArgs': null
            });
          },
          color: Colors.blue,
          textColor: Colors.white,
          padding: const EdgeInsets.only(top: 32.0),
          child: const Column(
            children: [
              Icon(
                Icons.list,
                size: 48,
              ),
              Text(
                "Jelentések",
                style: TextStyle(
                    fontSize: 18,
                    color: Colors.white,
                    fontWeight: FontWeight.bold),
              )
            ],
          ),
          //shape:  RoundedRectangleBorder(side:,borderRadius: Radius())), // CircleBorder(),
        ),
        MaterialButton(
          onPressed: () {
            Navigator.pushNamed(context, '/reportCreate', arguments: {
              'userArgs': employeePageArguments,
              'dataArgs': null
            });
          },
          color: Colors.blue,
          textColor: Colors.white,
          padding: const EdgeInsets.only(top: 32.0),
          child: const Column(
            children: [
              Icon(
                Icons.add,
                size: 48,
              ),
              Text(
                "Új jelentés",
                style: TextStyle(
                    fontSize: 18,
                    color: Colors.white,
                    fontWeight: FontWeight.bold),
              )
            ],
          ),
          //shape:  RoundedRectangleBorder(side:,borderRadius: Radius())), // CircleBorder(),
        ),
      ],
    ),
  );
}
