import 'package:diplomaticquarterapp/core/model/my_trakers/blood_sugar/DiabtecPatientResult.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/chartData/WeekChartDate.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/blood_sugar_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/medical/my_trackers/widget/LineChartCurved.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/charts/app_time_series_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:charts_flutter/flutter.dart' as charts;
import 'package:provider/provider.dart';
import 'AddBloodSugarPage.dart';

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

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

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return AppScaffold(
      body:timeSeriesData.isEmpty?Container(child: Center(child: Texts(TranslationBase.of(context).noDataAvailable),),): ListView(
        children: [
          Container(
            margin: EdgeInsets.only(top: 12, left: 8, right: 8),
            color: Colors.white,
            child: LineChartCurved(
              title: 'Sugar',
              timeSeries: timeSeriesData,
              indexes: timeSeriesData.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>[
                Table(
                  border: TableBorder.symmetric(
                    inside: BorderSide(width: 2.0, color: Colors.grey[300]),
                  ),
                  children:
                      fullData(context, projectViewModel, bloodSugarViewMode),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  List<TableRow> fullData(
      BuildContext context,
      ProjectViewModel projectViewModel,
      BloodSugarViewMode bloodSugarViewMode) {
    List<TableRow> tableRow = [];
    tableRow.add(
      TableRow(
        children: [
          Container(
            child: Container(
              decoration: BoxDecoration(
                color: Theme.of(context).primaryColor,
                borderRadius: BorderRadius.only(
                  topLeft: projectViewModel.isArabic
                      ? Radius.circular(0.0)
                      : Radius.circular(10.0),
                  topRight: projectViewModel.isArabic
                      ? Radius.circular(10.0)
                      : Radius.circular(0.0),
                ),
              ),
              child: Center(
                child: Texts(
                  TranslationBase.of(context).date,
                  color: Colors.white,
                  fontSize: 15,
                ),
              ),
              height: 40,
            ),
          ),
          Container(
            child: Container(
                decoration: BoxDecoration(
                  color: Theme.of(context).primaryColor,
                ),
                child: Center(
                  child: Texts(
                    TranslationBase.of(context).time,
                    color: Colors.white,
                    fontSize: 15,
                  ),
                ),
                height: 40),
          ),
          Container(
            child: Container(
                decoration: BoxDecoration(
                  color: Theme.of(context).primaryColor,
                ),
                child: Center(
                  child: Texts(
                    TranslationBase.of(context).measured,
                    color: Colors.white,
                    fontSize: 15,
                  ),
                ),
                height: 40),
          ),
          Container(
            child: Container(
                decoration: BoxDecoration(
                  color: Theme.of(context).primaryColor,
                ),
                child: Center(
                  child: Texts(
                    TranslationBase.of(context).value,
                    color: Colors.white,
                    fontSize: 15,
                  ),
                ),
                height: 40),
          ),
          Container(
            child: Container(
                decoration: BoxDecoration(
                  color: Theme.of(context).primaryColor,
                  borderRadius: BorderRadius.only(
                    topLeft: projectViewModel.isArabic
                        ? Radius.circular(10.0)
                        : Radius.circular(0.0),
                    topRight: projectViewModel.isArabic
                        ? Radius.circular(0.0)
                        : Radius.circular(10.0),
                  ),
                ),
                child: Center(
                  child: Texts(
                    TranslationBase.of(context).edit,
                    color: Colors.white,
                    fontSize: 15,
                  ),
                ),
                height: 40),
          ),
        ],
      ),
    );
    diabtecPatientResult.forEach(
      (diabtec) {
        tableRow.add(
          TableRow(
            children: [
              Container(
                child: Container(
                  height: 70,
                  padding: EdgeInsets.all(10),
                  color: Colors.white,
                  child: Center(
                    child: Texts(
                      '${projectViewModel.isArabic ? DateUtil.getMonthDayYearDateFormattedAr(diabtec.dateChart):DateUtil.getMonthDayYearDateFormatted(diabtec.dateChart)} ',
                      textAlign: TextAlign.center,
                      fontSize: 12,
                    ),
                  ),
                ),
              ),
              Container(
                child: Container(
                  height: 70,
                  padding: EdgeInsets.all(10),
                  color: Colors.white,
                  child: Center(
                    child: Texts(
                      '${diabtec.dateChart.hour}:${diabtec.dateChart.minute}',
                      textAlign: TextAlign.center,
                      fontSize: 12,
                    ),
                  ),
                ),
              ),
              Container(
                child: Container(
                  height: 70,
                  padding: EdgeInsets.all(10),
                  color: Colors.white,
                  child: Center(
                    child: Texts(
                      '${diabtec.measuredDesc}',
                      textAlign: TextAlign.center,
                      fontSize: 12,
                    ),
                  ),
                ),
              ),
              Container(
                child: Container(
                  height: 70,
                  padding: EdgeInsets.all(10),
                  color: Colors.white,
                  child: Center(
                    child: Texts(
                      '${diabtec.resultValue}',
                      textAlign: TextAlign.center,
                      fontSize: 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,
                        ),
                      ),
                    );
                  },
                  child: Container(
                    height: 70,
                    padding: EdgeInsets.all(10),
                    color: Colors.white,
                    child: Center(
                      child: Icon(Icons.edit),
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
    return tableRow;
  }
}
