import 'package:fcs/pages/main/model/main_model.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:flutter/services.dart';
import 'package:provider/provider.dart';

import '../../helpers/theme.dart';

class RequestInvitationPage extends StatefulWidget {
  @override
  _RequestInvitationPageState createState() => _RequestInvitationPageState();
}

class _RequestInvitationPageState extends State<RequestInvitationPage> {
  bool _isLoading = false;
  TextEditingController nameCtl;

  @override
  void initState() {
    super.initState();
    nameCtl = new TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return LocalProgress(
      inAsyncCall: _isLoading,
      child: new Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(CupertinoIcons.back),
            onPressed: () => Navigator.of(context).pop(),
          ),
          backgroundColor: primaryColor,
        ),
        body: _buildBody(context),
      ),
    );
  }

  Widget _buildBody(BuildContext context) {
    return ListView(
      padding: EdgeInsets.only(top: 5, left: 10, right: 10),
      children: <Widget>[
        Container(
          padding: EdgeInsets.only(top: 40),
          child: LocalText(
            context,
            'invite.title',
            fontSize: 21,
            color: primaryColor,
            fontWeight: FontWeight.bold,
          ),
        ),
        Container(
          padding: EdgeInsets.only(top: 25),
          child: LocalText(
            context,
            'invite.name.enter',
            color: labelColor,
            fontSize: 16,
          ),
        ),
        Container(
            padding: EdgeInsets.only(top: 0, bottom: 10),
            child: TextFormField(
              controller: nameCtl,
              cursorColor: primaryColor,
              textAlign: TextAlign.left,
              keyboardType: TextInputType.text,
              autofocus: true,
              style: TextStyle(
                fontSize: 18,
              ),
              decoration: new InputDecoration(
                enabledBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: primaryColor, width: 1.0)),
                focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: primaryColor, width: 1.0)),
              ),
            )),
        SizedBox(
          height: 20,
        ),
        Container(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                onPressed: _request,
                child: Text(getLocalString(context, "invite.request")),
              )
            ],
          ),
        ),
      ],
    );
  }

  _request() async {
    setState(() {
      _isLoading = true;
    });
    try {
      // just signup to request for invitation
      await context.read<MainModel>().signup(nameCtl.text);
      await showMsgDialog(context, "Successful",
          getLocalString(context, "invite.request.successful"));
      Navigator.pushNamedAndRemoveUntil(context, "/home", (r) => false);
    } catch (e) {
      await showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
}
