import 'package:cargocalculaterapp/core/extension/build_context_extension.dart';
import 'package:cargocalculaterapp/core/utils/app_utils.dart';
import 'package:cargocalculaterapp/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import '../../../../../core/widgets/loading/custom_loading.dart';
import '../../../../../core/widgets/text_filds/custom_text_field_name.dart';
import '../../../../../router/name_routes.dart';
import '../../bloc/auth/auth_bloc.dart';
import '../mixin/auth_mixin.dart';

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

  @override
  State<AuthPage> createState() => _AuthPageState();
}

class _AuthPageState extends State<AuthPage> with AuthMixin {
  @override
  void initState() {
    initControllers();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AuthBloc, AuthState>(
      builder: (context, state) {
        return Scaffold(
          appBar: AppBar(),
          body: ListView(
            padding: AppUtils.kPaddingHor16,
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 60),
                child: SvgPicture.asset("assets/svg/ic_logo_auth.svg"),
              ),
              AppUtils.kBoxHeight48,
              Row(
                children: [
                  Container(
                    width: 32,
                    height: 32,
                    decoration: BoxDecoration(
                      borderRadius: AppUtils.kBorderRadius12,
                      color: context.color.iconBackground,
                    ),
                    padding: AppUtils.kPaddingAll6,
                    child: SvgPicture.asset("assets/svg/ic_user.svg"),
                  ),
                  AppUtils.kBoxWidth12,
                  Text(
                    AppLocalization.current.auth_login,
                    style: context.text.authTitle,
                  ),
                ],
              ),
              AppUtils.kBoxHeight32,
              CustomTextFieldName(
                hint: AppLocalization.current.enter_phone_or_mail,
                inputType: TextInputType.text,
                name: AppLocalization.current.phone,
                controller: loginController,
                onchange: (value) {
                  context.read<AuthBloc>().add(
                    OnInputEnterEvent(
                      login: value,
                      password: passwordController.text,
                    ),
                  );
                },
              ),
              AppUtils.kBoxHeight16,
              CustomTextFieldName(
                hint: "********",
                prefixWidget: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      "CT-",
                      style: context.text.profileCategory.copyWith(
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ],
                ),
                inputType: TextInputType.number,
                name: AppLocalization.current.password,
                obscureText: state.passwordHidden,
                maxLines: 1,
                controller: passwordController,
                suffix: IconButton(
                  onPressed: () {
                    context.read<AuthBloc>().add(
                      const PasswordVisibilityEvent(),
                    );
                  },
                  icon: Icon(
                    state.passwordHidden
                        ? Icons.visibility_off_rounded
                        : Icons.visibility_rounded,
                  ),
                ),
                onchange: (value) {
                  context.read<AuthBloc>().add(
                    OnInputEnterEvent(
                      login: loginController.text,
                      password: value,
                    ),
                  );
                },
              ),
              AppUtils.kBoxHeight48,
              Padding(
                padding: AppUtils.kPaddingHor34,
                child: ElevatedButton(
                  onPressed: state.isButtonEnabled && !state.isLoading
                      ? () {
                          context.read<AuthBloc>().add(
                            SubmitEvent(
                              login: loginController.text,
                              password: "CT-${passwordController.text}",
                            ),
                          );
                        }
                      : null,
                  child: state.isLoading
                      ? const CustomLoadingWidget()
                      : Text(AppLocalization.current.auth),
                ),
              ),
              AppUtils.kBoxHeight16,
              Text(
                AppLocalization.current.no_account,
                style: context.text.authDesc,
                textAlign: TextAlign.center,
              ),
              AppUtils.kBoxHeight16,
              Padding(
                padding: AppUtils.kPaddingHor34,
                child: Material(
                  color: Colors.transparent,
                  child: InkWell(
                    onTap: () {
                      Navigator.pushNamed(context, Routes.signUp);
                    },
                    borderRadius: AppUtils.kBorderRadius24,
                    child: Ink(
                      decoration: BoxDecoration(
                        color: context.color.scaffoldBackgroundColor,
                        borderRadius: AppUtils.kBorderRadius24,
                        border: Border.all(color: context.color.primaryColor),
                      ),
                      height: 56,
                      padding: AppUtils.kPaddingHor16,
                      child: Center(
                        child: Text(
                          AppLocalization.current.sign_up,
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.w700,
                            color: context.color.textColor,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    disposeControllers();
    super.dispose();
  }
}
