import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:get/get.dart';
import 'package:taxglide/consts/app_asstes.dart';
import 'package:taxglide/consts/app_colors.dart';
import 'package:taxglide/consts/comman_button.dart';
import 'package:taxglide/consts/comman_container_auth.dart';
import 'package:taxglide/consts/comman_textformfileds.dart';
import 'package:taxglide/consts/responsive_helper.dart';
import 'package:taxglide/consts/validation_popup.dart';
import 'package:taxglide/controller/api_contoller.dart';
import 'package:taxglide/router/consts_routers.dart';

class EmployeeLoginScreen extends ConsumerStatefulWidget {
  const EmployeeLoginScreen({super.key});

  @override
  ConsumerState<EmployeeLoginScreen> createState() =>
      _EmployeeLoginScreenState();
}

class _EmployeeLoginScreenState extends ConsumerState<EmployeeLoginScreen> {
  final ValidationPopup _validationPopup = ValidationPopup();
  final TextEditingController _mobileController = TextEditingController();

  @override
  void initState() {
    super.initState();
    final args = Get.arguments;
    if (args != null && args is Map<String, dynamic>) {
      final mobile = args['mobile'] ?? '';
      if (mobile.isNotEmpty) {
        _mobileController.text = mobile;
      }
    }
  }

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

