
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:twong/api/index.dart';
import 'package:twong/config/style.dart';
import 'package:twong/models/address.dart';
import 'package:twong/providers/address.dart';
import 'package:twong/router/index.dart';
import 'package:twong/utils/index.dart';
import 'package:twong/widgets/address_item.dart';

class AddressBar extends StatefulWidget {
  final Address address;
  final Function(Address) onSelected;
  AddressBar({ this.onSelected, this.address });

  @override
  State<StatefulWidget> createState() {
    return _AddressState();
  }
}

class _AddressState extends State<AddressBar> {
  Address _address;

  @override
  void initState() {
    this._address = widget.address;
    super.initState();


    Network.inst.getAddressList().then((res) {
      Provider.of<AddressModel>(context, listen: false).set(res);
    });
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(child: Container(
      height: 380.px,
      child: ListView(
        physics: ClampingScrollPhysics(),
        children: [
          Stack(
            children: [
              Container(
                  width: double.infinity,
                  margin: EdgeInsets.only(top: 6.px, bottom: 6.px),
                  child: Text("选择收货地址", style: TextStyle(fontSize:
                  16.px), textAlign: TextAlign.center)
              ),
              Positioned(
                  top: 8.px,
                  right: 12.px,
                  child: InkWell(
                    onTap: () => Navigator.pop(context),
                    child: Icon(
                        Icons.close, color: Colors.grey, size: 18.px),
                  ))
            ],
          ),
          Consumer<AddressModel>(
            builder: (context, model, child) => _buildAddressList(model),
          )
        ],
      ),
    ));
  }

  Widget _buildAddressList(AddressModel model) {
    if(model.list == null) {
      return Center(child: Utils.loadingWidget);
    }

    List<Widget> widgets = List<Widget>();
    for(var item in model.list) {
      widgets.add(InkWell(
        onTap: () {
          if(widget.onSelected != null) {
            widget.onSelected(item);
          }

          Navigator.pop(context);
        },
        child: Container(
          // decoration: BoxDecoration(
          //   borderRadius: BorderRadius.circular(12.px),
          //   border: Border.all(color: _address?.id == item.id ? DColors.Main
          //       : Colors.grey),
          // ),
          padding: EdgeInsets.all(6.px),
          margin: EdgeInsets.only(left: 12.px, right: 12.px),
          child: AddressItem(item),
        ),
      ));
    }

    return Column(
      children: [
        Container(
          height: 268.px,
          margin: EdgeInsets.only(top: 12.px),
          child: ListView(
            children: widgets,
          ),
        ),
        Container(
          height: 68.px,
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: DShadow.top,
          ),
          padding: EdgeInsets.only(top: 6.px, bottom: 12.px),
          child: InkWell(
            onTap: () {
              Navigator.pushNamed(context, RouteNames.editAddress);
            },
            child: Container(
              decoration: BoxDecoration(
                color: DColors.Main,
                borderRadius: BorderRadius.circular(45.px),
              ),
              alignment: Alignment.center,
              margin: EdgeInsets.only(left: 22.px, right: 22.px, bottom: 8.px),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(Icons.room_outlined, color: Colors.white),
                  Text("新建收货地址", style: TextStyle(color: Colors.white))
                ],
              ),
            ),
          ),
        )
      ],
    );
  }
}