import 'package:diplomaticquarterapp/models/CovidDriveThru/CovidPaymentInfoResponse.dart';
import 'package:diplomaticquarterapp/models/CovidDriveThru/CovidTestProceduresResponse.dart';
import 'package:diplomaticquarterapp/pages/Covid-DriveThru/Covid-TimeSlots.dart';
import 'package:diplomaticquarterapp/services/covid-drivethru/covid-drivethru.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.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:flutter_svg/flutter_svg.dart';

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

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

  @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) {
    return AppScaffold(
      appBarTitle: TranslationBase.of(context).covidTest,
      isShowAppBar: true,
      body: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.fromLTRB(15.0, 15.0, 15.0, 100.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                height: 150.0,
                decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage(
                          "assets/images/new-design/covid-19-big-banner-bg.png"),
                      fit: BoxFit.fill,
                    ),
                    color: Colors.white.withOpacity(0.3),
                    borderRadius: BorderRadius.all(Radius.circular(10))),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      margin:
                          EdgeInsets.only(left: 15.0, right: 15.0, top: 30.0),
                      child: SvgPicture.asset(
                          'assets/images/new-design/covid-19-car.svg',
                          width: 90.0,
                          height: 90.0),
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(
                              left: 20.0, right: 20.0, top: 20.0),
                          child: Text(TranslationBase.of(context).covidTest,
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold,
                                  fontSize: 24.0)),
                        ),
                        Container(
                          margin: EdgeInsets.only(
                              left: 20.0, right: 20.0, top: 10.0),
                          child: Text(TranslationBase.of(context).driveThru,
                              style: TextStyle(
                                  color: Colors.white, fontSize: 24.0)),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              Container(
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10.0),
                    color: Colors.white),
                margin: EdgeInsets.fromLTRB(0.0, 30.0, 0.0, 5.0),
                padding: EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 20.0),
                child: Column(
                  children: <Widget>[
                    Container(
                      alignment: Alignment.center,
                      margin:
                          EdgeInsets.only(left: 0.0, right: 20.0, top: 10.0),
                      child: Text(
                          TranslationBase.of(context).covidSelectProcedure,
                          style: TextStyle(
                              color: Colors.black,
                              fontSize: 20.0,
                              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];
                                    });
                                  },
                                  child: ListTile(
                                    title: Text(
                                        widget.proceduresList[index]
                                            .procedureName,
                                        style: TextStyle(fontSize: 14.0)),
                                    leading: 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);
                                        });
                                      },
                                    ),
                                  ),
                                ),
                              )
                            ],
                          ),
                        ],
                      ),
                    ),
                    Divider(),
                    Container(
                      alignment: Alignment.center,
                      margin:
                          EdgeInsets.only(left: 0.0, right: 20.0, top: 10.0),
                      child: Text(TranslationBase.of(context).testFee,
                          style: TextStyle(
                              color: Colors.black,
                              fontSize: 22.0,
                              fontWeight: FontWeight.bold)),
                    ),
                    Table(
                      children: [
                        TableRow(children: [
                          TableCell(
                              child: _getNormalText(TranslationBase.of(context)
                                  .patientShareToDo)),
                          TableCell(
                              child: _getNormalText(widget
                                  .covidPaymentInfoResponse.patientShareField
                                  .toString())),
                        ]),
                        TableRow(children: [
                          TableCell(
                              child: _getNormalText(
                                  TranslationBase.of(context).patientTaxToDo)),
                          TableCell(
                              child: _getNormalText(widget
                                  .covidPaymentInfoResponse
                                  .patientTaxAmountField
                                  .toString())),
                        ]),
                        TableRow(children: [
                          TableCell(
                              child: _getNormalText(TranslationBase.of(context)
                                  .patientShareTotalToDo)),
                          TableCell(
                              child: _getNormalText(widget
                                  .covidPaymentInfoResponse
                                  .patientShareWithTaxField
                                  .toString())),
                        ]),
                      ],
                    ),
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 5.0),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Checkbox(
                      value: isAgree,
                      onChanged: (value) {
                        setState(() {
                          isAgree = !isAgree;
                        });
                      },
                      activeColor: Color(0xffB8382C),
                    ),
                    Texts(TranslationBase.of(context)
                        .iAgreeToTheTermsAndConditions),
                  ],
                ),
              ),
              Divider(
                color: Colors.grey,
              ),
              Container(
                alignment: Alignment.center,
                margin: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 5.0),
                child: Text(TranslationBase.of(context).payOptions,
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontSize: 16.0,
                        fontWeight: FontWeight.bold,
                        fontFamily: "Open-Sans")),
              ),
              Container(
                alignment: Alignment.center,
                margin: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 5.0),
                child: Image.asset(
                    "assets/images/new-design/payment_options_invoice_confirmation.png",
                    width: 300),
              ),
            ],
          ),
        ),
      ),
      bottomSheet: Container(
        margin: EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 20.0),
        child: Flex(
          direction: Axis.horizontal,
          children: <Widget>[
            Expanded(
              flex: 1,
              child: Container(
                margin: EdgeInsets.fromLTRB(10.0, 0.0, 5.0, 0.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: Colors.grey[500],
                    onPressed: () {
                      cancel();
                    },
                    child: Text(TranslationBase.of(context).cancel,
                        style: TextStyle(fontSize: 18.0)),
                  ),
                ),
              ),
            ),
            Expanded(
              flex: 1,
              child: Container(
                margin: EdgeInsets.fromLTRB(10.0, 0.0, 5.0, 0.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: Colors.grey[500],
                    onPressed: isAgree ? next : null,
                    child: Text(TranslationBase.of(context).next,
                        style: TextStyle(fontSize: 18.0)),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

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

  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)
        .then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (res['MessageStatus'] == 1) {
        setState(() {
          widget.covidPaymentInfoResponse =
              CovidPaymentInfoResponse.fromJson(res['COVID19_PatientShare']);
        });
      } else {}
    }).catchError((err) {
      print(err);
    });
  }

  _getNormalText(text) {
    return Container(
      margin: EdgeInsets.only(top: 20.0, right: 10.0),
      child: Text(text,
          textAlign: TextAlign.end,
          style: TextStyle(
              fontSize: 15,
              fontFamily: 'Open-Sans',
              letterSpacing: 0.5,
              color: Colors.grey[700])),
    );
  }
}
