import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:qadirneyriz/config/config.dart';
import 'package:qadirneyriz/screens/auth/state/state.dart';
import 'package:qadirneyriz/utils/enums/status.dart';
import 'package:qadirneyriz/utils/tools/tools.dart';
import 'package:qadirneyriz/widgets/custom_button.dart';
import 'package:qadirneyriz/widgets/custom_textfield.dart';
import 'package:go_router/go_router.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

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

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

class _LoginScreenState extends State<LoginScreen> {
  TextEditingController phoneController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Consumer<AuthState>(
        builder: (context, value, child) {
          return CustomScrollView(
            slivers: <Widget>[
              SliverToBoxAdapter(
                child: Container(
                  height: 30,
                  color: config.ui.mainGreen,
                ),
              ),
              // Header Image with cut-out shape at the bottom
              SliverToBoxAdapter(
                child: ClipPath(
                  child: Image.asset(
                    'assets/images/template.png',
                    width: double.infinity,
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              // Form section with inputs and buttons
              SliverPadding(
                padding: const EdgeInsets.symmetric(horizontal: 20),
                sliver: SliverToBoxAdapter(
                  child: Column(
                    children: [
                      Container(
                        padding: const EdgeInsets.all(16),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(16),
                          boxShadow: [
                            BoxShadow(
                              color:
                                  Colors.black.withOpacity(0.1), // light shadow
                              blurRadius: 10,
                              spreadRadius: 5,
                            ),
                          ],
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: [
                            // Username field
                            CustomTextField(
                              label: AppLocalizations.of(context)!.phonenumber,
                              hintText:
                                  AppLocalizations.of(context)!.hintphonenumber,
                              textEditingController: phoneController,
                              textInputType: TextInputType.phone,
                              textInputAction: TextInputAction.next,
                            ),
                            const SizedBox(height: 16),
                            // Password field
                            CustomTextField(
                              isPass: true,
                              label: AppLocalizations.of(context)!.password,
                              hintText: AppLocalizations.of(context)!.hintpass,
                              textEditingController: passwordController,
                              textInputType: TextInputType.visiblePassword,
                            ),
                            const SizedBox(height: 40),
                            // Buttons section
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                // Login button (filled)
                                CustomButton(
                                    hieght: 56,
                                    fontSize: 13,
                                    text: AppLocalizations.of(context)!
                                        .submitwithotp,
                                    color: Colors.white,
                                    textColor: Colors.green,
                                    onPressed:
                                        value.statusLogin == Status.loading
                                            ? null
                                            : () {
                                                context.pushNamed('loginotp');
                                              }),
                                const SizedBox(width: 16),
                                // Register button (outlined)
                                Expanded(
                                  child: submitButton(context, value),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                      const SizedBox(height: 40),
                    ],
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }

  CustomButton submitButton(BuildContext context, AuthState state) {
    switch (state.statusLogin) {
      case Status.loading:
        return CustomButton(
            hieght: 56,
            fontSize: 12,
            text: AppLocalizations.of(context)!.loading,
            onPressed: null);

      default:
        return CustomButton(
          hieght: 56,
          fontSize: 16,
          text: AppLocalizations.of(context)!.submit,
          onPressed: () async {
            if (phoneController.text == '') {
              Tools.showCustomSnackBar(
                text: AppLocalizations.of(context)!.phoneerror,
                isError: true,
                context,
              );
            } else if (passwordController.text == '') {
              Tools.showCustomSnackBar(
                text: AppLocalizations.of(context)!.passerror,
                isError: true,
                context,
              );
            } else {
              final status = await state.login(
                  mobile: phoneController.text,
                  password: passwordController.text);
              if (status == Status.ready) {
                context.goNamed('navigate', pathParameters: {'tab': '0'});
              } else {
                Tools.showCustomSnackBar(
                  text: state.errorsLogin == null
                      ? state.messageLogin ??
                          AppLocalizations.of(context)!.haserror
                      : Tools.combineErrorMessages(state.errorsLogin ?? {}),
                  isError: true,
                  context,
                );
              }
            }
          },
        );
    }
  }
}
