import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:mhunt_launcher/Backend/Backend.dart';
import 'package:mhunt_launcher/Widgets/CustomWidgets.dart';
import 'package:mhunt_launcher/home.dart';
import 'package:shared_preferences/shared_preferences.dart';

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

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {

  bool isReg = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    kickstartAnimations();

    autoLogin();
  }


  void autoLogin()async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    if(prefs.containsKey("username") && prefs.containsKey("password")){
      usernameController.text = prefs.getString("username")??'';
      passwordController.text = prefs.getString("password")??'';

      LoginOrReg();
    }
  }

  void kickstartAnimations() async {
    await Future.delayed(const Duration(milliseconds: 500));

    setState(() {
      op1 = 1;
      op2 = 1;
      op3 = 1;
    });
  }

  TextEditingController usernameController = TextEditingController();
  TextEditingController passwordController = TextEditingController();



  @override
  Widget build(BuildContext context) {


    const Color backgroundColor = Color(0xFF111111);
    const Color modalColor = Color(0xFFFFFFFF);

    return Scaffold(
      backgroundColor: Colors.transparent,
      body: CustomBody(
        context: context,
        onAnimEnd: () {
          // kickstartAnimations();
          setState(() {});
        },
        child: Center(
          child: GlassCard(child:
          Container(
            padding: EdgeInsets.all(25),
            width: 600,
            height: 400,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("Welcome to W3B Launcher", style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold),),
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Row(children: [Text("Username "), SizedBox(width: 300,child: TextField(controller: usernameController,))], mainAxisAlignment: MainAxisAlignment.center,),
                    Row(children: [Text("Password "), SizedBox(width: 300,child: TextField(controller: passwordController,))], mainAxisAlignment: MainAxisAlignment.center),
                    SizedBox(height: 30,),
                    GlassButton(onTap: LoginOrReg, child: Text(isReg? "Register":"Login"),width: 150,height: 40),
                  ],
                ),
                Row(children: [Text(isReg? "Already have an Account? ":"Don't have an Account? "), InkWell(child: Text(isReg? "Login Here":"Register Here",style: TextStyle(color: Colors.blue),),onTap: (){setState(() {
                  isReg = !isReg;
                });},)], mainAxisAlignment: MainAxisAlignment.center)
              ],
            ),
          )),
        )
      ),
    );
  }


  void LoginOrReg() async{
    // if(kDebugMode){
    //   Navigator.of(context).push(MaterialPageRoute(builder: (context)=>HomePage()));
    //   return;
    // }
    bool success = false;
    if(isReg){
      success = await Backend.Register(usernameController.text, passwordController.text);
    }else{
      success = await Backend.Login(usernameController.text, passwordController.text);
    }

    if(success){
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.setString('username', usernameController.text);
      prefs.setString('password', passwordController.text);

      Navigator.of(context).push(MaterialPageRoute(builder: (context)=>HomePage()));
    }
  }
}
