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 NewActivity extends StatefulWidget {
  const NewActivity({Key? key}) : super(key: key);

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

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

class _NewActivity extends State<NewActivity> {
  String value = 'CS:GO';
  DateTime startTime = DateTime.now();
  DateTime endTime = DateTime.now().add(Duration(minutes: 30));
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('New Activity')),
        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')),
                                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: value,
                                        isExpanded: true,
                                        items: <String>[
                                          'Rocket League',
                                          'CS:GO',
                                          'HALO',
                                          'Unity',
                                          'Add new Task Type...'
                                        ].map<DropdownMenuItem<String>>(
                                            (String value) {
                                          return DropdownMenuItem<String>(
                                            value: value,
                                            child: Text(value),
                                          );
                                        }).toList(),
                                        onChanged: (String? _value) {
                                          setState(() {
                                            value = _value!;
                                          });
                                        })),
                                Container(
                                    child: Divider(
                                  height: 30,
                                )),
                                Container(
                                    padding: EdgeInsets.all(10),
                                    child: Text('Start Time')),
                                Container(
                                    padding: EdgeInsets.symmetric(
                                        horizontal: 12, vertical: 1),
                                    decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(12),
                                        border: Border.all(
                                            color: Colors.grey, width: 2)),
                                    child: MaterialButton(
                                        onPressed: () {
                                          setState(() {
                                            DatePicker.showDateTimePicker(
                                                context,
                                                showTitleActions: true,
                                                onChanged: (date) {
                                              // print('change $date');
                                            }, onConfirm: (date) {
                                              setState(() {
                                                startTime = date;
                                              });
                                            },
                                                currentTime: startTime,
                                                locale: LocaleType.en);
                                          });
                                        },
                                        child: Text(
                                            dateFormat.format(startTime),
                                            style: TextStyle(
                                                color: Colors.blue)))),
                                SizedBox(
                                  height: 10,
                                ),
                                Container(
                                    padding: EdgeInsets.all(10),
                                    child: Text('Ended Time')),
                                Container(
                                    padding: EdgeInsets.symmetric(
                                        horizontal: 12, vertical: 1),
                                    decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(12),
                                        border: Border.all(
                                            color: Colors.grey, width: 2)),
                                    child: MaterialButton(

                                        onPressed: () {
                                          setState(() {
                                            DatePicker.showDateTimePicker(
                                                context,
                                                showTitleActions: true,
                                                onChanged: (date) {
                                              // print('change $date');
                                            }, onConfirm: (date) {
                                              setState(() {
                                                endTime = date;
                                              });
                                            },
                                                currentTime: endTime,
                                                locale: LocaleType.en);
                                          });
                                        },
                                        child: Text(dateFormat.format(endTime),
                                            style: TextStyle(
                                                color: Colors.blue)))),
                                SizedBox(
                                  height: 30,
                                ),
                                Text('Duration : ' +
                                    _printDuration(
                                        endTime.difference(startTime))),
                                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(() {});
                                  },
                                  child: Text('Add Entry',
                                      style: TextStyle(fontSize: 20))))),
                        ],
                      ))
                ])));
  }
}

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";
}
