import 'package:fcs/domain/entities/user.dart';
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/local_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class DeliveryAddressSelection extends StatefulWidget {
  final DeliveryAddress? deliveryAddress;
  final User? user;

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

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

class _DeliveryAddressSelectionState extends State<DeliveryAddressSelection> {
  List<DeliveryAddress> _deliveryAddresses = [];

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

  _getDeliverAddresses() async {
    var addressModel =
        Provider.of<DeliveryAddressModel>(context, listen: false);

    var deliveryAddresses =
        await addressModel.getDeliveryAddresses(widget.user!.id!);
    setState(() {
      this._deliveryAddresses = deliveryAddresses;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (widget.user != null) {}
    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: () async {
            bool? updated = await Navigator.of(context).push(CupertinoPageRoute(
                builder: (context) =>
                    DeliveryAddressEditor(user: widget.user)));
            if (updated ?? false) {
              _getDeliverAddresses();
            }
          },
          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: _deliveryAddresses.length,
              itemBuilder: (context, index) {
                return _row(context, _deliveryAddresses[index]);
              }),
        ));
  }

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

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

  _edit(BuildContext context, DeliveryAddress deliveryAddress) async {
    bool? updated = await Navigator.of(context).push(CupertinoPageRoute(
        builder: (context) => DeliveryAddressEditor(
            user: widget.user, deliveryAddress: deliveryAddress)));
    if (updated ?? false) {
      _getDeliverAddresses();
    }
  }
}
