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:aitrainer_app/widgets/nav_drawer.dart';

class CustomerNewPage extends StatefulWidget{
  _CustomerNewPageState createState() => _CustomerNewPageState();
}

class _CustomerNewPageState extends State {
  final CustomerViewModel customer = CustomerViewModel();
  String groupValue = "m";
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    CustomerChangingViewModel model;
    customer.createNew();
    customer.setSex(groupValue);
    return Scaffold(
        drawer: NavDrawer(),
        appBar: AppBar(
          title: Text('New customer'),
        ),
        body: Center(
            child: Form(
                key: _formKey,
                child: Column(
                  children: <Widget>[
                    TextFormField(
                      decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Name',
                      ),
                      validator: (input) => input.length == 0 ? "Please type the name" : null,
                      onChanged: (input) => customer.setName(input),
                    ),
                    TextFormField(
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        labelText: 'First Name',
                      ),
                      validator: (input) => input.length == 0 ? "Please type the first name" : null,
                      onChanged: (input) => customer.setFirstName(input),
                    ),
                    TextFormField(
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        labelText: 'Email',
                      ),
                      validator: (String input) {
                          RegExp exp = new RegExp(r"[\w._]+\@[\w._]+.[a-z]+",
                            caseSensitive: false,
                            multiLine: false,);
                          String ret = exp.hasMatch(input) == true ?
                              null:
                            "Please type an email address";
                          return ret;
                        },
                      onChanged: (input) => customer.setEmail(input),
                    ),
                    TextFormField(
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        labelText: 'Age',
                      ),
                      validator: (input) => (int.parse(input) < 99 && int.parse(input) > 0) ?
                          null :
                          "Please type the right age 0-99",
                      onChanged: (input) => customer.setBirthYear(int.parse(input)),
                    ),
                    RadioListTile(
                      title: const Text('Man'),
                      value: "m",
                      groupValue: groupValue,
                      onChanged: (input) => {
                        setState(() {
                          groupValue = input;
                          customer.setSex(input);
                        }
                        )},

                    ),RadioListTile(
                      title: const Text('Woman'),
                      value: "w",
                      groupValue: groupValue,
                      onChanged: (input) => {
                        setState(() {
                          groupValue = input;
                          customer.setSex(input);
                        }
                        )},
                    ),
                ])
            ),
        ),
        floatingActionButton: FloatingActionButton(
              onPressed: () => {
              if (_formKey.currentState.validate()) {
                model = CustomerChangingViewModel(customer),
                model.addCustomer(),
                model.addNewCustomerToList(customer),
                Navigator.pop(context),
              }
          },
          child: Icon(Icons.save,),
          mini: true,
          )
    );
  }
}