import 'package:hmg_patient_app/config/config.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/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/charts/show_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 'AddWeightPage.dart';

class WeightWeeklyPage extends StatelessWidget {
  final WeightPressureViewModel model;

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

  late ProjectViewModel projectViewModel;

  @override
  Widget build(BuildContext context) {
    ///if (projectViewModel == null)
      projectViewModel = Provider.of(context);
    return ListView(
      children: [
        Container(
          decoration: cardRadius(12),
          margin: EdgeInsets.only(left: 16, top: 16, right: 16, bottom: 8),
          child: ShowChart(
            title: TranslationBase.of(context).weight,
            timeSeries: model.weightWeekTimeSeriesData.isEmpty ? [TimeSeriesSales2(DateTime.now(), 0.0)] : model.weightWeekTimeSeriesData,
            indexes: model.weightWeekTimeSeriesData.length ~/ 5.5 ?? 0,
            horizontalInterval: 2,
          ),
        ),
        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.weightWeekTimeSeriesData.isEmpty
                        ? Container(
                            child: Center(
                              child: Texts(TranslationBase.of(context).noDataAvailable),
                            ),
                          )
                        : Table(
                            columnWidths: {
                              0: FlexColumnWidth(2.5),
                            },
                            children: fullData(context, model),
                          ),
                  ],
                ),
              )
            ],
          ),
        ),
        mHeight(80),
      ],
    );
  }

  List<TableRow> fullData(BuildContext context, WeightPressureViewModel 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).measured),
          Utils.tableColumnTitle(TranslationBase.of(context).edit),
        ],
      ),
    );
    model.weekWeightMeasurementResult.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),
              Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [
                SizedBox(height: 12),
                Container(
                  child: InkWell(
                    onTap: () {
                      Navigator.push(
                        context,
                        FadePage(
                          page: AddWeightPage(
                            isUpdate: true,
                            dayWeightDate: diabtec.weightDate,
                            measureTimeSelectedType: diabtec.unit == "1" ? 'Kg' : 'Pound',
                            weightValue: diabtec.weightMeasured.toString(),
                            lineItemNo: diabtec.lineItemNo,
                            weightUnit: int.parse(diabtec.unit),
                            model: model,
                          ),
                        ),
                      ).then((value) {
                        if (model.weekWeightMeasurementResult.isEmpty) {
                          model.weightWeekTimeSeriesData.clear();
                        }
                      });
                    },
                    child: Container(
                      child: Icon(
                        Icons.edit,
                        color: Color(0xff575757),
                      ),
                    ),
                  ),
                ),
                SizedBox(height: 6),
                Divider(
                  height: 1,
                  color: Color(0xffEFEFEF),
                  thickness: 1,
                )
              ]),
            ],
          ),
        );
      },
    );
    return tableRow;
  }
}
