import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/models/CovidDriveThru/CovidPaymentInfoResponse.dart';
import 'package:diplomaticquarterapp/models/CovidDriveThru/CovidTestProceduresResponse.dart';
import 'package:diplomaticquarterapp/models/CovidDriveThru/DriveThroughTestingCenterModel.dart';
import 'package:diplomaticquarterapp/pages/Covid-DriveThru/Covid-TimeSlots.dart';
import 'package:diplomaticquarterapp/services/covid-drivethru/covid-drivethru.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/widgets/buttons/custom_text_button.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';

class CovidPaymentDetails extends StatefulWidget {
  CovidPaymentInfoResponse covidPaymentInfoResponse;
  int projectID;
  List<CovidTestProceduresResponse> proceduresList;
  CovidTestProceduresResponse selectedProcedure;
  DriveThroughTestingCenterModel selectedProject;

  CovidPaymentDetails({@required this.covidPaymentInfoResponse, @required this.projectID, @required this.proceduresList, @required this.selectedProject});

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

class _CovidPaymentDetailsState extends State<CovidPaymentDetails> {
  bool isAgree = false;

  @override
  void initState() {
    widget.selectedProcedure = widget.proceduresList[0];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return AppScaffold(
      appBarTitle: TranslationBase.of(context).covidTest,
      isShowAppBar: true,
      showNewAppBarTitle: true,
      showNewAppBar: true,
      backgroundColor: CustomColors.appBackgroudGrey2Color,
      body: Container(
        // margin: EdgeInsets.fromLTRB(15.0, 15.0, 15.0, 100.0),

        child: Column(
          children: [
            Expanded(
              child: Padding(
                padding: EdgeInsets.all(16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(TranslationBase.of(context).covidSelectProcedure, style: TextStyle(color: Colors.black, fontSize: 16.0, letterSpacing: -0.64, fontWeight: FontWeight.bold)),
                    ...List.generate(
                      widget.proceduresList.length,
                      (index) => Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(
                            height: 1,
                          ),
                          Row(
                            children: <Widget>[
                              Expanded(
                                flex: 1,
                                child: InkWell(
                                  onTap: () {
                                    setState(() {
                                      widget.selectedProcedure = widget.proceduresList[index];
                                      getPaymentInfo(context, widget.projectID.toString(), widget.selectedProcedure.procedureID);
                                    });
                                  },
                                  child: Row(
                                    children: [
                                      Radio(
                                        value: widget.proceduresList[index],
                                        groupValue: widget.selectedProcedure,
                                        activeColor: Colors.red[800],
                                        toggleable: true,
                                        onChanged: (value) {
                                          setState(() {
                                            widget.selectedProcedure = value;
                                            print(widget.selectedProcedure.procedureName);
                                            getPaymentInfo(context, widget.projectID.toString(), widget.selectedProcedure.procedureID);
                                          });
                                        },
                                      ),
                                      Text(
                                        projectViewModel.isArabic ? widget.proceduresList[index].procedureNameN : widget.proceduresList[index].procedureName,
                                        style: TextStyle(
                                          fontSize: 12.0,
                                          letterSpacing: -0.48,
                                          fontWeight: FontWeight.w600,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              )
                            ],
                          ),
                        ],
                      ),
                    ),
                    Container(
                      decoration: cardRadius(12),
                      child: Padding(
                        padding: const EdgeInsets.all(12.0),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(TranslationBase.of(context).testFee,
                                style: TextStyle(
                                  color: Colors.black,
                                  fontSize: 16.0,
                                  fontWeight: FontWeight.w600,
                                  letterSpacing: -0.64,
                                )),
                            Container(
                              width: double.infinity,
                              padding: EdgeInsets.only(top: 10, bottom: 3),
                              child: Row(
                                children: [
                                  Expanded(
                                    child: _getNormalText(TranslationBase.of(context).patientShareToDo),
                                  ),
                                  Expanded(
                                    child: _getNormalText(widget.covidPaymentInfoResponse.patientShareField.toString(), isBold: true),
                                  )
                                ],
                              ),
                            ),
                            mDivider(Colors.grey[200]),
                            Container(
                              width: double.infinity,
                              padding: EdgeInsets.only(top: 3, bottom: 3),
                              child: Row(
                                children: [
                                  Expanded(
                                    child: _getNormalText(TranslationBase.of(context).patientTaxToDo),
                                  ),
                                  Expanded(
                                    child: _getNormalText(widget.covidPaymentInfoResponse.patientTaxAmountField.toString(), isBold: true),
                                  )
                                ],
                              ),
                            ),
                            mDivider(Colors.grey[200]),
                            Container(
                              width: double.infinity,
                              padding: EdgeInsets.only(top: 3, bottom: 3),
                              child: Row(
                                children: [
                                  Expanded(
                                    child: _getNormalText(TranslationBase.of(context).patientShareTotalToDo),
                                  ),
                                  Expanded(
                                    child: _getNormalText(widget.covidPaymentInfoResponse.patientShareWithTaxField.toString(), isBold: true),
                                  )
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Checkbox(
                            value: isAgree,
                            onChanged: (value) {
                              setState(() {
                                isAgree = !isAgree;
                              });
                            },
                            activeColor: Color(0xffB8382C),
                          ),
                          Text(
                            TranslationBase.of(context).agreeTo,
                            style: TextStyle(
                              fontSize: 12,
                              letterSpacing: -0.48,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                          mWidth(3),
                          InkWell(
                            onTap: () {
                              launch("https://hmg.com/en/Pages/Privacy.aspx");
                            },
                            child: Text(
                              TranslationBase.of(context).termsConditoins,
                              style: TextStyle(
                                fontSize: 12,
                                letterSpacing: -0.48,
                                color: CustomColors.accentColor,
                                fontWeight: FontWeight.w600,
                                decoration: TextDecoration.underline,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    mFlex(1),
                    Text(
                      TranslationBase.of(context).payOptions,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 12.0,
                        fontWeight: FontWeight.w600,
                        letterSpacing: -0.46,
                      ),
                    ),
                    Container(margin: EdgeInsets.fromLTRB(20.0, 5.0, 20.0, 5.0), child: getPaymentMethods()),
                  ],
                ),
              ),
            ),
            Container(
              width: double.infinity,
              child: Card(
                elevation: 0,
                margin: EdgeInsets.zero,
                child: Padding(
                  padding: const EdgeInsets.only(top: 12, bottom: 12, left: 12, right: 12),
                  child: Row(
                    children: [
                      Expanded(
                        flex: 1,
                        child: Container(
                          child: ButtonTheme(
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(10.0),
                            ),
                            minWidth: MediaQuery.of(context).size.width * 0.7,
                            height: 45.0,
                            child: CustomTextButton(
                              backgroundColor: CustomColors.accentColor,
                              disabledForegroundColor: Colors.grey[500].withOpacity(0.38),
                              disabledBackgroundColor: Colors.grey[500].withOpacity(0.12),
                              elevation: 0,
                              onPressed: () {
                                cancel();
                              },
                              child: Text(
                                TranslationBase.of(context).cancel_nocaps,
                                style: TextStyle(
                                  fontSize: 16.0,
                                  letterSpacing: -0.48,
                                  color: Colors.white,
                                  fontWeight: FontWeight.w600,
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                      mWidth(12),
                      Expanded(
                        flex: 1,
                        child: Container(
                          child: ButtonTheme(
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(10.0),
                            ),
                            minWidth: MediaQuery.of(context).size.width * 0.7,
                            height: 45.0,
                            child: CustomTextButton(
                              backgroundColor: CustomColors.green,
                              disabledForegroundColor: Colors.grey[500].withOpacity(0.38),
                              disabledBackgroundColor: Colors.grey[500].withOpacity(0.12),
                              elevation: 0,
                              onPressed: isAgree ? next : null,
                              child: Text(
                                TranslationBase.of(context).next,
                                style: TextStyle(fontSize: 16.0, letterSpacing: -0.48, fontWeight: FontWeight.w600, color: Colors.white),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  void next() {
    Navigator.push(context, FadePage(page: CovidTimeSlots(projectID: widget.projectID, selectedProcedure: widget.selectedProcedure, selectedProject: widget.selectedProject)));
  }

  cancel() {
    Navigator.pop(context);
  }

  getPaymentInfo(BuildContext context, String projectID, String selectedProcedureID) {
    CovidDriveThruService service = new CovidDriveThruService();
    GifLoaderDialogUtils.showMyDialog(context);
    service.getCovidPaymentInformation(context, int.parse(projectID), selectedProcedureID, widget.selectedProject.testTypeEnum, widget.selectedProject.testProcedureEnum).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (res['MessageStatus'] == 1) {
        setState(() {
          widget.covidPaymentInfoResponse = CovidPaymentInfoResponse.fromJson(res['COVID19_PatientShare']);
        });
      } else {}
    }).catchError((err) {
      print(err);
    });
  }

  _getNormalText(text, {bool isBold = false}) {
    return Text(
      text,
      style: TextStyle(
        fontSize: isBold ? 12 : 10,
        letterSpacing: -0.5,
        color: isBold ? Colors.black : Colors.grey[700],
        fontWeight: FontWeight.w600,
      ),
    );
  }
}
