import 'package:flutter/material.dart';
import 'package:tasktracker/NewCategory.dart';
import 'main.dart';
import 'NewTask.dart';
import 'User.dart' as User;
import 'Data.dart';
class Categories extends StatefulWidget {
  const Categories({Key? key}) : super(key: key);

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


bool selecting=false;
class _CategoriesState extends State<Categories> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  //init(context);
    UpdateList();
  }

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


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: FloatingActionButton.extended(
            onPressed: () {
              Navigator.of(context)
                  .push(MaterialPageRoute(builder: (context) => NewCategory()))
                  .then((value) => UpdateList());
            },
            label: Text("New Category"),
            icon: Icon(Icons.add)),
        appBar: AppBar(
            title: Row(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(children: [
                  Icon(Icons.account_tree_outlined, color: Theme.of(context).primaryColor),
                  SizedBox(width: 10),
                  Text('Categories')
                ]),
                (selecting)?Row(children: [
                  InkWell(onTap: (){
                    DeleteSelectedCats();
                  }, child: Icon(Icons.delete,size: 30,)),
                  SizedBox(width: 20,),
                  InkWell(onTap: (){setState(() {
                    selecting=false;
                  });}, child: Icon(Icons.close,size: 30),)
                ]) : Container(),
              ],
            )),
        drawer: navDrawer(context, 4),
        body: Container(
            padding: EdgeInsets.all(10),
            child: SingleChildScrollView(
                child: Column(
              children: PrintCats(),
            ))));
  }

  void UpdateList() async {
    await User.refreshUserData();
    setState(() {});
  }

  List<Widget> PrintCats() {
    List<Widget> _cats = [];
    print('Priting cats : ' + User.categories.length.toString());
    User.categories.forEach((element) {
      String name = element.name;
      Color color = HexColor.fromHex(element.color);
      bool productive = element.productive;
      Widget cat = TaskCard(context,name, productive, color);
      _cats.add(cat);
    });

    return _cats;
  }
  void OnItemSelected(String name){
    if (!selectedTasks.contains(name)) {
      selectedTasks.add(name);
    } else {
      selectedTasks.remove(name);
    }
  }

  void DeleteSelectedCats() async{
    selectedTasks.forEach((element) async {
      await User.UserOperations.deleteCategory(element, bulk:true);
    });

    await Future.delayed(Duration(seconds: 2));
    await User.UserOperations.executeQueries();
    selectedTasks=[];
    selecting=false;
    setState(() {
    });

  }
  Widget TaskCard(
      BuildContext context, String name, bool productive, Color color) {
    return Row(children: [
      // Container(),
      (selecting)
          ? Checkbox(
          value: selectedTasks.contains(name),
          onChanged: (value) {
            print('selected $name');
            OnItemSelected(name);
            setState(() {});
          })
          : Container(),
      Expanded(
        child: Column(children: [
          Card(
            // color: color,
              elevation:20,
              shadowColor: color,
              child: InkWell(
                  onTap: () {
                    //Open Respective Category
                    if(selecting){
                      OnItemSelected(name);
                    }
                    setState(() {

                    });
                  },
                  onLongPress: () {
                    print('gonna delete');
                    selecting = !selecting;
                    selectedTasks = [name];
                    setState(() {});

                  },
                  child: Container(
                      padding: EdgeInsets.all(10),
                      child: Column(
                        children: [
                          Row(
                              mainAxisSize: MainAxisSize.max,
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(name,
                                    ),
                                // Icon(Icons.analytics, color: color, size: 20,),
                                Icon(Icons.circle,
                                    color: (productive)
                                        ? Colors.green
                                        : Colors.red)
                              ]),
                        ],
                      )))),
          Container(
              margin: EdgeInsets.fromLTRB(15, 0, 15, 10),
              height: 2,
              color: color)
        ]),
      ),
    ]);
  }
}

List<String> selectedTasks = [];

// Widget TaskCard(String name, bool productive, Color color) {
//   return Column(
//       children: [Card(
//       // color: color,
//       elevation: 30,
//       shadowColor: color,
//       child: InkWell(
//           onTap: () {
//             //Open Respective Category
//           },
//           onLongPress: () {
//             print('gonna delete');
//           },
//           child: Container(
//                   padding: EdgeInsets.all(10),
//                   child: Column(
//                     children: [
//                       Row(
//                           mainAxisSize: MainAxisSize.max,
//                           mainAxisAlignment: MainAxisAlignment.spaceBetween,
//                           children: [
//                             Text(name, style: TextStyle(color: Colors.white)),
//                             // Icon(Icons.analytics, color: color, size: 20,),
//                             Icon(Icons.circle,
//                                 color: (productive) ? Colors.green : Colors.red)
//                           ]),
//                     ],
//                   ))
//
//           )),
//
//         Container(
//           margin: EdgeInsets.fromLTRB(10, 0, 10, 10),
//             height: 2,
//             color: color
//         )
//       ]);
// }
