import 'package:diplomaticquarterapp/core/model/vital_sign/vital_sign_res_model.dart';
import 'package:diplomaticquarterapp/pages/medical/vital_sign/vital_sign_details_wideget.dart';
import 'package:diplomaticquarterapp/widgets/charts/app_time_series_chart.dart';
import 'package:diplomaticquarterapp/widgets/others/app_expandable_notifier.dart';
import 'package:flutter/material.dart';

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

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<VitalSignResModel> vitalList;
  final String name;
  final String viewKey;
  final String title1;
  final String title2;
  List<TimeSeriesSales> timeSeriesData = [];

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        AppExpandableNotifier(
          headerWidget: AppTimeSeriesChart(
            seriesList: generateData(),
            chartName: name,
            startDate: DateTime(
                vitalList[vitalList.length - 1]
                    .vitalSignDate
                    .year,
                vitalList[vitalList.length - 1]
                    .vitalSignDate
                    .month +
                    3,
                vitalList[vitalList.length - 1]
                    .vitalSignDate
                    .day),
            endDate: vitalList[0].vitalSignDate,
          ),
          bodyWidget: VitalSignDetailsWidget(
            vitalList: vitalList.reversed.toList(),
            title1: title1,
            title2: title2,
            viewKey: viewKey,
          ),
        ),
      ],
    );
  }

  generateData() {
    if (vitalList.length > 0) {
      vitalList.forEach(
            (element) {
          timeSeriesData.add(
            TimeSeriesSales(
              new DateTime(element.vitalSignDate.year,
                  element.vitalSignDate.month, element.vitalSignDate.day),
              element.toJson()[viewKey]?.toInt(),
            ),
          );
        },
      );
    }
    return [
      new charts.Series<TimeSeriesSales, DateTime>(
        id: 'Sales',
        colorFn: (_, __) => charts.MaterialPalette.red.shadeDefault,
        domainFn: (TimeSeriesSales sales, _) => sales.time,
        measureFn: (TimeSeriesSales sales, _) => sales.sales,
        data: timeSeriesData,
      )
    ];
  }
}
