import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/blood_sugar/DiabtecPatientResult.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/blood_sugar_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/widgets/charts/app_time_series_chart.dart';
import 'package:diplomaticquarterapp/widgets/charts/show_chart.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'AddBloodSugarPage.dart';

class BloodSugarWeeklyPage extends StatelessWidget {
  final List<DiabtecPatientResult> diabtecPatientResult;
  final BloodSugarViewMode bloodSugarViewMode;
  final List<TimeSeriesSales2> timeSeriesData;
  ProjectViewModel projectViewModel;

  BloodSugarWeeklyPage({Key key, this.diabtecPatientResult, this.bloodSugarViewMode, this.timeSeriesData}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (projectViewModel == null) projectViewModel = Provider.of(context);
    return AppScaffold(
      body: ListView(
        children: [
          Container(
            margin: EdgeInsets.only(top: 12, left: 8, right: 8),
            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(AppGlobal.context).noDataAvailable),
                        ),
                      )
                    : Table(
                        columnWidths: {
                          0: FlexColumnWidth(1.8),
                          2: FlexColumnWidth(1.8),
                        },
                        children: fullData(context, bloodSugarViewMode),
                      ),
                SizedBox(
                  height: 80,
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  List<TableRow> fullData(BuildContext context, BloodSugarViewMode 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),
        ],
      ),
    );
    diabtecPatientResult.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),
              Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [
                SizedBox(height: 12),
                Container(
                  child: InkWell(
                    onTap: () {
                      Navigator.push(
                        context,
                        FadePage(
                          page: AddBloodSugarPage(
                            isUpdate: true,
                            bloodSugarDate: diabtec.dateChart,
                            measuredTime: diabtec.measuredDesc,
                            bloodSugarValue: diabtec.resultValue.toString(),
                            lineItemNo: diabtec.lineItemNo,
                            measureUnitSelectedType: diabtec.unit,
                            bloodSugarViewMode: bloodSugarViewMode,
                            measuredSelectedType: diabtec.measuredDesc,
                          ),
                        ),
                      ).then((value) {
                        if (bloodSugarViewMode.weekDiabtecPatientResult.isEmpty) {
                          timeSeriesData.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;
  }
}
