import 'package:country_code_picker/country_code_picker.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/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:provider/provider.dart';

class InvitationCreate extends StatefulWidget {
  @override
  _InvitationCreateState createState() => _InvitationCreateState();
}

class _InvitationCreateState extends State<InvitationCreate> {
  TextEditingController _nameController = new TextEditingController();
  TextEditingController _phoneController = new TextEditingController();

  bool _isLoading = false;
  String dialCode;

  @override
  void initState() {
    super.initState();
    dialCode = "+95";
  }

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

  @override
  Widget build(BuildContext context) {
    final nameBox = InputText(
        labelTextKey: 'customer.name',
        iconData: Icons.person,
        controller: _nameController);

    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,
            ),
            onPressed: () => Navigator.of(context).pop(),
          ),
          title: LocalText(
            context,
            "invitation.form.title",
            fontSize: 20,
            color: primaryColor,
          ),
        ),
        body: Container(
          padding: EdgeInsets.all(18),
          child: ListView(
            children: <Widget>[
              nameBox,
              SizedBox(height: 10),
              Row(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Icon(
                      Icons.phone,
                      color: primaryColor,
                    ),
                  ),
                  Container(
                    decoration: BoxDecoration(
                        border: Border.all(color: Colors.grey[400], width: 1),
                        borderRadius: BorderRadius.all(Radius.circular(12.0))),
                    child: CountryCodePicker(
                      onChanged: _countryChange,
                      initialSelection: dialCode,
                      countryFilter: ['mm', 'us'],
                      showCountryOnly: false,
                      showOnlyCountryWhenClosed: false,
                      alignLeft: false,
                      textStyle: TextStyle(
                        fontSize: 16,
                      ),
                    ),
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  Flexible(
                    child: Container(
                      padding: EdgeInsets.only(top: 10, bottom: 10),
                      child: TextFormField(
                        controller: _phoneController,
                        cursorColor: primaryColor,
                        textAlign: TextAlign.left,
                        keyboardType: TextInputType.phone,
                        style: TextStyle(
                          fontSize: 18,
                        ),
                        decoration: InputDecoration(
                          fillColor: Colors.white,
                          labelText: getLocalString(context, "customer.phone"),
                          labelStyle:
                              TextStyle(fontSize: 16, color: Colors.grey),
                          filled: true,
                          focusedBorder: UnderlineInputBorder(
                              borderSide:
                                  BorderSide(color: Colors.grey, width: 1.0)),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              SizedBox(height: 20),
              fcsButton(context, getLocalString(context, "invite.btn"),
                  callack: _invite),
            ],
          ),
        ),
      ),
    );
  }

  _countryChange(CountryCode countryCode) {
    setState(() {
      dialCode = countryCode.dialCode;
    });
  }

  _invite() async {
    String userName = _nameController.text;
    String phoneNumber = dialCode + _phoneController.text;
    if (userName == null ||
        userName == "" ||
        phoneNumber == null ||
        phoneNumber == "") {
      showMsgDialog(context, "Error", "Invalid name or phone number");
      return;
    }
    setState(() {
      _isLoading = true;
    });
    try {
      CustomerModel customerModel =
          Provider.of<CustomerModel>(context, listen: false);
      await customerModel.inviteUser(userName, phoneNumber);
      Navigator.pop(context);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
}
