import 'package:flutter/material.dart';
import 'package:school_management_system/api/auth_requests.dart';
import 'package:school_management_system/components/default_values.dart';
import 'package:school_management_system/screens/main/main_screen.dart';

class LoginForm extends StatefulWidget {
  const LoginForm({
    super.key,
  });

  @override
  State<LoginForm> createState() => _LoginFormState();
}

class _LoginFormState extends State<LoginForm> {
  var usernameController = TextEditingController();
  var passwordController = TextEditingController();
  var checked = false;
  var obscureText = true;
  //var authService = AuthoriseService();

  @override
  Widget build(BuildContext context) {
    return Column(
      // mainAxisAlignment: MainAxisAlignment.start,
      children: [
        TextField(
          controller: usernameController,
          keyboardType: TextInputType.emailAddress,
          textInputAction: TextInputAction.next,
          cursorColor: primaryColor,
          onSubmitted: (username) {
            setState(() {
              usernameController.text = username;
            });
          },
          decoration: const InputDecoration(
            hintText: "Username",
            prefixIcon: Padding(
              padding: EdgeInsets.all(defaultPadding),
              child: Icon(Icons.person),
            ),
          ),
        ),
        const SizedBox(height: 10),
        TextField(
          controller: passwordController,
          keyboardType: TextInputType.visiblePassword,
          textInputAction: TextInputAction.done,
          obscureText: obscureText,
          onSubmitted: (paswword) {
            setState(() {
              passwordController.text = paswword;
            });
          },
          cursorColor: primaryColor,
          decoration: InputDecoration(
              hintText: "Password",
              prefixIcon: const Padding(
                padding: EdgeInsets.all(defaultPadding),
                child: Icon(Icons.lock),
              ),
              suffixIcon: IconButton(
                  icon: Icon(
                      obscureText ? Icons.visibility : Icons.visibility_off),
                  onPressed: () {
                    setState(
                      () {
                        obscureText = !obscureText;
                      },
                    );
                  },
              )),
        ),
        const SizedBox(),
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Checkbox(
                value: checked,
                onChanged: (value) {
                  setState(() {
                    checked = value!;
                  });
                }),
            const Text('Save Password'),
          ],
        ),
        const SizedBox(height: defaultPadding),
        ElevatedButton(
          onPressed: () {
            login(usernameController.value.text, passwordController.value.text)
                .then((status) {
              if (status.startsWith('Successful')) {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) {
                      return const MainScaffold();
                    },
                  ),
                );

                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(
                    content: Text(status),
                  ),
                );
              } else {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(
                    content: Text(status),
                  ),
                );
              }
            });
          },
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                "sign in".toUpperCase(),
              ),
              const SizedBox(width: 10),
              const Icon(Icons.login)
            ],
          ),
        ),
      ],
    );
  }
}
