import 'package:hmg_patient_app/core/service/AlHabibMedicalService/health-calculator/bariatrics-service.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/models/Appointments/DoctorListResponse.dart';
import 'package:hmg_patient_app/pages/BookAppointment/SearchResults.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.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/buttons/button.dart';
import 'package:hmg_patient_app/widgets/buttons/secondary_button.dart';
import 'package:hmg_patient_app/widgets/data_display/text.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:provider/provider.dart';

class CarbsResult extends StatelessWidget {
  double pCal;
  double cCal;
  double fCal;
  double pCalGram;
  double cCalGram;
  double fCalGram;
  double pCalMeal;
  double cCalMeal;
  double fCalMeal;

  CarbsResult({required this.pCal, required this.cCal, required this.fCal, required this.pCalGram, required this.cCalGram, required this.fCalGram, required this.fCalMeal, required this.cCalMeal, required this.pCalMeal});

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return AppScaffold(
      isShowAppBar: true,
      isShowDecPage: false,
      showNewAppBarTitle: true,
      showNewAppBar: true,
      appBarTitle: TranslationBase.of(context).carbohydrate,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            decoration: containerRadius(Colors.white, 12),
            padding: EdgeInsets.all(12),
            margin: EdgeInsets.all(20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  TranslationBase.of(context).carbsProtin,
                  style: TextStyle(
                    fontSize: 19,
                    fontWeight: FontWeight.bold,
                    letterSpacing: -1.34,
                  ),
                ),
                mHeight(12),
                Table(
                  columnWidths: {
                    0: FlexColumnWidth(2.4),
                    1: FlexColumnWidth(1.8),
                    2: FlexColumnWidth(2),
                    3: FlexColumnWidth(1),
                  },
                  children: fullData(context, projectViewModel),
                ),
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
            color: Colors.white,
            child: SecondaryButton(
              label: TranslationBase.of(context).seeListOfDoctor,
              color: CustomColors.accentColor,
              onTap: () {
                getDoctorsList(context);
              },
            ),
          ),

        ],
      ),
    );
  }

  List<TableRow> fullData(BuildContext context, ProjectViewModel projectViewModel) {
    List<TableRow> tableRow = [];
    tableRow.add(
      TableRow(
        children: [
          Utils.tableColumnTitle(TranslationBase.of(context).description),
          Utils.tableColumnTitle(TranslationBase.of(context).Protein),
          Utils.tableColumnTitle(TranslationBase.of(context).carbohydrate),
          Utils.tableColumnTitle(TranslationBase.of(context).fat),
        ],
      ),
    );

    tableRow.add(
      TableRow(
        children: [
          Utils.tableColumnValue(TranslationBase.of(context).calDay, isCapitable: false, mProjectViewModel: projectViewModel),
          Utils.tableColumnValue(pCal.ceil().toString() + ' '+TranslationBase.of(context).Cals, isCapitable: false, mProjectViewModel: projectViewModel),
          Utils.tableColumnValue(cCal.ceil().toString() + ' '+TranslationBase.of(context).Cals, isCapitable: false, mProjectViewModel: projectViewModel),
          Utils.tableColumnValue(cCal.ceil().toString() + ' '+TranslationBase.of(context).Cals, isCapitable: false, mProjectViewModel: projectViewModel),
        ],
      ),
    );

    tableRow.add(
      TableRow(
        children: [
          Utils.tableColumnValue(TranslationBase.of(context).gramsPerDay, isCapitable: false, mProjectViewModel: projectViewModel),
          Utils.tableColumnValue(pCalGram.ceil().toString() + ' '+TranslationBase.of(context).gr, isCapitable: false, mProjectViewModel: projectViewModel),
          Utils.tableColumnValue(cCalGram.ceil().toString() + ' '+TranslationBase.of(context).gr, isCapitable: false, mProjectViewModel: projectViewModel),
          Utils.tableColumnValue(fCalGram.ceil().toString() + ' '+TranslationBase.of(context).gr, isCapitable: false, mProjectViewModel: projectViewModel),
        ],
      ),
    );
    tableRow.add(
      TableRow(
        children: [
          Utils.tableColumnValue(TranslationBase.of(context).gramsPerMeal, isCapitable: false, mProjectViewModel: projectViewModel),
          Utils.tableColumnValue(pCalMeal.ceil().toString() + ' '+TranslationBase.of(context).gr, isCapitable: false, mProjectViewModel: projectViewModel),
          Utils.tableColumnValue(cCalMeal.ceil().toString() + ' '+TranslationBase.of(context).gr, isCapitable: false, mProjectViewModel: projectViewModel),
          Utils.tableColumnValue(fCalMeal.ceil().toString() + ' '+TranslationBase.of(context).gr, isCapitable: false, mProjectViewModel: projectViewModel),
        ],
      ),
    );
    return tableRow;
  }

  getDoctorsList(BuildContext context) {
    GifLoaderDialogUtils.showMyDialog(context);
    BariatricsService service = new BariatricsService();

    List<DoctorList> doctorsList = [];
    List<PatientDoctorAppointmentList> _patientDoctorAppointmentListHospital =[];

    service.getCalculationDoctors(calculationID: 11).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (res['MessageStatus'] == 1) {
        if (res['List_CalculationTable'].length != 0) {
          res['List_CalculationTable'].forEach((item) {
            DoctorList docList = new DoctorList();
            docList.clinicID = item['ClinicID'];
            docList.clinicName = item['ClinicName'];
            docList.doctorID = item['DoctorID'];
            docList.doctorImageURL = item['DoctorImageURL'];
            docList.name = item['DoctorName'];
            docList.doctorTitle = item['DoctorTitle'];
            docList.gender = item['Gender'];
            docList.genderDescription = item['GenderDescription'];
            docList.nationalityName = item['Nationality'];
            docList.nationalityFlagURL = item['NationalityFlagURL'];
            docList.projectID = item['ProjectID'];
            docList.projectName = item['ProjectName'];
            docList.actualDoctorRate = 0;
            doctorsList.add(docList);
          });

          doctorsList.forEach((element) {
            List<PatientDoctorAppointmentList> doctorByHospital = _patientDoctorAppointmentListHospital
                .where(
                  (elementClinic) => elementClinic.filterName == element.projectName,
                )
                .toList();

            if (doctorByHospital.length != 0) {
              _patientDoctorAppointmentListHospital[_patientDoctorAppointmentListHospital.indexOf(doctorByHospital[0])].patientDoctorAppointmentList!.add(element);
            } else {
              _patientDoctorAppointmentListHospital.add(PatientDoctorAppointmentList(filterName: element.projectName, distanceInKMs: "0", patientDoctorAppointment: element));
            }
          });

          Navigator.push(context, FadePage(page: SearchResults(isLiveCareAppointment: false, doctorsList: doctorsList, patientDoctorAppointmentListHospital: _patientDoctorAppointmentListHospital, isDoctorSearchResult: false,)));
        }
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }
}
