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/main/util.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'model/delivery_address_model.dart';
import 'delivery_address_row.dart';

class DeliveryAddressList extends StatefulWidget {
  final DeliveryAddress deliveryAddress;

  const DeliveryAddressList({Key key, this.deliveryAddress}) : super(key: key);
  @override
  _DeliveryAddressListState createState() => _DeliveryAddressListState();
}

class _DeliveryAddressListState extends State<DeliveryAddressList> {
  bool _isLoading = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

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

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            leading: new IconButton(
              icon: new Icon(Icons.close),
              onPressed: () => Navigator.pop(context, widget.deliveryAddress),
            ),
            backgroundColor: primaryColor,
            title: LocalText(
              context,
              "delivery_addresses",
              fontSize: 20,
              color: Colors.white,
            ),
          ),
          body: Column(
            children: <Widget>[
              Expanded(
                child: Column(
                  children:
                      getAddressList(context, shipmentModel.deliveryAddresses),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 20, bottom: 15, right: 15),
                child: Align(
                  alignment: Alignment.bottomRight,
                  child: Container(
                    width: 130,
                    height: 40,
                    child: FloatingActionButton.extended(
                      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                      onPressed: () {
                        Navigator.push(
                          context,
                          BottomUpPageRoute(DeliveryAddressEditor()),
                        );
                      },
                      icon: Icon(Icons.add),
                      label: Text(
                        getLocalString(context, 'delivery_address.new_address'),
                        style: TextStyle(fontSize: 12),
                      ),
                      backgroundColor: primaryColor,
                    ),
                  ),
                ),
              ),
            ],
          )),
    );
  }

  List<Widget> getAddressList(
      BuildContext context, List<DeliveryAddress> addresses) {
    return addresses.asMap().entries.map((s) {
      return InkWell(
        onTap: () {
          // Navigator.pop(context, s.value);
        },
        child: DeliveryAddressRow(shippingAddress: s.value, index: s.key),
      );
    }).toList();
  }
}
