import 'package:adaptix/adaptix.dart';
import 'package:flutter/material.dart';
import 'package:sapaly_shop/features/screens/auth/login/login_screen.dart';
import 'package:sapaly_shop/features/services/auth_service.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/custom_button.dart';
import '../../../common/widgets/custom_text_field.dart';
import '../../../constants/global_variables.dart';
import '../../../models/auth/register/register_request_model.dart';

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

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

class _LoginScreenState extends State<RegisterScreen> {
  final TextEditingController emailController = TextEditingController();
  final TextEditingController phoneController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();
  final TextEditingController passwordConfirmationController =
      TextEditingController();
  final AuthService authService = AuthService();
  final formKey = GlobalKey<FormState>();

  void register() {
    authService.register(
      registerRequestModel: RegisterRequestModel(
        email: emailController.text,
        password: passwordController.text,
        passwordConfirmation: passwordConfirmationController.text,
        phone: phoneController.text,
      ),
      context: context,
    );
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: Container(),
      ),
      body: Container(
        margin: EdgeInsets.symmetric(
          vertical: GlobalVariables.verticalPadding(context) / (3),
          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.3,
        child: Form(
          key: formKey,
          child: ListView(
            physics: const NeverScrollableScrollPhysics(),
            children: [
              Padding(
                padding: EdgeInsets.symmetric(
                  vertical: 15.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,
                  obscureText: false,
                  isPassword: false,
                  inputType: TextInputType.emailAddress,
                ),
              ),
              CustomTextField(
                controller: phoneController,
                labelText: 'your_phone_number'.translation,
                hintText: '64283513',
                obscureText: false,
                isPassword: false,
                inputType: TextInputType.phone,
              ),
              Padding(
                padding: EdgeInsets.symmetric(vertical: 15.adaptedPx()),
                child: CustomTextField(
                  controller: passwordController,
                  labelText: 'password'.translation,
                  hintText: 'enter_your_password'.translation,
                  obscureText: true,
                  isPassword: true,
                  inputType: TextInputType.visiblePassword,
                ),
              ),
              CustomTextField(
                controller: passwordConfirmationController,
                labelText: 'repeat_password'.translation,
                hintText: 'repeat_password_to_confirm'.translation,
                obscureText: true,
                isPassword: true,
                inputType: TextInputType.visiblePassword,
              ),
              SizedBox(height: 20.adaptedPx()),
              CustomButton(
                name: 'register'.translation,
                onTap: () async {
                  if (formKey.currentState!.validate()) {
                    register();
                  }
                },
                isMain: true,
                backgroundColor: AppTheme.lightPrimaryColor,
              ),
              Align(
                alignment: Alignment.center,
                child: Text(
                  'or'.translation,
                  textAlign: TextAlign.center,
                  style:
                      Theme.of(context).primaryTextTheme.bodyMedium?.copyWith(
                            color: AppTheme.blackColor,
                            fontSize: 14.adaptedPx(),
                          ),
                ),
              ),
              CustomButton(
                name: 'login'.translation,
                isMain: false,
                onTap: () {
                  PageNavigator(ctx: context).nextPageOnly(page: LoginScreen());
                },
                backgroundColor: Colors.grey.withOpacity(0.15),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
