import 'package:cargocalculaterapp/core/extension/build_context_extension.dart';
import 'package:cargocalculaterapp/features/auth/presentation/pages/mixin/sign_up_mixin.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import '../../../../../core/utils/app_utils.dart';
import '../../../../../core/widgets/loading/custom_loading.dart';
import '../../../../../core/widgets/text_filds/custom_text_field_name.dart';
import '../../../../../generated/l10n.dart';
import '../../bloc/sign_up/sign_up_bloc.dart';

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

  @override
  State<SignUpPage> createState() => _SignUpPageState();
}

class _SignUpPageState extends State<SignUpPage> with SignUpMixin {
  @override
  void initState() {
    initControllers();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<SignUpBloc, SignUpState>(
      builder: (context, state) {
        return Scaffold(
          appBar: AppBar(),
          body: ListView(
            padding: AppUtils.kPaddingAll16,
            children: [
              Center(
                child: Container(
                  width: 64,
                  height: 64,
                  decoration: BoxDecoration(
                    borderRadius: AppUtils.kBorderRadius16,
                    color: context.color.iconBackground,
                  ),
                  padding: AppUtils.kPaddingAll16,
                  child: SvgPicture.asset(
                    "assets/svg/ic_add_user.svg",
                    width: 32,
                    height: 32,
                  ),
                ),
              ),
              AppUtils.kBoxHeight16,
              Text(
                AppLocalization.current.sign_up,
                style: context.text.authTitle,
                textAlign: TextAlign.center,
              ),
              AppUtils.kBoxHeight24,
              CustomTextFieldName(
                hint: AppLocalization.current.full_name_hint,
                inputType: TextInputType.name,
                name: AppLocalization.current.full_name,
                controller: fullNameController,
                onchange: (value) {
                  context.read<SignUpBloc>().add(
                    OnInputEnterEvent(
                      login: loginController.text,
                      fullName: value,
                    ),
                  );
                },
              ),
              AppUtils.kBoxHeight16,
              CustomTextFieldName(
                hint: AppLocalization.current.enter_phone_or_mail,
                inputType: TextInputType.text,
                name: AppLocalization.current.phone,
                controller: loginController,
                onchange: (value) {
                  context.read<SignUpBloc>().add(
                    OnInputEnterEvent(
                      login: value,
                      fullName: fullNameController.text,
                    ),
                  );
                },
              ),
              AppUtils.kBoxHeight48,
              Padding(
                padding: AppUtils.kPaddingHor34,
                child: ElevatedButton(
                  onPressed:
                      state.isButtonEnabled && !state.isLoading
                          ? () {
                            context.read<SignUpBloc>().add(
                              SubmitEvent(
                                login: loginController.text,
                                fullName: fullNameController.text,
                              ),
                            );
                          }
                          : null,
                  child:
                      state.isLoading
                          ? const CustomLoadingWidget()
                          : Text(AppLocalization.current.sign_up),
                ),
              ),
              AppUtils.kBoxHeight16,
              Text(
                AppLocalization.current.has_account,
                style: context.text.authDesc,
                textAlign: TextAlign.center,
              ),
              AppUtils.kBoxHeight16,
              Padding(
                padding: AppUtils.kPaddingHor34,
                child: Material(
                  color: Colors.transparent,
                  child: InkWell(
                    onTap: () {
                      Navigator.pop(context);
                    },
                    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.auth,
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.w700,
                            color: context.color.textColor,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

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