import 'package:fcs/domain/vo/contact.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/contact/model/contact_model.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/widgets/input_text.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:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';

import 'widgets.dart';

class ContactEditor extends StatefulWidget {
  final Contact contact;
  const ContactEditor({this.contact});
  @override
  _ContactEditorState createState() => _ContactEditorState();
}

class _ContactEditorState extends State<ContactEditor> {
  TextEditingController _usaPhone = new TextEditingController();
  TextEditingController _mmPhone = new TextEditingController();
  TextEditingController _usaAddress = new TextEditingController();
  TextEditingController _mmAddress = new TextEditingController();
  TextEditingController _email = new TextEditingController();
  TextEditingController _facebook = new TextEditingController();

  bool _isLoading = false;

  @override
  void initState() {
    super.initState();
    if (widget.contact != null) {
      _usaPhone.text = widget.contact.usaContactNumber;
      _mmPhone.text = widget.contact.mmContactNumber;
      _usaAddress.text = widget.contact.usaAddress;
      _mmAddress.text = widget.contact.mmAddress;
      _email.text = widget.contact.emailAddress;
      _facebook.text = widget.contact.facebookLink;
    }
  }

  @override
  Widget build(BuildContext context) {
    final usaPhoneBox = InputText(
        labelTextKey: 'contact.usa.phone',
        iconData: CupertinoIcons.phone,
        controller: _usaPhone);
    final mmPhoneBox = InputText(
        labelTextKey: 'contact.mm.phone',
        iconData: CupertinoIcons.phone,
        controller: _mmPhone);
    final usaAddreesBox = InputText(
        labelTextKey: 'contact.usa.address',
        iconData: CupertinoIcons.location,
        maxLines: 3,
        controller: _usaAddress);
    final mmAddressBox = InputText(
        labelTextKey: 'contact.mm.address',
        iconData: CupertinoIcons.location,
        maxLines: 3,
        controller: _mmAddress);
    final emailBox = InputText(
        labelTextKey: 'contact.email',
        iconData: CupertinoIcons.mail,
        controller: _email);
    final faceBookBox = InputText(
        labelTextKey: 'contact.facebook',
        iconData: FontAwesomeIcons.facebook,
        controller: _facebook);

    final saveBox = fcsButton(context, getLocalString(context, "btn.save"),
        callack: _submit);

    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(),
              ),
              shadowColor: Colors.transparent,
              backgroundColor: Colors.white,
              title: LocalText(
                context,
                'contact.edit.title',
                color: primaryColor,
                fontSize: 20,
              )),
          body: ListView(
            children: [
              Padding(
                padding: const EdgeInsets.only(left: 18.0, right: 18),
                child: Column(
                  children: [
                    itemTitle(context, "contact.callus"),
                    usaPhoneBox,
                    mmPhoneBox,
                    Divider(),
                    itemTitle(context, "contact.findus"),
                    usaAddreesBox,
                    mmAddressBox,
                    Divider(),
                    itemTitle(context, "contact.emailus"),
                    emailBox,
                    Divider(),
                    itemTitle(context, "contact.visitus"),
                    faceBookBox,
                    SizedBox(
                      height: 10,
                    ),
                    saveBox,
                    SizedBox(
                      height: 20,
                    )
                  ],
                ),
              ),
            ],
          ),
        ));
  }

  _submit() async {
    setState(() {
      _isLoading = true;
    });
    try {
      widget.contact.usaContactNumber = _usaPhone.text;
      widget.contact.mmContactNumber = _mmPhone.text;
      widget.contact.usaAddress = _usaAddress.text;
      widget.contact.mmAddress = _mmAddress.text;
      widget.contact.emailAddress = _email.text;
      widget.contact.facebookLink = _facebook.text;
      var contactModel = Provider.of<ContactModel>(context, listen: false);
      await contactModel.saveContact(widget.contact);
      Navigator.pop(context);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
}
