import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/model/labs/lab_result.dart';
import 'package:doctor_app_flutter/core/model/labs/patient_lab_orders.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/screens/patients/profile/lab_result/laboratory_result/flow_chart_page.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class LabResultWidget extends StatelessWidget {
  final String filterName;
  final List<LabResult> patientLabResultList;
  final PatientLabOrders patientLabOrder;
  final PatiantInformtion patient;
  final bool isInpatient;

  LabResultWidget({Key? key, required this.filterName, required this.patientLabResultList, required this.patientLabOrder, required this.patient, required this.isInpatient}) : super(key: key);
  late ProjectViewModel projectViewModel;

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);

    return Container(
      width: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          // if (!isInpatient)
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  AppText(
                    filterName,
                    fontSize: SizeConfig.textMultiplier! * 2.0,
                    fontWeight: FontWeight.w700,
                  ),
                ],
              ),
            ],
          ),
          SizedBox(
            height: 8,
          ),
          Row(
            children: [
              Expanded(
                child: Container(
                  child: Center(
                    child: AppText(
                      TranslationBase.of(context).description,
                      color: Colors.black,
                      fontSize: SizeConfig.textMultiplier! * 2.0,
                      fontWeight: FontWeight.w700,
                    ),
                  ),
                ),
              ),
              Expanded(
                child: Container(
                  child: Center(
                    child: AppText(
                      TranslationBase.of(context).value,
                      color: Colors.black,
                      fontSize: SizeConfig.textMultiplier! * 2.0,
                      fontWeight: FontWeight.w700,
                    ),
                  ),
                ),
              ),
              Expanded(
                child: Container(
                  child: Center(
                    child: AppText(
                      TranslationBase.of(context).range,
                      fontSize: SizeConfig.textMultiplier! * 2.0,
                      fontWeight: FontWeight.w700,
                    ),
                  ),
                ),
              ),
              Expanded(
                child: Container(),
              )
            ],
          ),
          Divider(
            color: Colors.grey.shade500,
            thickness: 1,
          ),
          SizedBox(
            height: 12,
          ),
          ListView.builder(
              itemCount: patientLabResultList.length,
              scrollDirection: Axis.vertical,
              physics: ScrollPhysics(),
              shrinkWrap: true,
              itemBuilder: (context, index) {
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      decoration: BoxDecoration(
                        shape: BoxShape.rectangle,
                        color: Color(0X55F2F2F2),
                        borderRadius: BorderRadius.all(
                          Radius.circular(8.0),
                        ),
                      ),
                      child: Row(
                        children: [
                          Expanded(
                            child: Container(
                              padding: EdgeInsets.all(10),
                              child: Center(
                                child: AppText(
                                  patientLabResultList[index].description!,
                                  textAlign: TextAlign.center,
                                  fontSize: SizeConfig.textMultiplier! * 1.8,
                                  isCopyable: true,
                                ),
                              ),
                            ),
                          ),
                          Expanded(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                if (patientLabResultList[index].checkResultStatus() != 0)
                                  Container(
                                    decoration: BoxDecoration(
                                      shape: BoxShape.circle,
                                      color: Colors.red.shade700,
                                    ),
                                    child: Icon(
                                      patientLabResultList[index].checkResultStatus() == 1 ? Icons.arrow_upward : Icons.arrow_downward,
                                      color: Colors.white,
                                      size: 16,
                                    ),
                                  ),
                                Expanded(
                                  child: Container(
                                    alignment: Alignment.center,
                                    padding: EdgeInsets.symmetric(vertical: 10, horizontal: 4),
                                    child: Center(
                                      child: AppText(
                                        patientLabResultList[index].resultValue ?? "" + " " + "${patientLabResultList[index].uOM ?? ""}",
                                        textAlign: TextAlign.center,
                                        isCopyable: true,
                                        fontSize: SizeConfig.textMultiplier! * 1.8,
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                          Expanded(
                            child: Container(
                              padding: EdgeInsets.all(10),
                              // color: Colors.white,
                              child: Center(
                                child: AppText(
                                  patientLabResultList[index].referanceRange!,
                                  textAlign: TextAlign.center,
                                  fontSize: SizeConfig.textMultiplier! * 1.8,
                                  isCopyable: true,
                                ),
                              ),
                            ),
                          ),
                          Expanded(
                            child: Container(
                              child: InkWell(
                                onTap: () {
                                  Navigator.push(
                                    context,
                                    FadePage(
                                      page: FlowChartPage(
                                        filterName: patientLabResultList[index].description!,
                                        patientLabOrder: patientLabOrder,
                                        patient: patient,
                                        isInpatient: isInpatient,
                                      ),
                                    ),
                                  );
                                },
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    AppText(
                                      TranslationBase.of(context).showMoreBtn,
                                      textDecoration: TextDecoration.underline,
                                      color: Colors.red.shade700,
                                      fontSize: SizeConfig.textMultiplier! * 1.8,
                                      fontWeight: FontWeight.w700,
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    SizedBox(
                      height: 16,
                    ),
                  ],
                );
              }),
          SizedBox(
            height: 8,
          ),
          Divider(),
        ],
      ),
    );
  }

  List<TableRow> fullData(List<LabResult> labResultList, context) {
    List<TableRow> tableRow = [];
    tableRow.add(
      TableRow(
        children: [
          Container(
            child: Center(
              child: AppText(
                TranslationBase.of(context).description,
                color: Colors.black,
                bold: true,
              ),
            ),
          ),
          Container(
            child: Center(
              child: AppText(
                TranslationBase.of(context).value,
                color: Colors.black,
                bold: true,
              ),
            ),
          ),
          Container(
            child: Center(
              child: AppText(
                TranslationBase.of(context).range,
                color: Colors.black,
                bold: true,
              ),
            ),
          ),
        ],
      ),
    );
    labResultList.forEach((lab) {
      tableRow.add(
        TableRow(
          children: [
            Container(
              child: Container(
                padding: EdgeInsets.all(10),
                color: Colors.white,
                child: Center(
                  child: AppText(
                    lab.description!,
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
            ),
            Container(
              child: Container(
                padding: EdgeInsets.all(10),
                color: Colors.white,
                child: Center(
                  child: AppText(
                    lab.resultValue! + " " + lab.uOM!,
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
            ),
            Container(
              child: Container(
                padding: EdgeInsets.all(10),
                color: Colors.white,
                child: Center(
                  child: AppText(
                    lab.referanceRange!,
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
            ),
          ],
        ),
      );
    });
    return tableRow;
  }
}
