import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:intl/intl.dart';
import 'User.dart' as User;

DateFormat dateFormat = DateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat durationFormat = DateFormat("HH:mm:ss");

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

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

List<String> getCategoryNames(){
  List<String> _cats = [];
  User.categories.forEach((element) {
    String name = element.name;
    _cats.add(name);
  });
  return _cats;
}

String selectedCat = User.categories[0].name;
class _NewTaskState extends State<NewTask> {
  TextEditingController nameController = TextEditingController();
  bool productive = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('New Task Type')),
        body: Container(
            height: MediaQuery.of(context).size.height,
            child: Column(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  SingleChildScrollView(
                      child: Padding(
                          padding: EdgeInsets.fromLTRB(20, 50, 20, 50),
                          child: Column(
                            children: [
                              Column(children: [
                                Container(
                                    padding: EdgeInsets.all(10),
                                    child: Text('Task Type Name')),
                                Container(
                                  padding: EdgeInsets.all(10),
                                  child: TextField(
                                    controller: nameController,
                                    decoration: InputDecoration(
                                      hintText: 'ex: Study Science, Play CS:GO, etc...',
                                      border: OutlineInputBorder()
                                    ),
                                  ),
                                ),
                                Divider(),
                                Container(
                                    padding: EdgeInsets.all(10),
                                    child: Text('Category')),
                                Container(
                                    padding: EdgeInsets.symmetric(
                                        horizontal: 12, vertical: 1),
                                    decoration: BoxDecoration(
                                        color: Colors.blueGrey,
                                        borderRadius: BorderRadius.circular(12),
                                        border: Border.all(
                                            color: Colors.grey, width: 2)),
                                    child: DropdownButton<String>(
                                        dropdownColor: Colors.blueGrey,
                                        iconSize: 30,
                                        elevation: 10,
                                        borderRadius: BorderRadius.circular(10),
                                        value: selectedCat,
                                        isExpanded: true,
                                        items: getCategoryNames().map<DropdownMenuItem<String>>(
                                            (String value) {

                                          return DropdownMenuItem<String>(
                                            value: value,
                                            child: Text(value),
                                          );
                                        }).toList(),
                                        onChanged: (String? _value) {
                                          setState(() {
                                            selectedCat = _value!;
                                          });
                                        })),
                                Container(
                                    child: Divider(
                                  height: 30,
                                )),
                              ]),
                            ],
                          ))),
                  Container(
                      padding:
                          EdgeInsets.symmetric(vertical: 10, horizontal: 20),
                      child: Row(
                        mainAxisSize: MainAxisSize.max,
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Expanded(
                              flex: 5,
                              child: Container(
                                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
                                  child: ElevatedButton(

                                    style:ElevatedButton.styleFrom(
                                        primary: Colors.red,
                                        shape: StadiumBorder()
                                    ),
                                      onPressed: () {
                                        setState(() {
                                          Navigator.pop(context);
                                        });
                                      },
                                      child: Text('Back',
                                          style: TextStyle(fontSize: 20))))),
                          Expanded(
                              flex: 6,
                            child: Container(
                                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
                                child: ElevatedButton(
                                  style:ElevatedButton.styleFrom(
                                      primary: Colors.green,
                                      shape: StadiumBorder()
                                  ),
                                  onPressed: () {
                                    setState(() {
                                      add_action();
                                    });
                                  },
                                  child: Text('Add Task Type',
                                      style: TextStyle(fontSize: 20))))),
                        ],
                      ))
                ])));
  }

  void add_action() async{
    String catName = nameController.value.text;
    print('adding Task Type : $catName, $selectedCat');
    if(catName.length< 2){
      showAlertDialog(context, 'Category needs a name', 'Please enter a name for this category');
      return;
    }
    await User.UserOperations.addTaskType(catName,selectedCat);
    Navigator.of(context).popUntil((route){
      return route.isFirst;
    });
  }
}

String _printDuration(Duration duration) {
  String twoDigits(int n) => n.toString().padLeft(2, "0");
  String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
  String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));
  return "${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds";
}
showAlertDialog(BuildContext context, String title, String message) {

  // set up the button
  Widget okButton = TextButton(
    child: Text("OK"),
    onPressed: () { Navigator.of(context).pop(); },
  );

  // set up the AlertDialog
  AlertDialog alert = AlertDialog(
    title: Text(title),
    content: Text(message),
    actions: [
      okButton,
    ],
  );

  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}