import 'package:hmg_patient_app/core/model/vital_sign/vital_sign_res_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/uitl/date_uitl.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/charts/app_time_series_chart.dart';
import 'package:hmg_patient_app/widgets/charts/show_chart.dart';
import 'package:hmg_patient_app/widgets/charts/sync_fu_chart.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'LineChartCurved.dart';

class VitalSingChartAndDetials extends StatefulWidget {
  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;

  @override
  _VitalSingChartAndDetialsState createState() => _VitalSingChartAndDetialsState();
}

class _VitalSingChartAndDetialsState extends State<VitalSingChartAndDetials> {
  List<TimeSeriesSales2> timeSeriesData = [];
  bool isExpended = true;
  late ProjectViewModel projectViewModel;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    generateData();
  }

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          Container(
            height: 400,
            child: Container(
              decoration: cardRadius(12),
              margin: EdgeInsets.only(left: 16, top: 16, right: 16, bottom: 8),
              child: ShowChart(
                title: widget.name,
                timeSeries: timeSeriesData,
                indexes: timeSeriesData.length ~/ 5.5,
                horizontalInterval: 8,
              ),
              // child: SyncFuChart(),
            ),
          ),
          Container(
            decoration: cardRadius(12),
            margin: EdgeInsets.only(left: 16, top: 8, right: 16, bottom: 16),
            child: Padding(
              padding: const EdgeInsets.all(12.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    TranslationBase.of(context).graphDetails,
                    style: TextStyle(
                      fontSize: 16,
                      letterSpacing: -0.64,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  Table(
                    columnWidths: {
                      0: FlexColumnWidth(2),
                      1: FlexColumnWidth(1),
                      // 2: FlexColumnWidth(1),
                    },
                    children: fullData(widget.vitalList, context),
                  ),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  List<TableRow> fullData(List<VitalSignResModel> labResultList, context) {
    List<TableRow> tableRow = [];
    tableRow.add(
      TableRow(
        children: [
          Utils.tableColumnTitle(widget.title1),
          Utils.tableColumnTitle(widget.title2),
          // Utils.tableColumnTitle(TranslationBase.of(context).range),
        ],
      ),
    );

    for (int i = 0; i < labResultList.length; i++) {
      var data = labResultList[i].toJson()[widget.viewKey];
      if (data != 0)
        tableRow.add(
          TableRow(
            children: [
              Utils.tableColumnValue(
                  "${projectViewModel.isArabic ? DateUtil.getWeekDayArabic(labResultList[i].vitalSignDate!.weekday) : DateUtil.getWeekDay(labResultList[i].vitalSignDate!.weekday)}, ${labResultList[i].vitalSignDate!.day} ${projectViewModel.isArabic ? DateUtil.getMonthArabic(labResultList[i].vitalSignDate!.month) : DateUtil.getMonth(labResultList[i].vitalSignDate!.month)}, ${labResultList[i].vitalSignDate!.year}",
                  isLast: i == (labResultList.length - 1), mProjectViewModel: projectViewModel),
              Utils.tableColumnValue('${labResultList[i].toJson()[widget.viewKey]}', isLast: i == (labResultList.length - 1), mProjectViewModel: projectViewModel),
            ],
          ),
        );
    }

    return tableRow;
  }

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