  Future<void> _handleLogin() async {
    final mobile = _mobileController.text.trim();

    if (!_validationPopup.validateMobileNumber(context, mobile)) {
      return;
    }

    await ref.read(employeeloginProvider.notifier).login(mobile);
    final state = ref.read(employeeloginProvider);

    state.when(
      data: (data) {
        if (data['success'] == true) {
          Get.toNamed(ConstRouters.employeeotp, arguments: {'mobile': mobile});
          _validationPopup.showSuccessMessage(
            context,
            "OTP sent successfully!",
          );
        } else if (data['error'] != null) {
          _validationPopup.showErrorMessage(context, data['error'].toString());
        }
      },
      loading: () {},
      error: (err, _) {
        _validationPopup.showErrorMessage(context, "Error: $err");
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    final loginState = ref.watch(employeeloginProvider);

    // Initialize responsive utils
    final r = ResponsiveUtils(context);

    // Responsive values
    final logoWidth = r.getValue<double>(
      mobile: 120,
      tablet: 141,
      desktop: 160,
    );
    final logoHeight = r.getValue<double>(
      mobile: 85,
      tablet: 100,
      desktop: 115,
    );
    final titleFontSize = r.fontSize(mobile: 26, tablet: 32, desktop: 36);
    final subtitleFontSize = r.fontSize(mobile: 13, tablet: 14, desktop: 15);
    final termsFontSize = r.fontSize(mobile: 10.5, tablet: 11.34, desktop: 12);
    final linkFontSize = r.fontSize(mobile: 13, tablet: 14, desktop: 15);
    final otherLoginFontSize = r.fontSize(mobile: 12, tablet: 13, desktop: 14);

    final spacingXS = r.spacing(mobile: 10, tablet: 10, desktop: 12);
    final spacingSM = r.spacing(mobile: 15, tablet: 20, desktop: 24);
    final spacingMD = r.spacing(mobile: 20, tablet: 20, desktop: 24);
    final spacingLG = r.spacing(mobile: 20, tablet: 22, desktop: 28);

    return Scaffold(
      resizeToAvoidBottomInset: true,
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: [
              Color(0xFFFFF8F0),
              Color(0xFFEBC894),
              Color(0xFFE8DAF2),
              Color(0xFFB49EF4),
            ],
            stops: [0.0, 0.3, 0.6, 1.0],
          ),
        ),
        child: SingleChildScrollView(
          child: ConstrainedBox(
            constraints: BoxConstraints(minHeight: r.screenHeight),
            child: IntrinsicHeight(
              child: Center(
                child: CommonContainerAuth(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      // Logo
                      Image.asset(
                        AppAssets.taxgildelogoauth,
                        width: logoWidth,
                        height: logoHeight,
                        fit: BoxFit.contain,
                      ),
                      SizedBox(height: spacingMD),

                      // Login Title
                      Text(
                        "Login",
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontFamily: 'Gilroy-SemiBold',
                          fontSize: titleFontSize,
                          fontWeight: FontWeight.w600,
                          height: 1.3,
                          letterSpacing: 0.01 * titleFontSize,
                          color: AppColors.authheading,
                        ),
                      ),
                      SizedBox(height: spacingMD),

                      // Subtitle
                      Text(
                        "Enter your registered Mobile Number",
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontFamily: 'Gilroy-SemiBold',
                          fontSize: subtitleFontSize,
                          fontWeight: FontWeight.w600,
                          height: 1.4,
                          letterSpacing: 0.03 * subtitleFontSize,
                          color: AppColors.authleading,
                        ),
                      ),
                      SizedBox(height: spacingLG),

                      // Mobile Number Input
                      CommanTextFormField(
                        controller: _mobileController,
                        hintText: 'Enter your Mobile Number',
                        keyboardType: TextInputType.phone,
                        prefixIcon: Icons.phone_android,
                      ),
                      SizedBox(height: spacingLG),

                      // Terms and Conditions
                      Text.rich(
                        TextSpan(
                          text: "By signing up, you agree to the ",
                          style: TextStyle(
                            fontFamily: 'Gilroy-Medium',
                            fontWeight: FontWeight.w500,
                            fontSize: termsFontSize,
                            height: 1.8,
                            letterSpacing: 0.01,
                            color: AppColors.authleading,
                          ),
                          children: [
                            TextSpan(
                              text: "Terms of Service ",
                              style: TextStyle(
                                fontFamily: 'Gilroy-Bold',
                                fontWeight: FontWeight.w700,
                                fontSize: termsFontSize,
                                height: 1.8,
                                letterSpacing: 0.01,
                                color: AppColors.authtermsandcondition,
                              ),
                            ),
                            TextSpan(
                              text: "and ",
                              style: TextStyle(
                                fontFamily: 'Gilroy-Medium',
                                fontWeight: FontWeight.w500,
                                fontSize: termsFontSize,
                                height: 1.8,
                                letterSpacing: 0.01,
                                color: AppColors.authleading,
                              ),
                            ),
                            TextSpan(
                              text: "Data Processing Agreement",
                              style: TextStyle(
                                fontFamily: 'Gilroy-Bold',
                                fontWeight: FontWeight.w700,
                                fontSize: termsFontSize,
                                height: 1.8,
                                letterSpacing: 0.01,
                                color: AppColors.authtermsandcondition,
                              ),
                            ),
                          ],
                        ),
                        textAlign: TextAlign.center,
                      ),

                      SizedBox(height: spacingLG),

                      // Login Button or Loading
                      loginState.isLoading
                          ? const CircularProgressIndicator()
                          : CommanButton(
                              text: "Login",
                              onPressed: _handleLogin,
                            ),

                      SizedBox(height: spacingSM),

                      Text(
                        "Other Login",
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontFamily: 'Inter',
                          fontWeight: FontWeight.w500,
                          fontSize: otherLoginFontSize,
                          height: 1.8,
                          letterSpacing: 0.13,
                          color: const Color(0xFF6C7278),
                        ),
                      ),

                      SizedBox(height: spacingXS),

                      Text.rich(
                        TextSpan(
                          text: "User Login? ",
                          style: TextStyle(fontSize: linkFontSize),
                          children: [
                            TextSpan(
                              text: "Click Here",
                              style: TextStyle(
                                fontSize: linkFontSize,
                                color: AppColors.authsignup,
                                fontWeight: FontWeight.bold,
                              ),
                              recognizer: TapGestureRecognizer()
                                ..onTap = () {
                                  Get.offNamed(ConstRouters.login);
                                },
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
