import 'package:flutter/material.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/main.dart';

import 'Analytics.dart';
import 'Data.dart';
import 'User.dart' as User;
class AvgDayPage extends StatefulWidget {
  const AvgDayPage({Key? key}) : super(key: key);

  @override
  State<AvgDayPage> createState() => _AvgDayPageState();
}

class _AvgDayPageState extends State<AvgDayPage> {
  DateFormat dFormat = DateFormat('HH:mm');
  DateTimeRange dateRange = DateTimeRange(start: DateTime.now().subtract(Duration(days: 8)), end: DateTime.now().subtract(Duration(days: 1)));
  @override
  Widget build(BuildContext context) {
    List<Activity> avgActs = AnalyticTools.getAverageDayActs(dateRange);

    avgActs.sort((a,b)=> a.startTime.compareTo(b.startTime));

    return Scaffold(
      appBar:AppBar(title:Row(children: [
        FaIcon(FontAwesomeIcons.calendarDay),
        SizedBox(width: 15,),
        Text('Average Day')
      ],)),
      body: Container(
        child: SingleChildScrollView(
          child: Column(
            children: [
              Container(
                padding: EdgeInsets.all(10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,

                  children: [
                    FaIcon(FontAwesomeIcons.calendarDays),
                    SizedBox(width: 10,),
                    Text('Date Range',style:TextStyle(fontSize: 16)),
                    SizedBox(width: 20,),
                    InkWell(
                      onTap: () async {
                        DateTimeRange? value = await showDateRangePicker(context: context, firstDate: User.activities[User.activities.length-1].startTime , lastDate: User.activities[0].startTime);
                        if (value != null) {
                          dateRange = value;
                        }

                        setState(() {

                        });
                      },
                      child: Text((dateRange != null) ? (DateFormat("MM/dd").format(dateRange!.start) + " - " + DateFormat("MM/dd").format(dateRange!.end)) : 'n/a',
                      style: TextStyle(fontSize: 18,fontWeight: FontWeight.bold)),
                    ),
                  ],
                ),
              ),
              ScrollablePositionedList.builder(
                  shrinkWrap: true,
                  itemCount: avgActs.length,
                  itemBuilder: (context, i){
                return Row(
                  children: [
                    Container(
                        padding: EdgeInsets.fromLTRB(10, 0, 5, 0),
                        child: Column(
                          mainAxisSize: MainAxisSize.max,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            // Text(dateFormat.format(activity.endTime)),

                            Text(dFormat.format(avgActs[i].startTime),style: TextStyle(fontSize: 16)),
                            SizedBox(width: 1, height: 50, child: Container(color: Colors.grey)),
                          ],
                        )),
                    Expanded(
                      child: Column(
                        children: [
                          Card(
                            elevation: 30,
                            shadowColor:HexColor.fromHex(avgActs[i].taskType!.cat!.color) ,
                            child: Container(
                              padding: EdgeInsets.all(8),
                              child: Column(
                                children: [
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(avgActs[i].taskType.name,style: TextStyle(fontSize: 18)),
                                      Text(MinutesToTimeString(avgActs[i].endTime.difference(avgActs[i].startTime).inMinutes),style: TextStyle(fontSize: 18)),
                                    ],
                                  ),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text('${dFormat.format(avgActs[i].startTime)} - ${dFormat.format(avgActs[i].endTime)}'),
                                      Container(
                                          padding: EdgeInsets.symmetric(horizontal: 10, vertical: 2),
                                          decoration:
                                          BoxDecoration(borderRadius: BorderRadius.circular(10), color: (avgActs[i].taskType.cat!.productive) ? Colors.green : Colors.red),
                                          child: Text(avgActs[i].taskType.cat?.name ?? 'n/a'))
                                    ],
                                  )
                                ],
                              ),
                            )
                          ),
                          SizedBox(width: 1000,height: 2,child: Container(color: HexColor.fromHex(avgActs[i].taskType!.cat!.color)),),
                          SizedBox(height: 10,),
                        ],
                      ),
                    ),
                  ],
                );
              }),
            ],
          ),
        )
      ),
    );
  }
}
