import 'package:doctor_app_flutter/models/patient/vital_sign/patient-vital-sign-data.dart';
import 'package:doctor_app_flutter/models/patient/vital_sign/patient-vital-sign-history.dart';
import 'package:doctor_app_flutter/screens/patients/profile/vital_sign/vital_sign_details_wideget.dart';
import 'package:doctor_app_flutter/util/date-utils.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/charts/app_time_series_chart.dart';
import 'package:doctor_app_flutter/widgets/shared/app_expandable_notifier.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:flutter/material.dart';

import 'package:charts_flutter/flutter.dart' as charts;

import 'LineChartCurved.dart';

class VitalSingChartAndDetials extends StatelessWidget {
  VitalSingChartAndDetials({
    Key key,
    @required this.vitalList,
    @required this.name,
    @required this.viewKey,
    @required this.title1,
    @required this.title2,
  }) : super(key: key);

  final List<VitalSignHistory> vitalList;
  final String name;
  final String viewKey;
  final String title1;
  final String title2;
  List<TimeSeriesSales2> timeSeriesData = [];

  @override
  Widget build(BuildContext context) {
    generateData();
    return timeSeriesData.length != 0 ? Column(
      children: <Widget>[
        AppExpandableNotifier(
          // isExpand: true,
          headerWid: LineChartCurved(title: name,timeSeries:timeSeriesData,indexes: timeSeriesData.length~/5.5,),
          bodyWid: VitalSignDetailsWidget(
            vitalList: vitalList,
            title1: title1,
            title2: title2,
            viewKey: viewKey,
          ),
        ),
      ],
    ) : Container(
      width: double.infinity,
      height: MediaQuery.of(context).size.height,
      child: Center(
        child: AppText(TranslationBase.of(context).vitalSignDetailEmpty),
      ),
    );
  }

  generateData() {
    if (vitalList.length > 0) {
      vitalList.reversed.toList().forEach(
            (element) {
          DateTime elementDate = DateUtils.getDateTimeFromServerFormat(element.createdOn);
          if(element.toJson()[viewKey]!=null && element.toJson()[viewKey]?.toInt()!=0)
            timeSeriesData.add(
              TimeSeriesSales2(
                new DateTime(elementDate.year, elementDate.month, elementDate.day),
                element.toJson()[viewKey].toDouble(),
              ),
            );
        },
      );
    }
    return timeSeriesData.reversed.toList();
  }
}
