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/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/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'AddBloodPressurePage.dart';

class BloodPressureWeeklyPage extends StatelessWidget {
  final BloodPressureViewMode? model;

  BloodPressureWeeklyPage({Key? key, this.model}) : super(key: key);

  late ProjectViewModel projectViewModel;

  @override
  Widget build(BuildContext context) {
   // if (projectViewModel == null)
      projectViewModel = Provider.of(context);
    return ListView(
      children: [
        Container(
          margin: EdgeInsets.only(top: 12, left: 8, right: 8, bottom: 12.0),
          padding: EdgeInsets.only(bottom: 12.0),
          color: Colors.white,
          child: LineChartCurvedBloodPressure(
            title: TranslationBase.of(context).bloodPressure,
            timeSeries1: model!.weightWeekTimeSeriesDataTop.isEmpty ? [TimeSeriesSales2(DateTime.now(), 0.0)] : model!.weightWeekTimeSeriesDataTop,
            timeSeries2: model!.weightWeekTimeSeriesDataLow.isEmpty ? [TimeSeriesSales2(DateTime.now(), 0.0)] : model!.weightWeekTimeSeriesDataLow,
            indexes: model!.weightWeekTimeSeriesDataLow.length ~/ 5.5,
          ),
        ),
        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>[
              model!.bloodPressureService.weekDiabtecPatientResult.isEmpty
                  ? Container(
                      child: Center(
                        child: Texts(TranslationBase.of(context).noDataAvailable),
                      ),
                    )
                  : Table(
                      columnWidths: {
                        0: FlexColumnWidth(1.8),
                        2: FlexColumnWidth(1.8),
                      },
                      children: fullData(context, model!),
                    ),
              SizedBox(height: 80)
            ],
          ),
        )
      ],
    );
  }

  List<TableRow> fullData(BuildContext context, BloodPressureViewMode bloodSugarViewMode) {
    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),
          Utils.tableColumnTitle(TranslationBase.of(context).edit),
        ],
      ),
    );
    model!.weekDiabtecPatientResult!.reversed.forEach(
      (diabtec) {
        tableRow.add(
          TableRow(
            children: [
              Utils.tableColumnValue('${DateUtil.getDayMonthYearDateFormatted(diabtec.bloodPressureDate!)} ', isCapitable: false, mProjectViewModel: projectViewModel),
              Utils.tableColumnValue(diabtec.bloodPressureDate!.hour.toString() + ':' + diabtec.bloodPressureDate!.minute.toString(), isCapitable: false, mProjectViewModel: projectViewModel),
              Utils.tableColumnValue('${diabtec.measuredArmDesc}', isCapitable: false, mProjectViewModel: projectViewModel),
              Utils.tableColumnValue('${diabtec.systolicePressure}/${diabtec.diastolicPressure}', isCapitable: false, mProjectViewModel: projectViewModel),
              Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [
                SizedBox(height: 12),
                Container(
                  child: InkWell(
                    onTap: () {
                      Navigator.push(
                        context,
                        FadePage(
                          page: AddBloodPressurePage(
                            model: model,
                            isUpdate: true,
                            lineItemNo: diabtec.lineItemNo,
                            bloodSugarDate: diabtec.bloodPressureDate,
                            bloodDiastolicValue: diabtec.diastolicPressure.toString(),
                            bloodSystolicValue: diabtec.systolicePressure.toString(),
                            measureTimeSelectedType: diabtec.measuredArmDesc,
                            measuredArm: diabtec.measuredArm,
                          ),
                        ),
                      ).then((value) {
                        model!.getBloodPressure();
                        if (model!.weekDiabtecPatientResult.isEmpty) {
                          model!.weightWeekTimeSeriesDataTop.clear();
                          model!.weightWeekTimeSeriesDataLow.clear();
                        }
                      });
                    },
                    child: Container(
                      // height: 70,
                      // padding: EdgeInsets.all(10),
                      child: Icon(
                        Icons.edit,
                        color: Color(0xff575757),
                      ),
                    ),
                  ),
                ),
                SizedBox(height: 6),
                Divider(
                  height: 1,
                  color: Color(0xffEFEFEF),
                  thickness: 1,
                )
              ]),
            ],
          ),
        );
      },
    );
    return tableRow;
  }
}
