import 'package:flutter/material.dart';
import 'package:flutter_provisioning_for_iot/objects/cloud_service_api.dart';

import '../widgets/sidebar.dart';
import '../widgets/single_section.dart';

class Settings extends StatefulWidget {
  const Settings({Key? key}) : super(key: key);
  static CloudServiceAPI cloudService = CloudServiceAPI();

  static String address = cloudService.address;
  static String username = cloudService.username;
  static String password = cloudService.password;

  @override
  State<Settings> createState() => _SettingsState();

  /// Initializes the Settings with the correct Values from the last session
  static Future initSettings() async {}
}

class _SettingsState extends State<Settings> {
  bool _passwordVisible = false;

  @override
  void initState() {
    debugPrint("PASSWORD ${Settings.password}");
    super.initState();
    _passwordVisible = false;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          // Here we take the value from the MyHomePage object that was created by
          // the App.build method, and use it to set our appbar title.
          title: const Text("Settings"),
        ),
        body: Center(
          child: ListView(children: [
            SingleChildScrollView(
              physics: const ScrollPhysics(),
              child: Column(children: <Widget>[
                SingleSection(
                  title: "Server Settings",
                  children: [
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
                      child: TextFormField(
                        decoration: const InputDecoration(
                          border: OutlineInputBorder(),
                          hintText: 'Enter the server url (no https:// and paths)',
                          labelText: "Address for the Device Manager",
                        ),
                        initialValue: Settings.address,
                        keyboardType: TextInputType.text,
                        onChanged: (String newValue) {
                          Settings.address = newValue;
                          Settings.cloudService.address = newValue;
                        },
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
                      child: TextFormField(
                        decoration: const InputDecoration(
                          border: OutlineInputBorder(),
                          hintText: 'Enter the server username',
                          labelText: "Username",
                        ),
                        initialValue: Settings.username,
                        keyboardType: TextInputType.text,
                        onChanged: (String newValue) {
                          Settings.username = newValue;
                          Settings.cloudService.username = newValue;
                        },
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
                      child: TextFormField(
                        obscureText: _passwordVisible ? false : true,
                        decoration: InputDecoration(
                          border: const OutlineInputBorder(),
                          hintText: 'Enter the server password',
                          labelText: "Password",
                          suffixIcon: IconButton(
                            icon: Icon(_passwordVisible ? Icons.visibility : Icons.visibility_off),
                            onPressed: () {
                              setState(() {
                                _passwordVisible = !_passwordVisible;
                              });
                            },
                          ),
                          filled: true,
                        ),
                        initialValue: Settings.password,
                        keyboardType: TextInputType.text,
                        onChanged: (String newValue) {
                          Settings.password = newValue;
                          Settings.cloudService.password = newValue;
                        },
                      ),
                    ),
                  ],
                ),
              ]),
            )
          ]),
        ),
        drawer: const Sidebar());
  }
}
