import "package:collection/collection.dart";
import 'package:hmg_patient_app/core/model/my_trakers/blood_sugar/DiabtecPatientResult.dart';
import 'package:hmg_patient_app/core/viewModels/medical/blood_sugar_view_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/uitl/date_uitl.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils.dart';
import 'package:hmg_patient_app/widgets/charts/app_time_series_chart.dart';
import 'package:hmg_patient_app/widgets/charts/show_chart.dart';
import 'package:hmg_patient_app/widgets/data_display/text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class BloodYearPage extends StatelessWidget {
  final List<DiabtecPatientResult>? diabtecPatientResult;
  final List<TimeSeriesSales2>? timeSeriesData;
  final BloodSugarViewMode? bloodSugarViewMode;

  BloodYearPage({Key? key, this.diabtecPatientResult, this.timeSeriesData, this.bloodSugarViewMode}) : super(key: key);
  List<List> yearlyGroup = [];
  late ProjectViewModel projectViewModel;

  @override
  Widget build(BuildContext context) {
    // if (projectViewModel == null) 
      projectViewModel = Provider.of(context);

    groupData();
    return ListView(
      children: [
        Container(
          margin: EdgeInsets.only(top: 12, left: 8, right: 8),
          width: double.maxFinite,
          color: Colors.white,
          child: ShowChart(
            title: TranslationBase.of(context).weight,
            timeSeries: timeSeriesData!.isEmpty ? [TimeSeriesSales2(DateTime.now(), 0.0)] : timeSeriesData,
            indexes: timeSeriesData!.length ~/ 5.5,
            horizontalInterval: 2,
          ),
        ),
        SizedBox(
          height: 12,
        ),
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Texts(TranslationBase.of(context).details),
        ),
        Container(
          padding: EdgeInsets.all(10),
          color: Colors.transparent,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              timeSeriesData!.isEmpty
                  ? Container(
                      child: Center(
                        child: Texts(TranslationBase.of(context).noDataAvailable),
                      ),
                    )
                  : Column(children: [
                      for (var monthly in yearlyGroup)
                        Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                          Container(
                              width: double.maxFinite,
                              padding: EdgeInsets.only(top: 10, bottom: 10, left: 5, right: 5),
                              decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(5)),
                              child: Texts(monthly[0])),
                          Table(
                            columnWidths: {
                              0: FlexColumnWidth(1.8),
                              2: FlexColumnWidth(1.8),
                            },
                            children: fullData(context, monthly[1]),
                          )
                        ])
                    ]),
              SizedBox(height: 80)
            ],
          ),
        )
      ],
    );
  }

  List<TableRow> fullData(BuildContext context, yearlyGroup) {
    List<TableRow> tableRow = [];
    tableRow.add(
      TableRow(
        children: [
          Utils.tableColumnTitle(TranslationBase.of(context).date),
          Utils.tableColumnTitle(TranslationBase.of(context).time),
          Utils.tableColumnTitle(TranslationBase.of(context).measured),
          Utils.tableColumnTitle(TranslationBase.of(context).value),
        ],
      ),
    );
    yearlyGroup.forEach(
      (diabtec) {
        tableRow.add(
          TableRow(
            children: [
              Utils.tableColumnValue(DateUtil.getDayMonthYearDateFormatted(diabtec.dateChart), isCapitable: false, mProjectViewModel: projectViewModel),
              Utils.tableColumnValue(diabtec.dateChart.hour.toString() + ':' + diabtec.dateChart.minute.toString(), isCapitable: false, mProjectViewModel: projectViewModel),
              Utils.tableColumnValue(diabtec.measuredDesc, isCapitable: false, mProjectViewModel: projectViewModel),
              Utils.tableColumnValue(diabtec.resultValue.toString(), isCapitable: false, mProjectViewModel: projectViewModel),
            ],
          ),
        );
      },
    );
    return tableRow;
  }

  groupData() {
    yearlyGroup.clear();
    var groupedArray = groupBy(diabtecPatientResult!.reversed, (obj) => DateUtil.getMonth(obj.dateChart!.month) + ' ' + obj.dateChart!.year.toString());
    groupedArray.entries.forEach((e) => yearlyGroup.add([e.key, e.value]));
  }
}
