import 'package:hmg_patient_app/core/service/AuthenticatedUserObject.dart';
import 'package:hmg_patient_app/core/viewModels/pharmacyModule/PrescriptionViewModel.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/locator.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/pages/medical/prescriptions/prescription_items_page.dart';
import 'package:hmg_patient_app/pages/medical/prescriptions/prescriptions_home_page.dart';
import 'package:hmg_patient_app/pages/pharmacies/widgets/home/ViewAllHomeWidget.dart';
import 'package:hmg_patient_app/theme/colors.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/widgets/others/network_base_view.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:provider/provider.dart';


class PrescriptionsWidget extends StatelessWidget {
  AuthenticatedUserObject authenticatedUserObject = locator<AuthenticatedUserObject>();

  @override
  Widget build(BuildContext context) {

    ProjectViewModel projectProvider = Provider.of(context);
    return BaseView<PrescriptionViewModel>(
      onModelReady: (model) async {
        if (Provider.of<ProjectViewModel>(context, listen: false).isLogin) {
          model.getPrescription();
        }
      },
      allowAny: true,
      builder: (_, model, wi) => model.prescriptionsList.length > 0 && authenticatedUserObject.isLogin
          ? NetworkBaseView(
              isLocalLoader: true,
              baseViewModel: model,
              child: Container(
                child: Column(
                  children: [
                    ViewAllHomeWidget(TranslationBase.of(context).myPrescription, HomePrescriptionsPage()),
                    Container(
                      margin: EdgeInsets.only(right: 10.0, left: 10.0),
                      height: MediaQuery.of(context).size.height * 0.30,
                      child: ListView.builder(
                        scrollDirection: Axis.horizontal,
                        shrinkWrap: true,
                        physics: ScrollPhysics(),
                        itemCount: model.prescriptionsList.length,
                        itemBuilder: (context, index) {
                          return InkWell(
                            onTap: () {
                              Navigator.push(
                                context,
                                FadePage(
                                  page: PrescriptionItemsPage(
                                    prescriptions: model.prescriptionsList[index],
                                  ),
                                ),
                              );
                            },
                            child: Container(
                              padding: EdgeInsets.only(left: 8.0, right: 8.0),
                              margin: EdgeInsets.only(right: 5.0, left: 5.0),
                              decoration: BoxDecoration(
                                  border: Border.all(
                                    color: Colors.grey,
                                    style: BorderStyle.solid,
                                    width: 1.0,
                                  ),
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(10.0)),
                              child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
                                Row(
                                  children: <Widget>[
                                    Container(
                                      padding: EdgeInsets.only(
                                        top: 10.0,
                                        left: 10.0,
                                        right: 10.0,
                                        bottom: 10.0,
                                      ),
                                      child: CircleAvatar(
                                        radius: 30,
                                        backgroundColor: Colors.transparent,
                                        child: Image.network(
                                          model.prescriptionsList[index].doctorImageURL!,
                                          width: 50,
                                          height: 50,
                                        ),
                                      ),
                                    ),
                                    Column(
                                      children: [
                                        Container(
                                            padding: EdgeInsets.only(left: 15.0, right: 15.0),
                                            decoration: BoxDecoration(
                                                border: Border.all(
                                                  color: Colors.green,
                                                  style: BorderStyle.solid,
                                                  width: 3.0,
                                                ),
                                                color: Colors.green,
                                                borderRadius: BorderRadius.circular(30.0)),
                                            child: Text(
                                                projectProvider.isArabic
                                                  ? model.prescriptionsList[index].isInOutPatientDescriptionN.toString()
                                                  : model.prescriptionsList[index].isInOutPatientDescription.toString(),
                                              style: TextStyle(
                                                color: Colors.white,
                                                fontSize: 15.0,
                                              ),
                                            )),
                                        Row(children: <Widget>[
                                          Image.asset(
                                            'assets/images/Icon-awesome-calendar.png',
                                            width: 30,
                                            height: 30,
                                          ),
                                          Text(
                                            DateUtil.convertStringToDate(model.prescriptionsList[index].appointmentDate.toString()).toString().substring(0, 10),
                                            style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 15.0,
                                            ),
                                          )
                                        ]),
                                      ],
                                    ),
                                  ],
                                ),
                                Container(
                                  margin: EdgeInsets.only(left: 5),
                                  child: Row(children: <Widget>[
                                    Text(
                                      model.prescriptionsList[index].doctorTitle.toString(),
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 15.0,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    ),
                                    Text(
                                      model.prescriptionsList[index].doctorName.toString(),
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 15.0,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    ),
                                  ]),
                                ),
                                Container(
                                  margin: EdgeInsets.only(left: 5),
                                  child: Text(
                                    model.prescriptionsList[index].clinicDescription.toString(),
                                    style: TextStyle(
                                      color: Colors.green,
                                      fontSize: 15.0,
                                    ),
                                  ),
                                ),
                                Row(children: <Widget>[
                                  Container(
                                    margin: EdgeInsets.only(left: 5),
                                    child:
                                    RatingBar(
                                      initialRating: model.prescriptionsList[index].actualDoctorRate!.toDouble(),
                                      direction: Axis.horizontal,
                                      allowHalfRating: true,
                                      itemCount: 5,
                                      itemSize: 20,
                                      ignoreGestures: true,
                                      ratingWidget: RatingWidget(
                                        full: Icon(Icons.star, color: CustomColors.accentColor,),
                                        half: Icon(Icons.star_half, color: CustomColors.accentColor,),
                                        empty: Icon(Icons.star, color: CustomColors.accentColor,),
                                      ),
                                      unratedColor: Colors.grey[500],
                                      tapOnlyMode: true,
                                      itemPadding: EdgeInsets.symmetric(horizontal: 4.0),
                                      onRatingUpdate: (rating) {
                                        print(rating);
                                      },
                                    )

                                 /*   RatingBar.readOnly(
                                      initialRating: model.prescriptionsList[index].actualDoctorRate.toDouble(),
                                      size: 15.0,
                                      filledColor: Colors.yellow[700],
                                      emptyColor: Colors.grey[500],
                                      isHalfAllowed: true,
                                      halfFilledIcon: Icons.star_half,
                                      filledIcon: Icons.star,
                                      emptyIcon: Icons.star,
                                    ),*/
                                  ),
                                  SizedBox(
                                    width: 130.0,
                                  ),
                                  Container(
                                      child: Icon(
                                    Icons.arrow_forward,
                                    color: Theme.of(context).primaryColor,
                                  )),
                                ]),
                              ]),
                            ),
                          );
                        },
                      ),
                    ),
                  ],
                ),
              ),
            )
          : Container(),
    );
  }
}
