import 'package:charts_flutter/flutter.dart' as charts ;
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/viewModel/dashboard_view_model.dart';
import 'package:doctor_app_flutter/core/model/dashboard/dashboard_model.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/dashboard/guage_chart.dart';
import 'package:doctor_app_flutter/widgets/dashboard/row_count.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:flutter/material.dart';

import 'label.dart';

class DashboardReferralPatient extends StatelessWidget {
  final List<DashboardModel> dashboardItemList;
  final double height;
  final DashboardViewModel? model;

  const DashboardReferralPatient(
      {Key? key, required this.dashboardItemList, required this.height, this.model})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(bottom: 20, top: 10, left: 5, right: 5),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20),
            topRight: Radius.circular(20),
            bottomLeft: Radius.circular(20),
            bottomRight: Radius.circular(20)),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.5),
            spreadRadius: 0,
            blurRadius: 9,
            offset: Offset(0, 0), // changes position of shadow
          ),
        ],
      ),
      child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Expanded(
                flex: 1,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(
                      flex: 4,
                      child: Padding(
                          padding: const EdgeInsets.all(5.0),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Padding(
                                padding: EdgeInsets.all(8),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    SizedBox(
                                        height: SizeConfig.getHeightMultiplier(
                                                height: height) *
                                            (SizeConfig.isHeightVeryShort
                                                ? 3
                                                : SizeConfig.isHeightShort
                                                    ? 2
                                                    : 2)),
                                    Label(
                                      firstLine:
                                          TranslationBase.of(context).patients,
                                      secondLine:
                                          TranslationBase.of(context).referral,
                                      color: Color(0xFF2B353E),
                                      secondLineFontSize:
                                          SizeConfig.getHeightMultiplier(
                                                  height: height) *
                                              (SizeConfig.isHeightVeryShort
                                                  ? 5
                                                  : SizeConfig.isHeightShort
                                                      ? 7
                                                      : 12),
                                    ),
                                    SizedBox(
                                        height: SizeConfig.getHeightMultiplier(
                                                height: height) *
                                            (SizeConfig.isHeightVeryShort
                                                ? 5
                                                : SizeConfig.isHeightShort
                                                    ? 10
                                                    : 5))
                                  ],
                                ),
                              ),
                              Expanded(
                                flex: 1,
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    RowCounts(
                                      dashboardItemList[2]
                                          .summaryoptions![0]
                                          .kPIParameter,
                                      dashboardItemList[2]
                                          .summaryoptions![0]
                                          .value!,
                                      AppGlobal.appTextColor,
                                      height: height,
                                    ),
                                    RowCounts(
                                      dashboardItemList[2]
                                          .summaryoptions![1]
                                          .kPIParameter,
                                      dashboardItemList[2]
                                          .summaryoptions![1]
                                          .value!,
                                      Color(0xFFC8D0DC),
                                      height: height,
                                    ),
                                    RowCounts(
                                      dashboardItemList[2]
                                          .summaryoptions![2]
                                          .kPIParameter,
                                      dashboardItemList[2]
                                          .summaryoptions![2]
                                          .value!,
                                      Color(0xFFEC6666),
                                      height: height,
                                    ),
                                  ],
                                ),
                              )
                            ],
                          )),
                    ),
                    Expanded(
                      flex: 3,
                      child: Stack(children: [
                        Container(
                            padding: EdgeInsets.all(0),
                            child: GaugeChart(
                                _createReferralData(dashboardItemList))),
                        Positioned(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                AppText(
                                  model
                                      !.getPatientCount(dashboardItemList[2])
                                      .toString(),
                                  fontSize: SizeConfig.textMultiplier! * 3.2,
                                  color: AppGlobal.appTextColor,
                                  fontWeight: FontWeight.bold,
                                )
                              ],
                            ),
                            top: height *
                                (SizeConfig.isHeightVeryShort ? 0.35 : 0.35),
                            left: 0,
                            right: 0)
                      ]),
                    ),
                  ],
                )),
          ]),
    );
  }

  static List<charts.Series<GaugeSegment, String>> _createReferralData(
      List<DashboardModel> dashboardItemList) {
    final data = [
      new GaugeSegment(
          dashboardItemList[2].summaryoptions![0].kPIParameter!,
          getValue(dashboardItemList[1].summaryoptions![0].value),
          charts.ColorUtil.fromDartColor(AppGlobal.appTextColor)),
      new GaugeSegment(
        dashboardItemList[2].summaryoptions![1].kPIParameter!,
        getValue(dashboardItemList[1].summaryoptions![1].value),
        charts.ColorUtil.fromDartColor(
          Color(0xFFC6CEDA),
        ),
      ),
      new GaugeSegment(
        dashboardItemList[2].summaryoptions![2].kPIParameter!,
        getValue(dashboardItemList[1].summaryoptions![2].value),
        charts.ColorUtil.fromDartColor(
          Color(0xFFEC6666),
        ),
      ),
    ];

    return [
      new charts.Series<GaugeSegment, String>(
        id: 'Segments',
        domainFn: (GaugeSegment segment, _) => segment.segment,
        measureFn: (GaugeSegment segment, _) => segment.size,
        strokeWidthPxFn: (GaugeSegment segment, _) => 200,
        data: data,
       // colorFn: (GaugeSegment segment, _) => segment.,
      )
    ];
  }

  static int getValue(value) {
    return value == 0 ? 1 : value;
  }
}
