import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/viewModels/dashboard_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/services/authentication/auth_provider.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/text/app_texts_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:provider/provider.dart';

class ProfileSettings extends StatefulWidget {
  @override
  _ProfileSettings createState() => _ProfileSettings();
}

class _ProfileSettings extends State<ProfileSettings>
    with TickerProviderStateMixin {
  bool smsAlert = true;
  bool emailAlert = true;
  int language = 1;
  final authService = new AuthProvider();
  TextEditingController emergencyContact = new TextEditingController();
  TextEditingController emailController = new TextEditingController();
  TextEditingController emergencyContactName = new TextEditingController();
  @override
  void initState() {
    Future.delayed(new Duration(seconds: 0), () {
      ProjectViewModel projectProvider;
      projectProvider = Provider.of(context);
      if (projectProvider.isLogin == true) getSettings(context);
    });
    super.initState();
  }

  Widget build(BuildContext context) {
    return BaseView<DashboardViewModel>(
        onModelReady: (model) => {},
        builder: (_, model, wi) => Container(
            child: model.user != null
                ? ListView(scrollDirection: Axis.vertical, children: <Widget>[
                    Container(
                      color: Theme.of(context).textTheme.headline2.color,
                      padding: EdgeInsets.all(15),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          AppText(
                            TranslationBase.of(context).fileNo,
                            color: Colors.black,
                          ),
                          AppText(
                            model.user.patientID.toString(),
                            color: Colors.black,
                          ),
                        ],
                      ),
                    ),
                    SizedBox(
                      height: 1,
                      width: MediaQuery.of(context).size.width,
                      child: Container(
                        color: Colors.grey[300],
                      ),
                    ),
                    Padding(
                      child: AppText(
                          TranslationBase.of(context).languageSetting,
                          fontWeight: FontWeight.bold),
                      padding: EdgeInsets.all(10),
                    ),
                    Container(
                        color: Theme.of(context).textTheme.headline2.color,
                        padding: EdgeInsets.only(
                            top: 0, left: 10, right: 10, bottom: 0),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            AppText(TranslationBase.of(context).english),
                            new Radio(
                              value: 2,
                              groupValue: language,
                              onChanged: (value) {
                                setState(() {
                                  language = value;
                                });
                              },
                            )
                          ],
                        )),
                    Container(
                        color: Theme.of(context).textTheme.headline2.color,
                        padding: EdgeInsets.only(
                            top: 0, left: 10, right: 10, bottom: 0),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            AppText(TranslationBase.of(context).arabic),
                            new Radio(
                              value: 1,
                              groupValue: language,
                              onChanged: (value) {
                                setState(() {
                                  language = value;
                                });
                              },
                            )
                          ],
                        )),
                    Padding(
                      child: AppText(TranslationBase.of(context).alert),
                      padding: EdgeInsets.all(10),
                    ),
                    Container(
                        color: Theme.of(context).textTheme.headline2.color,
                        padding: EdgeInsets.only(
                            top: 0, left: 10, right: 10, bottom: 0),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            AppText(TranslationBase.of(context).emailAlert),
                            Switch(
                              value: emailAlert,
                              onChanged: (value) {
                                setState(() {
                                  emailAlert = value;
                                });
                              },
                              activeTrackColor: Colors.lightGreenAccent,
                              activeColor: Colors.green,
                            )
                          ],
                        )),
                    Container(
                        color: Theme.of(context).textTheme.headline2.color,
                        padding: EdgeInsets.only(
                            top: 0, left: 10, right: 10, bottom: 0),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            AppText(TranslationBase.of(context).smsAlert),
                            Switch(
                              value: smsAlert,
                              onChanged: (value) {
                                setState(() {
                                  smsAlert = value;
                                });
                              },
                              activeTrackColor: Colors.lightGreenAccent,
                              activeColor: Colors.green,
                            )
                          ],
                        )),
                    Padding(
                      child: AppText(TranslationBase.of(context).contactInfo),
                      padding: EdgeInsets.all(10),
                    ),
                    Container(
                        color: Theme.of(context).textTheme.headline2.color,
                        padding: EdgeInsets.only(
                            top: 0, left: 10, right: 10, bottom: 0),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            AppText(TranslationBase.of(context).email),
                            TextField(
                                controller: emailController,
                                decoration: InputDecoration(
                                  suffixIcon: Icon(Icons.edit),
                                ))
                          ],
                        )),
                    Container(
                        color: Theme.of(context).textTheme.headline2.color,
                        padding: EdgeInsets.only(
                            top: 0, left: 10, right: 10, bottom: 0),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            AppText(TranslationBase.of(context).emergencyName),
                            TextField(
                                controller: emergencyContactName,
                                decoration: InputDecoration(
                                  suffixIcon: Icon(Icons.edit),
                                ))
                          ],
                        )),
                    Container(
                        color: Theme.of(context).textTheme.headline2.color,
                        padding: EdgeInsets.only(
                            top: 0, left: 10, right: 10, bottom: 0),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            AppText(
                                TranslationBase.of(context).emergencyContact),
                            TextField(
                              controller: emergencyContact,
                              decoration: InputDecoration(
                                suffixIcon: Icon(Icons.edit),
                              ),
                            )
                          ],
                        )),
                    Container(
                        padding: EdgeInsets.all(10),
                        child: Row(
                          children: <Widget>[
                            Expanded(
                                child: DefaultButton(
                              TranslationBase.of(context).save,
                              () {
                                saveSettings();
                              },
                            )),
                          ],
                        ))
                  ])
                : Center(
                    child:
                        AppText(TranslationBase.of(context).loginToUseService),
                  )));
  }

  getSettings(context) {
    GifLoaderDialogUtils.showMyDialog(context);
    authService.getSettings().then((result) => {
          GifLoaderDialogUtils.hideDialog(context),
          setValue(result["PateintInfoForUpdateList"][0])
        });
  }

  setValue(value) {
    setState(() {
      this.language = int.parse(value["PreferredLanguage"]);
      this.emailAlert = value["IsEmailAlertRequired"];
      this.smsAlert = value["IsSMSAlertRequired"];
      this.emailController.text = value["EmailAddress"];
      this.emergencyContact.text = value["EmergencyContactNo"];
      this.emergencyContactName.text = value["EmergencyContactName"];
    });
  }

  saveSettings() {
    GifLoaderDialogUtils.showMyDialog(context);
    Map<String, dynamic> request = {};
    request["EmailAddress"] = this.emailController.text;
    request["EmergencyContactName"] = this.emergencyContactName.text;
    request["EmergencyContactNo"] = this.emergencyContact.text;
    request["IsEmailAlertRequired"] = this.emailAlert;
    request["IsSMSAlertRequired"] = this.smsAlert;
    request["PreferredLanguage"] = this.language.toString();
    authService.saveSettings(request).then((result) => {
          AppToast.showSuccessToast(
              message: TranslationBase.of(context).profileUpdate),
          GifLoaderDialogUtils.hideDialog(context)
        });
  }
}
