import "package:collection/collection.dart";
import 'package:hmg_patient_app/core/viewModels/medical/blood_pressure_view_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/pages/medical/my_trackers/widget/CurvedChartBloodPressure.dart';
import 'package:hmg_patient_app/pages/medical/vital_sign/LineChartCurvedBloodPressure.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/data_display/text.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class BloodPressureYearPage extends StatelessWidget {
  final BloodPressureViewMode model;

  BloodPressureYearPage({Key? key, required this.model}) : super(key: key);
  List<List> monthlyGroup = [];
  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),
          color: Colors.white,
          child: LineChartCurvedBloodPressure(
            title: TranslationBase.of(context).bloodPressure,
            timeSeries1: model.weightYearTimeSeriesDataTop.isEmpty ? [TimeSeriesSales2(DateTime.now(), 0.0)] : model.weightYearTimeSeriesDataTop,
            timeSeries2: model.weightYearTimeSeriesDataLow.isEmpty ? [TimeSeriesSales2(DateTime.now(), 0.0)] : model.weightYearTimeSeriesDataLow,
            indexes: model.weightWeekTimeSeriesDataLow.length ~/ 5.5,
          ),
        ),
        SizedBox(
          height: 12,
        ),
        Container(
          padding: EdgeInsets.all(10),
          color: Colors.transparent,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              model.weightYearTimeSeriesDataTop.isEmpty
                  ? Container(
                child: Center(
                  child: Texts(TranslationBase.of(context).noDataAvailable),
                ),
              )
                  : Column(children: [
                for (var monthly in monthlyGroup)
                  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, model) {
    List<TableRow> tableRow = [];
    tableRow.add(
      TableRow(
        children: [
          Utils.tableColumnTitle(TranslationBase.of(context).date),
          Utils.tableColumnTitle(TranslationBase.of(context).time),
          Utils.tableColumnTitle(TranslationBase.of(context).arm),
          Utils.tableColumnTitle(TranslationBase.of(context).value),
        ],
      ),
    );
    model.forEach(
      (diabtec) {
        tableRow.add(
          TableRow(
            children: [
              Utils.tableColumnValue('${DateUtil.getDayMonthYearDateFormatted(diabtec.bloodPressureDate)}', isCapitable: false, mProjectViewModel: projectViewModel),
              Utils.tableColumnValue('${diabtec.bloodPressureDate.hour}:${diabtec.bloodPressureDate.minute}', isCapitable: false, mProjectViewModel: projectViewModel),
              Utils.tableColumnValue(diabtec.measuredArmDesc, isCapitable: false, mProjectViewModel: projectViewModel),
              Utils.tableColumnValue('${diabtec.systolicePressure}/${diabtec.diastolicPressure}', isCapitable: false, mProjectViewModel: projectViewModel),
            ],
          ),
        );
      },
    );
    return tableRow;
  }

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