import 'package:aitrainer_app/localization/app_localization.dart';
import 'package:aitrainer_app/model/auth.dart';
import 'package:aitrainer_app/viewmodel/customer_changing_view_model.dart';
import 'package:aitrainer_app/viewmodel/customer_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

class CustomerModifyPage extends StatefulWidget{
  _CustomerModifyPageState _state;

  _CustomerModifyPageState createState() {
    _state = _CustomerModifyPageState();
    return _state;
  }
}

class GenderItem {
  GenderItem(this.dbValue,this.name);
  final String dbValue;
  String name;
}

class _CustomerModifyPageState extends State<CustomerModifyPage> {
  final _formKey = GlobalKey<FormState>();
  GenderItem selectedGender;
  List<GenderItem> genders;

  @override
  void initState() {
    super.initState();
    genders = [
      GenderItem("m", "Man"),
      GenderItem("w", "Woman"),
    ];
    selectedGender = genders[0];

  }

  @override
  Widget build(BuildContext context) {
    final CustomerViewModel model = CustomerViewModel();
    model.customer = Auth().userLoggedIn;
    final CustomerChangingViewModel customerChangeModel =
      CustomerChangingViewModel(model);
    customerChangeModel.customer.customer.sex = selectedGender.dbValue;

    // we cannot initialize the translations in the initState
    genders.forEach((GenderItem element) {
      if ( element.dbValue == "m") {
        element.name = AppLocalizations.of(context).translate("Man");
      }
      if ( element.dbValue == "w") {
        element.name = AppLocalizations.of(context).translate("Woman");
      }
    });

    return Scaffold(
        resizeToAvoidBottomInset: true,
      appBar: AppBar(
        title: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Text("Profil"),
            Image.asset(
              'asset/image/WT_long_logo.png',
              fit: BoxFit.cover,
              height: 65.0,
            ),
          ],
        ),
        //title: Text(AppLocalizations.of(context).translate('Settings')),
        backgroundColor: Colors.transparent,
      ),
      body: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('asset/image/WT_light_background.png'),
              fit: BoxFit.cover,
              alignment: Alignment.center,
            ),
          ),
        child: Form(
          key: _formKey,
          child: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            padding: EdgeInsets.only(top: 40, left: 25, right: 45, bottom:100),

            child: Container(
              alignment: Alignment.center,

              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [

                      Expanded(
                        child: TextFormField(
                          style: TextStyle(fontSize: 12),
                          decoration: InputDecoration(
                            fillColor: Colors.white24,
                            filled: true,
                            labelText: AppLocalizations.of(context).translate('Email'),
                          ),
                          initialValue: customerChangeModel.customer.customer.email,
                            onFieldSubmitted: (input) => customerChangeModel.customer.setEmail(input)
                        )
                      )
                    ],
                  ),
                  Divider(),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [

                      Expanded(
                          child: TextFormField(
                            style: TextStyle(fontSize: 12),
                            obscureText: true,
                            decoration: InputDecoration(
                              fillColor: Colors.white24,
                              filled: true,
                              labelText: AppLocalizations.of(context).translate('Password (Leave empty if you don\'t want to change)' ),
                            ),
                            initialValue: customerChangeModel.customer.customer.password,
                              onFieldSubmitted: (input) => customerChangeModel.customer.setPassword(input)
                          )
                      )
                    ],
                  ),
                  Divider(),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [

                      Expanded(
                          child: TextFormField(
                            style: TextStyle(fontSize: 12),
                            decoration: InputDecoration(
                              fillColor: Colors.white24,
                              filled: true,
                              labelText: AppLocalizations.of(context).translate('Name'),
                            ),
                            initialValue: customerChangeModel.customer.customer.name,
                              onFieldSubmitted: (input) => customerChangeModel.customer.setName(input)
                          )
                      )
                    ],
                  ),
                  Divider(),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [

                      Expanded(
                          child: TextFormField(
                            style: TextStyle(fontSize: 12),
                            decoration: InputDecoration(
                              fillColor: Colors.white24,
                              filled: true,

                              labelText: AppLocalizations.of(context).translate('First Name'),
                            ),
                            keyboardType: TextInputType.emailAddress,
                            initialValue: customerChangeModel.customer.customer.firstname,
                              onFieldSubmitted: (input) => customerChangeModel.customer.setFirstName(input)
                          )
                      )
                    ],
                  ),
                  Divider(),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [

                      Expanded(
                          child: TextFormField(
                            style: TextStyle(fontSize: 12),
                            decoration: InputDecoration(
                              fillColor: Colors.white24,
                              filled: true,
                              labelText: AppLocalizations.of(context).translate('Birth Year'),
                            ),
                            keyboardType: TextInputType.number,
                              inputFormatters: <TextInputFormatter>[
                                WhitelistingTextInputFormatter.digitsOnly
                              ],
                            initialValue: customerChangeModel.customer.customer.birthYear.toString(),
                              onFieldSubmitted: (input) => customerChangeModel.customer.setBirthYear(int.parse(input))
                          )
                      )
                    ],
                  ),
                  Divider(),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [

                      Expanded(
                          child: TextFormField(
                            style: TextStyle(fontSize: 12),
                            decoration: InputDecoration(
                              fillColor: Colors.white24,
                              filled: true,
                              labelText: AppLocalizations.of(context).translate('Weight'),
                            ),
                            inputFormatters: <TextInputFormatter>[
                              WhitelistingTextInputFormatter.digitsOnly
                            ],
                            initialValue: customerChangeModel.customer.customer.weight.toString(),
                            keyboardType: TextInputType.number,
                            onFieldSubmitted: (input) => customerChangeModel.customer.setWeight(int.parse(input)),
                          )
                      )
                    ],
                  ),
                  Divider(),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [

                      Expanded(
                          child: DropdownButtonHideUnderline(
                              child: DropdownButton<GenderItem>(
                                hint: Text(AppLocalizations.of(context).translate('Select a gender')),
                                style: TextStyle(fontSize: 12, color: Colors.black),
                                focusColor: Colors.white24,
                                value: selectedGender,
                                items: genders.map((GenderItem gender){
                                  return DropdownMenuItem<GenderItem>(
                                    value: gender,
                                    child: Text(gender.name)
                                  );
                                }).toList(),
                                onChanged:(GenderItem gender) => {
                                  setState(() {
                                    selectedGender = gender;
                                    customerChangeModel.customer.setSex(gender.dbValue);

                                    print ("Gender " + gender.name);
                                    })
                                  //model.customer.sex =
                                },
                            )
                          )
                      )
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [

                      Expanded(
                          child: RaisedButton(

                            color: Colors.orange,
                            textColor: Colors.white,
                            child: InkWell(
                                child: Text(AppLocalizations.of(context).translate("Next"))),
                            onPressed: () => {
                              customerChangeModel.saveCustomer(),
                              Navigator.of(context).pushNamed("customerGoalPage", arguments: customerChangeModel)
                            },
                          )
                      )
                    ],
                  ),
                ],
              ),
          ),
        ),
        )
      )
    );
  }
}