import "package:collection/collection.dart";
import 'package:hmg_patient_app/core/viewModels/medical/weight_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/MonthLineChartCurved.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/uitl/utils_new.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:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class WeightMonthlyPage extends StatelessWidget {
  final WeightPressureViewModel? model;

  WeightMonthlyPage({
    Key? key,
    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(
          decoration: cardRadius(12),
          margin: EdgeInsets.only(left: 16, top: 16, right: 16, bottom: 8),
          child: MonthLineChartCurved(
            horizontalInterval: 1.0,
            title: TranslationBase.of(context).weight,
            timeSeries: model!.weighMonthTimeSeriesData.isEmpty ? [TimeSeriesSales3(0, 0.0)] : model!.weighMonthTimeSeriesData,
            indexes: model!.weighMonthTimeSeriesData.length ~/ 5.5,
          ),
        ),
        Container(
          decoration: cardRadius(12),
          margin: EdgeInsets.only(left: 16, top: 16, right: 16, bottom: 8),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              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>[
                    model!.weighMonthTimeSeriesData.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(2.5),
                                    // 2: FlexColumnWidth(1.8),
                                  },
                                  children: fullData(context, monthly[1]),
                                )
                              ])
                          ])
                  ],
                ),
              )
            ],
          ),
        ),
        mHeight(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).value),
        ],
      ),
    );
    model.forEach(
      (diabtec) {
        tableRow.add(
          TableRow(
            children: [
              Utils.tableColumnValue('${DateUtil.getDayMonthYearDateFormatted(diabtec.weightDate)} ', isCapitable: false, mProjectViewModel: projectViewModel),
              Utils.tableColumnValue('${diabtec.weightDate.hour}:${diabtec.weightDate.minute}', isCapitable: false, mProjectViewModel: projectViewModel),
              Utils.tableColumnValue('${diabtec.weightMeasured}', isCapitable: false, mProjectViewModel: projectViewModel),
            ],
          ),
        );
      },
    );
    return tableRow;
  }

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