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

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

class DeliveryAddressList extends StatefulWidget {
  const DeliveryAddressList({Key key}) : 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(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 Row(
      children: [
        InkWell(
          onTap: () => _select(deliveryAddress),
          child: Padding(
            padding: const EdgeInsets.all(10.0),
            child: Icon(Icons.check,
                color:
                    deliveryAddress.isDefault ? primaryColor : Colors.black26),
          ),
        ),
        Expanded(
          child: DeliveryAddressRow(
              key: ValueKey(deliveryAddress.id),
              deliveryAddress: deliveryAddress,
              selectionCallback: (d) => _edit(context, deliveryAddress)),
        ),
      ],
    );
  }

  _edit(BuildContext context, DeliveryAddress deliveryAddress) {
    Navigator.push(
      context,
      CupertinoPageRoute(
          builder: (context) =>
              DeliveryAddressEditor(deliveryAddress: deliveryAddress)),
    );
  }

  Future<void> _select(DeliveryAddress deliveryAddress) async {
    if (deliveryAddress.isDefault) {
      Navigator.pop(context);
      return;
    }
    setState(() {
      _isLoading = true;
    });
    var deliveryAddressModel =
        Provider.of<DeliveryAddressModel>(context, listen: false);
    try {
      await deliveryAddressModel.selectDefalutDeliveryAddress(deliveryAddress);
      Navigator.pop(context);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
}
