import 'package:diplomaticquarterapp/models/Appointments/AppoimentAllHistoryResultList.dart';
import 'package:diplomaticquarterapp/models/Appointments/DoctorListResponse.dart';
import 'package:diplomaticquarterapp/pages/BookAppointment/BookConfirm.dart';
import 'package:diplomaticquarterapp/pages/BookAppointment/components/DocAvailableAppointments.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';
import 'package:rating_bar/rating_bar.dart';

import 'widgets/AppointmentActions.dart';

class AppointmentDetails extends StatefulWidget {
  AppoitmentAllHistoryResultList appo;
  static bool isLoading = false;

  static bool showFooterButton = false;

  AppointmentDetails({@required this.appo});

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

class _AppointmentDetailsState extends State<AppointmentDetails>
    with SingleTickerProviderStateMixin {
  static TabController _tabController;

  @override
  void initState() {
    _tabController = new TabController(length: 2, vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      appBarTitle: widget.appo.doctorNameObj,
      isShowAppBar: true,
      bottomSheet: AppointmentDetails.showFooterButton
          ? Container(
        width: MediaQuery.of(context).size.width,
        height: 50.0,
        margin: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
        child: ButtonTheme(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10.0),
          ),
          minWidth: MediaQuery.of(context).size.width * 0.7,
          height: 45.0,
          child: RaisedButton(
            color: new Color(0xFF60686b),
            textColor: Colors.white,
            disabledTextColor: Colors.white,
            disabledColor: new Color(0xFFbcc2c4),
            onPressed: goToBookConfirm,
            child: Text(TranslationBase.of(context).bookNow,
                style: TextStyle(fontSize: 18.0)),
          ),
        ),
      )
          : null,
      body: Container(
        color: new Color(0xFFf6f6f6),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.only(top: 20.0),
                      alignment: Alignment.center,
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(100.0),
                        child: Image.network(widget.appo.doctorImageURL,
                            fit: BoxFit.fill, height: 120.0, width: 120.0),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 10.0, left: 10.0, right: 10.0),
                      alignment: Alignment.center,
                      child: Text(
                          widget.appo.doctorTitle +
                              " " +
                              widget.appo.doctorNameObj,
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              fontSize: 20.0,
                              color: Colors.grey[900],
                              letterSpacing: 1.0)),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 10.0),
                      alignment: Alignment.center,
                      child: Text(widget.appo.clinicName,
                          style: TextStyle(
                              fontSize: 12.0,
                              color: Colors.grey[900],
                              letterSpacing: 1.0)),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 5.0),
                      alignment: Alignment.center,
                      child: RatingBar.readOnly(
                        initialRating: widget.appo.actualDoctorRate.toDouble(),
                        size: 35.0,
                        filledColor: Colors.yellow[700],
                        emptyColor: Colors.grey[500],
                        isHalfAllowed: true,
                        halfFilledIcon: Icons.star_half,
                        filledIcon: Icons.star,
                        emptyIcon: Icons.star,
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 5.0),
                      alignment: Alignment.center,
                      child: Text(
                          "(" +
                              widget.appo.noOfPatientsRate.toString() +
                              " Reviews)",
                          style: TextStyle(
                            fontSize: 14.0,
                            color: Colors.blue[800],
                            letterSpacing: 1.0,
                            decoration: TextDecoration.underline,
                          )),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 10.0),
                      child: Divider(
                        color: Colors.grey[500],
                      ),
                    ),
                    TabBar(
                      onTap: (index) {
                        setState(() {
                          index == 1
                              ? AppointmentDetails.showFooterButton = true
                              : AppointmentDetails.showFooterButton = false;
                          print(AppointmentDetails.showFooterButton);
                        });
                      },
                      tabs: [
                        Tab(
                            child: Text(TranslationBase.of(context).appoActions,
                                style: TextStyle(color: Colors.black))),
                        Tab(
                          child: Text(TranslationBase.of(context).availableAppo,
                              style: TextStyle(color: Colors.black)),
                        )
                      ],
                      controller: _tabController,
                    ),
                  ],
                ),
              ),
              Container(
                height: MediaQuery.of(context).size.height * 0.87,
                child: TabBarView(
                  physics: NeverScrollableScrollPhysics(),
                  children: [
                    AppointmentActions(appo: widget.appo, tabController: _tabController, enableFooterButton: enableFooterButton),
                    DocAvailableAppointments(doctor: getDoctorObject())
                  ],
                  controller: _tabController,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void enableFooterButton() {
    setState(() {
      AppointmentDetails.showFooterButton = true;
    });
  }

  void goToBookConfirm() {
    if (DocAvailableAppointments.areSlotsAvailable)
      navigateToBookConfirm(context);
    else
      AppToast.showErrorToast(message: "Please select Time Slot to continue");
  }

  Future navigateToBookConfirm(context) async {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => BookConfirm(
                doctor: getDoctorObject(),
                selectedDate: DocAvailableAppointments.selectedDate,
                selectedTime: DocAvailableAppointments.selectedTime)));
  }



  DoctorList getDoctorObject() {
    DoctorList docObj = new DoctorList();
    docObj.doctorID = widget.appo.doctorID;
    docObj.clinicID = widget.appo.clinicID;
    docObj.projectID = widget.appo.projectID;
    docObj.doctorTitle = widget.appo.doctorTitle;
    docObj.name = widget.appo.doctorNameObj;
    docObj.clinicName = widget.appo.clinicName;
    docObj.speciality = widget.appo.doctorSpeciality;
    docObj.actualDoctorRate = widget.appo.actualDoctorRate;
    docObj.projectName = widget.appo.projectName;
    docObj.originalClinicID = widget.appo.originalClinicID;
    docObj.doctorImageURL = widget.appo.doctorImageURL;
    return docObj;
  }
}
