import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
import 'package:tasktracker/Data.dart';
import 'package:tasktracker/NewProject.dart';
import 'package:tasktracker/ProjectDetails.dart';
import 'Dialogs.dart';
import 'User.dart' as User;
import 'main.dart';
class Projects extends StatefulWidget {
  const Projects({Key? key}) : super(key: key);

  @override
  State<Projects> createState() => _ProjectsState();
}

class _ProjectsState extends State<Projects> {
  var refreshSub;
  @override void initState() {
    // TODO: implement initState
    super.initState();

    refreshSub = User.refreshStream.stream.listen((event) {
        setState(() {

        });
    });
  }

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

  void refresh(){
    setState(() {

    });
  }

  int selectedPage = 0;
  @override
  Widget build(BuildContext context) {
    bool landscape=((MediaQuery.of(context).size.width / MediaQuery.of(context).size.height) > 1);
    return SafeArea(child: Scaffold(
      floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            Navigator.of(context).push(MaterialPageRoute(builder: (context) => NewProject())).then((value) => {User.refreshUserData().then((va) => {refresh()})});
          },
          label: Text("New Project"),
          icon: Icon(Icons.add)),
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: FaIcon(FontAwesomeIcons.newspaper),label:'Summary'),
          BottomNavigationBarItem(icon: FaIcon(FontAwesomeIcons.info),label:'Details'),
        ],
        currentIndex:selectedPage,
        onTap: (val){
          selectedPage= val;
          setState(() {

          });
        },
      ),
      appBar: AppBar(
          title: Column(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(children: [Icon(Icons.work_outline_sharp, color: Theme.of(context).primaryColor), SizedBox(width: 10), Text('Projects')]),
                  Row(
                    children: [
                      (User.offline)
                          ? Icon(Icons.signal_cellular_connected_no_internet_4_bar_outlined)
                          : InkWell(
                        onTap: () async{
                          User.refreshUserData().then((val){setState(() {

                          });});
                          setState(() {
                            //LoadStats();

                          });
                        },
                        child: Icon(Icons.refresh, size: 30),
                      )
                    ],
                  )
                ],
              ),
              //Container(color: Colors.red,child: Text("Offline",style:TextStyle(fontSize: 5))),
            ],
          )),
      drawer: landscape ? null : navDrawer(context, 7),
      body:
      Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          landscape?navDrawer(context, 7) : Container(),
          Expanded(
            child: (User.projects.isEmpty)? Container(child: Image.asset(('images/project.png'),color: Colors.white.withOpacity(0.6), colorBlendMode: BlendMode.modulate,)) :
            (selectedPage == 0) ?
                SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  child: Container(
                      padding: EdgeInsets.all(15),
                      child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Card(child:
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          children: [
                            Row(
                              children: [
                                FaIcon(FontAwesomeIcons.cogs, color: Colors.yellow),
                                SizedBox(width: 15,),
                                Text('Active Projects (${User.projects.length})',style:TextStyle(fontSize: 18)),
                              ],
                            ),
                            Divider(),
                            Container(
                              height: 200,
                              child: ListView.builder(
                                  itemCount: User.projects.length,
                                  itemBuilder: (context,index){
                                    int progress =  0;
                                    for (var value in User.projects[index].steps) {
                                      if(value.finishedDate!=null){
                                        progress += value.progress;
                                      }
                                    }

                                    return InkWell(
                                      onTap: (){
                                        Navigator.of(context).push(MaterialPageRoute(builder: (context)=> ProjectDetails(project: User.projects[index],)));
                                      },
                                      child: Container(
                                        decoration: BoxDecoration(color: Colors.black26, borderRadius: BorderRadius.circular(10)),
                                          padding:EdgeInsets.all(8),
                                          margin: EdgeInsets.all(1),
                                          child:
                                      Row(mainAxisSize: MainAxisSize.max,
                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                        children: [
                                          Expanded(flex:3,child: Text(User.projects[index].name)),
                                          Expanded(flex:2,child: Text('$progress%')),
                                          Expanded(
                                            flex:2,
                                            child: Row(
                                              mainAxisAlignment: MainAxisAlignment.end,
                                              children: [
                                              Container(
                                                decoration: BoxDecoration(
                                                  borderRadius: BorderRadius.circular(20),
                                                  color: User.projects[index].cat!.productive ? Colors.green : Colors.redAccent,
                                                ),
                                                padding:EdgeInsets.symmetric(horizontal: 8),
                                                child:Text(User.projects[index].cat!.name)
                                              )
                                            ],),
                                          )
                                        ],
                                      )
                                      ),
                                    );
                                  }
                              ),
                            )

                          ],
                        ),
                      )),
                      SizedBox(height: 30,),
                      Card(child:
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          children: [
                            Row(
                              children: [
                                FaIcon(FontAwesomeIcons.check,color: Colors.green,),
                                SizedBox(width: 15,),
                                Text('Finished Projects (0)',style:TextStyle(fontSize: 18)),
                              ],
                            ),
                            Divider(),
                            Container(
                              height: 50,
                            )

                          ],
                        ),
                      )),
                    ],
                  )),
                )
                :
            SingleChildScrollView(
              scrollDirection: Axis.vertical,
              child: Container(
                padding: EdgeInsets.all(10),
                child: Column(
                  children: [
                    Column(
                      children: printProjects(),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    ));
  }

  List<Widget> printProjects(){
    List<Widget> projectWidgets = [];
    print(User.projects.length);
    for (var element in User.projects) {
      print(element.name);
      if(element.cat==null){print(element.name + " has no cat");continue;}
      projectWidgets.add(ProjectCard(element));
    }

    return projectWidgets;
  }

  Widget ProjectCard(Project project){
    Duration timeLeft = project.deadline.difference(DateTime.now());
    return Row(
      mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.center,
        children:[
          Expanded(
              child: Card(
                  child:AnimatedContainer(
                    duration: const Duration(milliseconds: 200),
                    height: 100,
                    child: InkWell(
                      onTap: (){
                        Navigator.of(context).push(MaterialPageRoute(builder: (context)=> ProjectDetails(project: project,)));
                      },
                      onLongPress: (){
                        Dialogs.showProjectDetails(project);
                      },
                      child: Padding(
                        padding: const EdgeInsets.all(10.0),
                        child: Column(
                          children: [
                            Row(
                              mainAxisSize: MainAxisSize.max,
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(project.name ,style: TextStyle(fontSize: 18)),
                                Container(
                                  decoration: BoxDecoration(
                                    color: (project.cat!.productive) ?Colors.green : Colors.red,
                                    borderRadius: BorderRadius.circular(8)
                                  ),
                                    child:Padding(
                                  padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 2),
                                  child: Text(project.cat!.name),
                                ))
                              ],
                            ),
                            Divider(),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                (project.steps.length > 0) ? Text("${project.steps.length} steps") : Container(),
                                Container(
                                    decoration: BoxDecoration(
                                        color: Colors.redAccent,
                                        borderRadius: BorderRadius.circular(8)
                                    ),
                                    child:Padding(
                                      padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 2),
                                      // padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 2),
                                      child: Text(durationToDays(timeLeft)),
                                      // child: Text("Deadline : " +DateFormat("yyyy-MM-dd").format(project.deadline)),
                                    ))
                              ],
                            )
                          ],
                        ),
                      ),
                    ),
                  )
              )
          )
        ]
    );
  }
}

String durationToDays(Duration duration){
  String output ="";
  if(duration.inDays > 30){
    output += (duration.inDays/30).floor().toString() + " months";
    output += (duration.inDays%30==0) ? "" : " and " + (duration.inDays %30).toString() + " days";
  }else{
    output += (duration.inDays %30).toString() + " days";
  }
  
  output += " left";

  return output;
}