import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:recording_tool/app/router/app_navigator.dart';
import 'package:recording_tool/core/cache/app_storage.dart';
import 'package:recording_tool/core/cache/storage_keys.dart';
import 'package:recording_tool/features/auth/view_model_auth/view_model_auth.dart';
import 'package:recording_tool/features/scan_qrcode/pages/page_scan_qrcode.dart';
import 'package:recording_tool/shared/widgets/widgets.dart';

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

  @override
  ConsumerState<AuthPageWidget> createState() => _AuthPageWidgetState();
}

class _AuthPageWidgetState extends ConsumerState<AuthPageWidget> {
  late TextEditingController? _controller;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
    _controller?.text = '555';

    WidgetsBinding.instance.addPostFrameCallback((_) async {
      final token = AppStorage.getString(StorageKeys.authToken);
      if (token?.isNotEmpty ?? false) {
        AppNavigator.push(const ScanQrCodePage());
      }
    });
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final authState = ref.watch(authProvider);

    return Center(
      child: Column(
        children: [
          SizedBox(height: 180.h),
          AppText('裁判工作台', fontSize: 30.sp),
          SizedBox(height: 20.h),
          Text(
            '输入执裁口令',
            style: TextStyle(fontSize: 18.sp, color: Colors.black),
          ),
          SizedBox(height: 20.h),

          Container(
            padding: EdgeInsets.symmetric(horizontal: 20.w),
            child: Column(
              children: [
                AppTextField(controller: _controller),
                SizedBox(height: 20.h),
                SizedBox(
                  width: double.maxFinite,
                  child: AppButton(
                    label: '确定',
                    onPressed: () async {
                      final code = _controller?.text;
                      final success = await ref
                          .read(authProvider.notifier)
                          .auth(code ?? '');
                      if (!mounted) return;
                      if (success) {
                        AppNavigator.push(const ScanQrCodePage());
                        return;
                      }
                      final message = ref.read(authProvider).errorMessage;
                      if (message != null && message.isNotEmpty) {
                        AppToast.show(message);
                      }
                    },
                    variant: AppButtonVariant.secondary,
                    isLoading: authState.isLoading,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
