import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'package:tasktracker/NewJournal.dart';
import 'package:tasktracker/NewTodo.dart';
import 'package:tasktracker/main.dart';
import 'package:tasktracker/newActivity.dart';
import 'Data.dart';
import 'DebugHelper.dart';
import 'User.dart' as User;
import 'Dialogs.dart';

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

  @override
  State<TodosPage> createState() => _TodosPageState();
}

class _TodosPageState extends State<TodosPage> {
  bool selecting = false;
  int expandedIndex = -1;
  var refreshStream;

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

    refreshStream = User.refreshStream.stream.listen((event) {
      if (!event) {
        setState(() {});
      }
    });
  }

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

  Map<String, List<Todo>> projects = <String, List<Todo>>{};
  List<Todo> simpleTodos = [];
  List<String> expandedProjects = [];
  bool expandedSimpleTodos = false;
  List<String> selectedProjects = [];
  @override
  Widget build(BuildContext context) {
    projects = <String, List<Todo>>{};
    simpleTodos = [];
    List<Todo> todos = User.todos;
    todos.sort((a,b)=> a.dueDate.compareTo(b.dueDate));
    for (var todo in todos) {
      if (todo.task!.relatedProject != null) {
        String projectName = todo.task!.relatedProject!.name;
        if (projects.containsKey(projectName)) {
          projects[projectName]!.add(todo);
        } else {
          projects.putIfAbsent(projectName, () => [todo]);
        }
      }else{
        simpleTodos.add(todo);
      }
    }
    bool landscape=((MediaQuery.of(context).size.width / MediaQuery.of(context).size.height) > 1);
    return Scaffold(
      floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            Navigator.of(context).push(MaterialPageRoute(builder: (context) => NewTodo())).then((val) {
              setState(() {});
            });
          },
          label: Text("New To-Do"),
          icon: Icon(Icons.add)),
      appBar: AppBar(
        title: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                FaIcon(FontAwesomeIcons.calendarCheck),
                SizedBox(
                  width: 15,
                ),
                Text('To-Do')
              ],
            ),
            Row(
              children: [
                if (selecting && selectedProjects.length > 0)
                  InkWell(
                      onTap: () {
                        selecting = false;
                        deleteTodos();
                        setState(() {});
                      },
                      child: Container(margin: EdgeInsets.all(8), child: Icon(Icons.delete))),
                if (selecting)
                  InkWell(
                      onTap: () {
                        selecting = false;
                        setState(() {});
                      },
                      child: Container(margin: EdgeInsets.all(8), child: Icon(Icons.cancel))),
                if (!selecting)
                  InkWell(
                      onTap: () {
                        setState(() {});
                      },
                      child: Container(
                        margin: EdgeInsets.all(8),
                        child: Icon(Icons.refresh),
                      ))
              ],
            )
          ],
        ),
      ),
      drawer: landscape ? null : navDrawer(context, 9),
      body: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          landscape?navDrawer(context, 9) : Container(),
          Expanded(
            child: SingleChildScrollView(
             // margin: EdgeInsets.fromLTRB(2, 10, 0, 10),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Container(
                    margin: EdgeInsets.all(8),
                    padding: EdgeInsets.symmetric(vertical: 10),
                    decoration: BoxDecoration(color: Colors.black26, borderRadius: BorderRadius.circular(10)),
                    child: (projects.length <=0) ?Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text('No Todos Related to Projects'),
                      ],
                    ) :ScrollablePositionedList.builder(
                      shrinkWrap: true,
                      itemCount: projects.length,
                      itemBuilder: (context, i) {
                        String thisProject=  projects.keys.toList()[i];
                        return Container(
                          margin: EdgeInsets.symmetric(horizontal: 0,vertical: 8),
                          child: Column(
                            children: [
                              InkWell(
                                onTap: (){
                                  if(expandedProjects.contains(thisProject)){
                                    expandedProjects.remove(thisProject);
                                  }else{
                                    expandedProjects.add(thisProject);

                                  }
                                  setState(() {

                                  });
                                },
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                  Row(
                                    children: [
                                      Icon((!expandedProjects.contains(thisProject)) ? Icons.keyboard_arrow_right_outlined : Icons.keyboard_arrow_down_outlined),
                                      Text('${projects.keys.toList()[i]} (${projects.values.toList()[i].length})',style: TextStyle(fontSize: 18)),
                                    ],
                                  ),
                                    Container(),
                                  ],
                                ),
                              ),
                              if(expandedProjects.contains(thisProject))Container(
                                padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
                                child: ScrollablePositionedList.builder(
                                    shrinkWrap: true,
                                    itemCount: projects.values.toList()[i].length,
                                    itemBuilder: (context, index) {
                                      int maxCharCount = 100;
                                      Todo todo = projects.values.toList()[i][index];

                                      print('$i : $index');
                                      return todoCard(todo);
                                    }),
                              ),
                            ],
                          ),
                        );
                      }
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.all(8),
                    padding: EdgeInsets.symmetric(vertical: 10),
                    decoration: BoxDecoration(color: Colors.black26, borderRadius: BorderRadius.circular(15)),
                    child: Column(
                      children: [
                        InkWell(
                          onTap: (){expandedSimpleTodos=!expandedSimpleTodos;setState(() {

                          });},
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Row(
                                children: [
                                  Icon((!expandedSimpleTodos) ? Icons.keyboard_arrow_right_outlined : Icons.keyboard_arrow_down_outlined),
                                  Text('Typical Todos (${simpleTodos.length})',style: TextStyle(fontSize: 18)),
                                ],
                              ),
                              Container(),
                            ],
                          ),
                        ),
                        if(expandedSimpleTodos)Container(
                          padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
                          child: ScrollablePositionedList.builder(
                              shrinkWrap: true,
                              itemCount: simpleTodos.length,
                              itemBuilder: (context, index) {
                                int maxCharCount = 100;
                                Todo todo = simpleTodos[index];
                                return todoCard(todo);
                              }),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget todoCard(Todo todo){

    bool containsStepData = todo.metadata.contains('[') && todo.metadata.contains(']');

    return Container(
        child: InkWell(
          child: Row(
            children: [
              if (selecting)
                Checkbox(
                  value: selectedProjects.contains(todo.id),
                  onChanged: (newVal) {
                    if (selectedProjects.contains(todo.id)) {
                      selectedProjects.remove(todo.id);
                    } else {
                      selectedProjects.add(todo.id);
                    }
                    setState(() {});
                  },
                ),
              Expanded(
                child: Column(
                  children: [
                    Card(

                      // color: color,
                        elevation: 30,
                        shadowColor: colorFromHex(todo.task!.cat!.color),
                        child: InkWell(
                            onTap: () {
                              //Open Respective Category
                              if (selecting) {
                                if (selectedProjects.contains(todo.id)) {
                                  selectedProjects.remove(todo.id);
                                } else {
                                  selectedProjects.add(todo.id);
                                }
                                setState(() {});
                              } else {
                                Navigator.of(context).push(MaterialPageRoute(builder: (context)=> NewActivity(selectedTask: TaskType.getDisplayName(todo.task!), metadata: todo.metadata ,todo: todo,)));
                                setState(() {

                                });
                              }
                            },
                            onLongPress: () {
                              print('gonna delete');
                              selecting = !selecting;
                              selectedProjects=[];
                              //  selectedActivities = [activity];
                              setState(() {});
                            },
                            child: Container(
                                padding: EdgeInsets.all(15),
                                child: Column(
                                  children: [
                                    Row(mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.start, children: [
                                      Text(todo.task!.name, style: TextStyle(fontSize: 17)),
                                      Row(mainAxisAlignment: MainAxisAlignment.start, mainAxisSize: MainAxisSize.max, children: [
                                        Icon(
                                          Icons.arrow_forward_outlined,
                                          size: 20,
                                        ),
                                        SizedBox(
                                          width: MediaQuery.of(context).size.width / 3,
                                          child: Text(
                                            (containsStepData)
                                                ? todo.metadata.substring(todo.metadata!.indexOf(']') + 1)
                                                : (todo.metadata ?? ''),
                                          ),
                                        ),
                                      ]),
                                      // Icon(Icons.analytics, color: color, size: 20,),
                                    ]),
                                    SizedBox(
                                      height: 5,
                                    ),
                                    Row(mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                                      Container(
                                          padding: EdgeInsets.symmetric(horizontal: 10, vertical: 2),
                                          decoration: BoxDecoration(borderRadius: BorderRadius.circular(10), color: Colors.blue),
                                          child: Text(DateFormat('yyyy-MM-dd').format(todo.dueDate))),
                                      SizedBox(
                                        width: 20,
                                      ),
                                      Row(
                                        children: [
                                          (todo.task!.relatedProject != null)
                                              ? Row(
                                            children: [
                                              if (containsStepData)
                                                Container(
                                                    padding: EdgeInsets.symmetric(horizontal: 10, vertical: 2),
                                                    decoration:
                                                    BoxDecoration(borderRadius: BorderRadius.circular(10), color: Colors.black26),
                                                    child: Text(todo.metadata!
                                                        .substring(1, todo.metadata!.indexOf(']')) ??
                                                        'n/a')),
                                              SizedBox(
                                                width: 8,
                                              ),
                                              Container(
                                                  padding: EdgeInsets.symmetric(horizontal: 10, vertical: 2),
                                                  decoration: BoxDecoration(borderRadius: BorderRadius.circular(10), color: Colors.black26),
                                                  child: Text(todo.task!.relatedProject!.name ?? 'n/a')),
                                            ],
                                          )
                                              : Container(),
                                          SizedBox(
                                            width: 10,
                                          ),
                                          Container(
                                              padding: EdgeInsets.symmetric(horizontal: 10, vertical: 2),
                                              decoration: BoxDecoration(
                                                  borderRadius: BorderRadius.circular(10),
                                                  color: (todo.task!.cat!.productive) ? Colors.green : Colors.red),
                                              child: Text(todo.task!.cat?.name ?? 'n/a')),
                                        ],
                                      )
                                      // Icon(Icons.circle,
                                      //   color: (productive)
                                      //       ? Colors.green
                                      //       : Colors.red)
                                    ])
                                  ],
                                )))),
                    Container(
                        margin: EdgeInsets.fromLTRB(15, 0, 15, 10),
                        height: 2,
                        child: Row(
                          children: [
                            Expanded(flex: 100, child: Container(color: colorFromHex(todo.task!.cat!.color))),
                            Expanded(flex: 0, child: Container())
                          ],
                        )),
                  ],
                ),
              ),
              if (selecting)
                InkWell(
                    onTap: () {
                      Debug.Log('notification : ${todo.notificationTime}');
                      Navigator.of(context).push(MaterialPageRoute(builder: (context) =>NewTodo(selectedTask: TaskType.getDisplayName(todo.task!), metadata: todo.metadata, dueDate: todo.dueDate, notificationTime: todo.notificationTime, old_id: todo.id,))).then((val) {
                        setState(() {});
                      });
                      selecting = false;
                      setState(() {});
                    },
                    child: Container(margin: EdgeInsets.all(8), child: FaIcon(FontAwesomeIcons.edit)))
            ],
          ),
        ));
  }

  void onClickTodo(Todo todo){

  }

  void deleteTodos() async{
    if(selectedProjects.length > 0){
      User.todos.forEach((list) {
        selectedProjects.forEach((selected) {
          if(list.id == selected){
            User.UserOperations.deleteTodo(selected,bulk: true);
          }
        });
      });

      await User.UserOperations.executeQueries();

    }else{
      Dialogs.showAlertDialog(context, 'Non-Selected', 'Select some tasks to delete, This message should not be shown btw');
    }
  }

  // List<Widget> todos() {
  //   List<Widget> _todos = [];
  //   for (int i = 0; i < projects.length; i++) {
  //     _todos.add(;
  //   }
  //   return _todos;
  // }
}
