import 'package:adaptix/adaptix.dart';
import 'package:flutter/material.dart';
import 'package:sapaly_shop/common/custom_button.dart';
import 'package:sapaly_shop/models/auth/login/login_request_model.dart';
import 'package:sapaly_shop/features/services/page_navigator.dart';
import 'package:sapaly_shop/models/settings_model.dart';
import 'package:sapaly_shop/themes/app_theme.dart';

import '../../../../common/widgets/custom_text_field.dart';
import '../../../../constants/global_variables.dart';
import '../../../services/auth_service.dart';
import '../register_screen.dart';

class LoginScreen extends StatefulWidget {
  static const String routeName = '/login';
  LoginScreen({super.key});

  @override
  State<LoginScreen> createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  final formKey = GlobalKey<FormState>();
  TextEditingController emailController = TextEditingController();
  TextEditingController passwordController = TextEditingController();

  final AuthService authService = AuthService();

  void login() {
    debugPrint('EMAIL ${emailController.text}');
    authService.login(
      loginRequestModel: LoginRequestModel(
        email: emailController.text,
        password: passwordController.text,
      ),
      context: context,
    );
  }

  @override
  void dispose() {
    emailController.dispose();
    passwordController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: Container(),
      ),
      body: Container(
        margin: EdgeInsets.symmetric(
          vertical: GlobalVariables.verticalPadding(context) * 2,
          horizontal: GlobalVariables.horizontalPadding(context),
        ),
        padding: EdgeInsets.symmetric(
          vertical: GlobalVariables.verticalPadding(context) / 2,
          horizontal: GlobalVariables.horizontalPadding(context),
        ),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10.adaptedPx()),
          border: Border.all(
            width: 2.adaptedPx(),
            color: AppTheme.blackColor.withOpacity(0.1),
          ),
        ),
        height: GlobalVariables.deviceHeight(context) / 1.45,
        child: Form(
          key: formKey,
          child: ListView(
            physics: const NeverScrollableScrollPhysics(),
            children: [
              Padding(
                padding: EdgeInsets.symmetric(
                  vertical: 30.adaptedPx(),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Image.asset(
                      'assets/images/sapalyLogo.png',
                    ),
                    SizedBox(width: 15.adaptedPx()),
                    Text(
                      GlobalVariables.kAppName,
                      style: Theme.of(context)
                          .primaryTextTheme
                          .bodyMedium
                          ?.copyWith(
                            color: AppTheme.blackColor,
                            fontSize: 18.adaptedPx(),
                            fontWeight: FontWeight.w600,
                          ),
                    )
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.symmetric(vertical: 15.adaptedPx()),
                child: CustomTextField(
                  controller: emailController,
                  labelText: 'email'.translation,
                  hintText: 'gurbanov@gmail.com'.translation,
                  isPassword: false,
                  obscureText: false,
                  inputType: TextInputType.emailAddress,
                ),
              ),
              CustomTextField(
                controller: passwordController,
                labelText: 'password'.translation,
                hintText: 'enter_your_password'.translation,
                obscureText: true,
                isPassword: true,
                inputType: TextInputType.visiblePassword,
              ),
              SizedBox(height: 30.adaptedPx()),
              CustomButton(
                name: 'login'.translation,
                onTap: () {
                  if (formKey.currentState!.validate()) {
                    login();
                  }
                },
                backgroundColor: AppTheme.lightPrimaryColor,
                isMain: true,
              ),
              Align(
                alignment: Alignment.center,
                child: Padding(
                  padding: EdgeInsets.symmetric(vertical: 10.adaptedPx()),
                  child: Text(
                    'or'.translation,
                    style: Theme.of(context)
                        .primaryTextTheme
                        .bodyMedium
                        ?.copyWith(
                            fontSize: 15.adaptedPx(),
                            color: AppTheme.lightTextColor),
                  ),
                ),
              ),
              CustomButton(
                name: 'register'.translation,
                onTap: () {
                  PageNavigator(ctx: context).nextPage(
                    page: const RegisterScreen(),
                  );
                },
                backgroundColor: Colors.grey.withOpacity(0.15),
                isMain: false,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
