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

import '../../../config/size_config.dart';
import '../../../models/patient/vital_sign/vital_sign_res_model.dart';
import '../../../widgets/shared/rounded_container_widget.dart';

/*
 *@author: Elham Rababah 
 *@Date:03/6/2020
 *@param:
 *@return:
 *@desc: AppTimeSeriesChart
 */
class AppTimeSeriesChart extends StatelessWidget {
  AppTimeSeriesChart(
      {Key key,
      @required this.vitalList,
      @required this.viewKey,
      this.chartName = ''});

  final List<VitalSignResModel> vitalList;
  final String chartName;
  final String viewKey;
  List<charts.Series> seriesList;

  @override
  Widget build(BuildContext context) {
    seriesList = generateData();
    return RoundedContainer(
      height: SizeConfig.realScreenHeight * 0.47,
      child: Column(
        children: <Widget>[
          Text(
            chartName,
            style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: SizeConfig.textMultiplier * 3),
          ),
          Container(
            height: SizeConfig.realScreenHeight * 0.37,
            child: Center(
              child: Container(
                child: charts.TimeSeriesChart(
                  seriesList,
                  animate: true,
                  behaviors: [
                    new charts.RangeAnnotation(
                      [
                        new charts.RangeAnnotationSegment(
                            DateTime(
                                vitalList[vitalList.length - 1]
                                    .vitalSignDate
                                    .year,
                                vitalList[vitalList.length - 1]
                                        .vitalSignDate
                                        .month +
                                    3,
                                vitalList[vitalList.length - 1]
                                    .vitalSignDate
                                    .day),
                            vitalList[0].vitalSignDate,
                            charts.RangeAnnotationAxisType.domain),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  /*
 *@author: Elham Rababah 
 *@Date:03/6/2020
 *@param:
 *@return:
 *@desc: generateData
 */
  generateData() {
    final List<TimeSeriesSales> data = [];
    if (vitalList.length > 0) {
      vitalList.forEach(
        (element) {
          data.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',
        domainFn: (TimeSeriesSales sales, _) => sales.time,
        measureFn: (TimeSeriesSales sales, _) => sales.sales,
        data: data,
      )
    ];
  }
}

/*
 *@author: Elham Rababah 
 *@Date:03/6/2020
 *@param:
 *@return:
 *@desc: TimeSeriesSales
 */
class TimeSeriesSales {
  final DateTime time;
  final int sales;

  TimeSeriesSales(this.time, this.sales);
}
