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();

enum Currency { USD, MMK }

class ProfileCurrencyEdit extends StatefulWidget {
  @override
  _ProfileCurrencyEditState createState() => _ProfileCurrencyEditState();
}

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

  @override
  void initState() {
    super.initState();
    MainModel mainModel = Provider.of<MainModel>(context, listen: false);
    if (mainModel.user.preferCurrency == "MMK") {
      _currency = Currency.MMK;
    } else {
      _currency = Currency.USD;
    }
  }

  Currency _currency = Currency.USD;

  @override
  Widget build(BuildContext context) {
    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.currency.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>[
            InkWell(
              onTap: () => setState(() {
                _currency = Currency.USD;
              }),
              child: ListTile(
                title: Text('USD'),
                leading: Radio(
                  activeColor: primaryColor,
                  value: Currency.USD,
                  groupValue: _currency,
                  onChanged: (Currency value) {
                    setState(() {
                      _currency = value;
                    });
                  },
                ),
              ),
            ),
            InkWell(
              onTap: () => setState(() {
                _currency = Currency.MMK;
              }),
              child: ListTile(
                title: const Text('MMK'),
                leading: Radio(
                  activeColor: primaryColor,
                  value: Currency.MMK,
                  groupValue: _currency,
                  onChanged: (Currency value) {
                    setState(() {
                      _currency = value;
                    });
                  },
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(18.0),
              child: saveBtn,
            ),
          ],
        ),
      ),
    );
  }

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