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/material.dart';
import 'package:flutter/services.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';

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

class SignupPage extends StatefulWidget {
  @override
  _SignupPageState createState() => _SignupPageState();
}

class _SignupPageState extends State<SignupPage> {
  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(Icons.close),
            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,
            'user_edit.welcome',
            fontSize: 21,
            color: primaryColor,
            fontWeight: FontWeight.bold,
          ),
        ),
        Container(
          padding: EdgeInsets.only(top: 25),
          child: LocalText(
            context,
            'user_edit.name',
            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.end,
            children: <Widget>[
              InkWell(
                onTap: () => _submit(),
                child: CircleAvatar(
                  minRadius: 25,
                  backgroundColor: primaryColor,
                  child: Icon(
                    FontAwesomeIcons.check,
                    color: Colors.white,
                  ),
                ),
              )
            ],
          ),
        ),
      ],
    );
  }

  _submit() async {
    setState(() {
      _isLoading = true;
    });
    try {
      await context.read<MainModel>().joinInvite(nameCtl.text);
      Navigator.pushNamedAndRemoveUntil(context, "/home", (r) => false);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
}
