import 'package:fcs/domain/vo/delivery_address.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/delivery_address/delivery_address_editor.dart';
import 'package:fcs/pages/delivery_address/delivery_address_row.dart';
import 'package:fcs/pages/delivery_address/model/delivery_address_model.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class DeliveryAddressSelection extends StatelessWidget {
  final DeliveryAddress deliveryAddress;

  const DeliveryAddressSelection({Key key, this.deliveryAddress})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    var shipmentModel = Provider.of<DeliveryAddressModel>(context);

    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(CupertinoIcons.back, color: primaryColor),
            onPressed: () => Navigator.of(context).pop(),
          ),
          backgroundColor: Colors.white,
          shadowColor: Colors.transparent,
          title: LocalText(context, 'delivery_addresses',
              color: primaryColor, fontSize: 20),
        ),
        floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            Navigator.of(context).push(CupertinoPageRoute(
                builder: (context) => DeliveryAddressEditor()));
          },
          icon: Icon(Icons.add),
          label: LocalText(context, "delivery_address.new_address",
              color: Colors.white),
          backgroundColor: primaryColor,
        ),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: ListView.separated(
              separatorBuilder: (c, i) => Divider(
                    color: primaryColor,
                  ),
              itemCount: shipmentModel.deliveryAddresses.length,
              itemBuilder: (context, index) {
                return _row(context, shipmentModel.deliveryAddresses[index]);
              }),
        ));
  }

  _row(BuildContext context, DeliveryAddress _deliveryAddress) {
    return InkWell(
      onTap: () => _select(context, _deliveryAddress),
      child: Row(
        children: [
          Padding(
            padding: const EdgeInsets.all(10.0),
            child: Icon(Icons.check,
                color: deliveryAddress != null &&
                        _deliveryAddress.id == deliveryAddress.id
                    ? primaryColor
                    : Colors.black26),
          ),
          Expanded(
              child: DeliveryAddressRow(
            key: ValueKey(_deliveryAddress.id),
            deliveryAddress: _deliveryAddress,
          )),
        ],
      ),
    );
  }

  _select(BuildContext context, DeliveryAddress _deliveryAddress) {
    Navigator.pop(context, _deliveryAddress);
  }
}
