import 'package:fcs/domain/entities/user.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/customer/model/customer_model.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/widgets/display_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

typedef void FindCallBack();

class InvitationEditor extends StatefulWidget {
  final User customer;
  const InvitationEditor({this.customer});
  @override
  _InvitationEditorState createState() => _InvitationEditorState();
}

class _InvitationEditorState extends State<InvitationEditor> {
  bool _isLoading = false;

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

  @override
  Widget build(BuildContext context) {
    var phoneNumberBox = Row(
      children: <Widget>[
        Expanded(
            child: DisplayText(
          text: widget.customer.phoneNumber,
          labelTextKey: getLocalString(context, "customer.phone"),
          iconData: Icons.phone,
        )),
        IconButton(
            icon: Icon(Icons.open_in_new, color: primaryColor),
            onPressed: () => call(context, widget.customer.phoneNumber)),
      ],
    );

    return LocalProgress(
        inAsyncCall: _isLoading,
        child: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.white,
            shadowColor: Colors.transparent,
            centerTitle: true,
            leading: new IconButton(
              icon: new Icon(
                CupertinoIcons.back,
                color: primaryColor,
                size: 30,
              ),
              onPressed: () => Navigator.of(context).pop(),
            ),
            title: Text(
              widget.customer.name,
              style: TextStyle(fontSize: 20, color: primaryColor),
            ),
          ),
          body: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              children: <Widget>[
                Expanded(
                  child: ListView(
                    children: [phoneNumberBox],
                  ),
                ),
                fcsButton(context, getLocalString(context, "btn.delete"),
                    callack: _delete)
              ],
            ),
          ),
        ));
  }

  _delete() async {
    showConfirmDialog(context, "invitation.confirm.delete", () async {
      setState(() {
        _isLoading = true;
      });
      if (widget.customer == null) return;
      CustomerModel customerModel =
          Provider.of<CustomerModel>(context, listen: false);
      try {
        await customerModel.deleteInvite(widget.customer.phoneNumber);
        Navigator.pop(context);
      } catch (e) {
        showMsgDialog(context, "Error", e.toString());
      } finally {
        setState(() {
          _isLoading = false;
        });
      }
    });
  }
}
