import 'package:diplomaticquarterapp/core/enum/OrderService.dart';
import 'package:diplomaticquarterapp/core/enum/OrderService.dart';
import 'package:diplomaticquarterapp/core/enum/OrderService.dart';
import 'package:diplomaticquarterapp/core/enum/OrderService.dart';
import 'package:diplomaticquarterapp/core/model/er/PatientER.dart';
import 'package:diplomaticquarterapp/core/model/er/get_all_transportation_method_list_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/er/am_request_view_model.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

enum Direction { ToHospital, FromHospital }
enum Way { OneWay, TwoWays }

class SelectTransportationMethod extends StatefulWidget {
  final Function changeCurrentTab;
  final PatientER patientER;
  final AmRequestViewModel amRequestViewModel;

  SelectTransportationMethod(
      {Key key,
      this.changeCurrentTab,
      this.patientER,
      this.amRequestViewModel});

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

class _SelectTransportationMethodState
    extends State<SelectTransportationMethod> {
  PatientERTransportationMethod _erTransportationMethod =
      PatientERTransportationMethod();
  Direction _direction = Direction.FromHospital;
  Way _way = Way.OneWay;

  OrderService _orderService = OrderService.AMBULANCE;

  @override
  void initState() {
    super.initState();
    if (widget.patientER.direction != null) {
      _direction = widget.patientER.direction == 0
          ? Direction.ToHospital
          : Direction.FromHospital;
      _way = widget.patientER.tripType == 1 ? Way.OneWay : Way.TwoWays;
      _erTransportationMethod = widget.amRequestViewModel
          .amRequestModeList[(widget.patientER.selectedAmbulate - 1)];
    } else {
      if (widget.amRequestViewModel.amRequestModeList.length != 0)
        _erTransportationMethod = widget.amRequestViewModel.amRequestModeList[
            widget.amRequestViewModel.amRequestModeList.length - 1];
    }
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      physics: BouncingScrollPhysics(),
      child: Container(
        margin: EdgeInsets.only(left: 12, right: 12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 12,
            ),
            Texts(TranslationBase.of(context).transportHeading),
            ...List.generate(
              widget.amRequestViewModel.amRequestModeList.length,
              (index) => InkWell(
                onTap: () {
                  setState(() {
                    _erTransportationMethod =
                        widget.amRequestViewModel.amRequestModeList[index];
                  });
                },
                child: Container(
                  margin: EdgeInsets.all(5),
                  decoration: BoxDecoration(
                    shape: BoxShape.rectangle,
                    borderRadius: BorderRadius.circular(8),
                    border: Border.all(color: Colors.grey, width: 0.5),
                    color: Colors.white,
                  ),
                  child: Row(
                    children: [
                      Expanded(
                        flex: 3,
                        child: ListTile(
                          title: Text(widget.amRequestViewModel
                              .amRequestModeList[index].title),
                          leading: Radio(
                            value: widget
                                .amRequestViewModel.amRequestModeList[index],
                            groupValue: _erTransportationMethod,
                            activeColor: Theme.of(context).primaryColor,
                            onChanged: (value) {
                              setState(() {
                                _erTransportationMethod = value;
                              });
                            },
                          ),
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: Texts(
                            TranslationBase.of(context).sar+' ${widget.amRequestViewModel.amRequestModeList[index].price}'),
                      )
                    ],
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 12,
            ),
            Texts(TranslationBase.of(context).directionHeading),
            SizedBox(
              height: 5,
            ),
            Container(
              width: double.maxFinite,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Expanded(
                    child: InkWell(
                      onTap: () {
                        setState(() {
                          _direction = Direction.ToHospital;
                        });
                      },
                      child: Container(
                        width: double.maxFinite,
                        decoration: BoxDecoration(
                          shape: BoxShape.rectangle,
                          borderRadius: BorderRadius.circular(8),
                          border: Border.all(color: Colors.grey, width: 0.5),
                          color: Colors.white,
                        ),
                        child: ListTile(
                          title: Text(TranslationBase.of(context).toHospital),
                          leading: Radio(
                            value: Direction.ToHospital,
                            groupValue: _direction,
                            activeColor: Theme.of(context).primaryColor,
                            onChanged: (value) {
                              setState(() {
                                _direction = value;
                              });
                            },
                          ),
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                    child: InkWell(
                      onTap: () {
                        setState(() {
                          _direction = Direction.FromHospital;
                        });
                      },
                      child: Container(
                        width: double.maxFinite,
                        decoration: BoxDecoration(
                          shape: BoxShape.rectangle,
                          borderRadius: BorderRadius.circular(8),
                          border: Border.all(color: Colors.grey, width: 0.5),
                          color: Colors.white,
                        ),
                        child: ListTile(
                          title: Text(TranslationBase.of(context).fromHospital),
                          leading: Radio(
                            value: Direction.FromHospital,
                            groupValue: _direction,
                            activeColor: Theme.of(context).primaryColor,
                            onChanged: (value) {
                              setState(() {
                                _direction = value;
                              });
                            },
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            if (_direction == Direction.ToHospital)
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    height: 8,
                  ),
                  Texts(TranslationBase.of(context).directionHeading),
                  SizedBox(
                    height: 5,
                  ),
                  Row(
                    children: [
                      Expanded(
                        child: InkWell(
                          onTap: () {
                            setState(() {
                              _way = Way.OneWay;
                            });
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              shape: BoxShape.rectangle,
                              borderRadius: BorderRadius.circular(8),
                              border:
                                  Border.all(color: Colors.grey, width: 0.5),
                              color: Colors.white,
                            ),
                            child: ListTile(
                              title: Text(TranslationBase.of(context).oneDirec),
                              leading: Radio(
                                value: Way.OneWay,
                                groupValue: _way,
                                activeColor:Theme.of(context).primaryColor,
                                onChanged: (value) {
                                  setState(() {
                                    _way = value;
                                  });
                                },
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: InkWell(
                          onTap: () {
                            setState(() {
                              _way = Way.TwoWays;
                            });
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              shape: BoxShape.rectangle,
                              borderRadius: BorderRadius.circular(8),
                              border:
                                  Border.all(color: Colors.grey, width: 0.5),
                              color: Colors.white,
                            ),
                            child: ListTile(
                              title: Text(TranslationBase.of(context).twoDirec),
                              leading: Radio(
                                value: Way.TwoWays,
                                groupValue: _way,
                                activeColor: Theme.of(context).primaryColor,
                                onChanged: (value) {
                                  setState(() {
                                    _way = value;
                                  });
                                },
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            SizedBox(
              height: 15,
            ),
            Container(
              padding: EdgeInsets.all(15),
              width: double.maxFinite,
              height: 76,
              child: SecondaryButton(
                color: Colors.grey[800],
                textColor: Colors.white,
                onTap: () {
                  setState(() {
                    widget.patientER.transportationMethodId =(widget.amRequestViewModel.amRequestModeList.indexOf(_erTransportationMethod) + 1);
                    widget.patientER.direction = _direction == Direction.ToHospital ? 1 : 0;
                    widget.patientER.tripType = _way == Way.TwoWays ? 0 : 1;
                    widget.patientER.selectedAmbulate = (widget.amRequestViewModel.amRequestModeList.indexOf(_erTransportationMethod) + 1);
                    widget.patientER.patientERTransportationMethod = _erTransportationMethod;
                    widget.patientER.orderServiceID = _orderService.getIdOrderService();
                    widget.patientER.pickupUrgency = 1;
                    widget.patientER.lineItemNo = 1;
                    widget.patientER.cost = _erTransportationMethod.price;
                    widget.patientER.vAT = _erTransportationMethod.vAT ?? 0;
                    widget.patientER.totalPrice = _erTransportationMethod.totalPrice;
                    widget.changeCurrentTab(1);
                  });
                },
                label: TranslationBase.of(context).next,
              ),
            )
          ],
        ),
      ),
    );
  }
}
