import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/main/model/language_model.dart';
import 'package:fcs/pages/main/model/main_model.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';
import 'package:fcs/pages/main/util.dart';

typedef void ProfileCallback();

class ProfileEdit extends StatefulWidget {
  @override
  _ProfileEditState createState() => _ProfileEditState();
}

class _ProfileEditState extends State<ProfileEdit> {
  final TextEditingController nameController = new TextEditingController();
  bool _loading = false;

  @override
  void initState() {
    super.initState();
    MainModel mainModel = Provider.of<MainModel>(context, listen: false);
    nameController.text = mainModel.user.name;
  }

  @override
  Widget build(BuildContext context) {
    var languageModel = Provider.of<LanguageModel>(context);

    final name = Container(
        padding: EdgeInsets.only(top: 0, left: 20, right: 15, bottom: 30),
        child: TextFormField(
          controller: nameController,
          autofocus: true,
          cursorColor: primaryColor,
          style: textStyle,
          decoration: new InputDecoration(
            labelText: AppTranslations.of(context).text("profile.name"),
            labelStyle: languageModel.isEng ? labelStyle : labelStyleMM,
            icon: Icon(
              Icons.person,
              color: primaryColor,
            ),
            enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: primaryColor, width: 1.0)),
            focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: primaryColor, width: 1.0)),
          ),
        ));

    final saveBtn =
        fcsButton(context, getLocalString(context, "btn.save"), callack: _save);

    return LocalProgress(
      inAsyncCall: _loading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: LocalText(
            context,
            "profile.edit_title",
            fontSize: 20,
            color: primaryColor,
          ),
          backgroundColor: Colors.white,
          shadowColor: Colors.transparent,
          leading: IconButton(
            icon: Icon(
              CupertinoIcons.back,
              size: 35,
              color: primaryColor,
            ),
            onPressed: () => Navigator.of(context).pop(),
          ),
        ),
        body: Column(
          children: <Widget>[
            Expanded(child: name),
            Padding(
              padding: const EdgeInsets.all(18.0),
              child: saveBtn,
            ),
          ],
        ),
      ),
    );
  }

  _save() async {
    setState(() {
      _loading = true;
    });
    try {
      await Provider.of<MainModel>(context, listen: false)
          .updateProfileName(nameController.text);
      Navigator.pop(context);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _loading = false;
      });
    }
  }
}
