import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:niogu_app/core/constants/app_asset.dart';
import 'package:niogu_app/core/constants/app_color.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
import 'package:niogu_app/core/router/app_route.dart';
import 'package:niogu_app/core/utils/log_message.dart';
import 'package:pinput/pinput.dart';
import 'package:sizer/sizer.dart';

class VerificationOtpScreen extends StatefulWidget {
  final String identifier; // Email atau No WhatsApp
  const VerificationOtpScreen({super.key, required this.identifier});

  @override
  State<VerificationOtpScreen> createState() => _VerificationOtpScreenState();
}

class _VerificationOtpScreenState extends State<VerificationOtpScreen> {
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        final bool isTablet = 100.w >= 600;

        final defaultPinTheme = PinTheme(
          width: 13.w,
          height: 15.w,
          textStyle: TextStyle(
            fontSize: AppFontSize.medium.sp,
            fontWeight: FontWeight.bold,
            color: Colors.black,
          ),
          decoration: BoxDecoration(
            color: Colors.grey[50],
            borderRadius: BorderRadius.circular(2.5.w),
            border: Border.all(color: Colors.grey.shade300),
          ),
        );

        final focusedPinTheme = defaultPinTheme.copyDecorationWith(
          border: Border.all(color: AppColor.primaryColor, width: 1.5),
          borderRadius: BorderRadius.circular(2.5.w),
        );

        final errorPinTheme = defaultPinTheme.copyDecorationWith(
          border: Border.all(color: Colors.red, width: 1.5),
          borderRadius: BorderRadius.circular(2.5.w),
        );
        return SafeArea(
          top: false,
          bottom: true,
          right: false,
          left: false,
          child: Scaffold(
            backgroundColor: Colors.white,
            appBar: AppBar(
              backgroundColor: Colors.white,
              elevation: 0,
              leading: IconButton(
                icon: const Icon(Icons.arrow_back, color: Colors.black),
                onPressed: () => Navigator.pop(context),
              ),
            ),
            body: SingleChildScrollView(
              padding: EdgeInsets.symmetric(horizontal: 8.w),
              child: Column(
                children: [
                  Image.asset(AppAsset.LOGO_UPDATE, height: 10.h),

                  SizedBox(height: 4.h),

                  Text(
                    "Verifikasi Kode OTP",
                    style: TextStyle(
                      fontSize: AppFontSize.medium.sp,
                      fontWeight: FontWeight.bold,
                      color: AppColor.primaryColor,
                    ),
                  ),

                  SizedBox(height: 1.5.h),

                  Text(
                    "Masukkan 6 digit kode yang dikirimkan ke",
                    style: TextStyle(
                      fontSize: isTablet
                          ? (AppFontSize.medium - 1).sp
                          : (AppFontSize.small - 1).sp,
                      color: Colors.black,
                    ),
                  ),
                  SizedBox(height: 0.5.h),
                  Text(
                    "08123456789",
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: isTablet
                          ? (AppFontSize.medium - 1).sp
                          : (AppFontSize.small - 1).sp,
                    ),
                  ),

                  SizedBox(height: 1.h),

                  GestureDetector(
                    onTap: () => context.pop(),
                    child: Text(
                      "Nomor Salah? Ubah di sini",
                      style: TextStyle(
                        color: AppColor.primaryColor,
                        fontWeight: FontWeight.bold,
                        fontSize: isTablet
                            ? (AppFontSize.medium - 1).sp
                            : (AppFontSize.small - 1).sp,
                      ),
                    ),
                  ),

                  SizedBox(height: 6.h),

                  Pinput(
                    length: 6,
                    defaultPinTheme: defaultPinTheme,
                    focusedPinTheme: focusedPinTheme,
                    separatorBuilder: (index) => SizedBox(width: 2.w),
                    hapticFeedbackType: HapticFeedbackType.lightImpact,
                    onCompleted: (pin) => LogMessage.log.i(pin),
                    cursor: Column(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Container(
                          margin: EdgeInsets.only(bottom: 2.w),
                          width: 5.w,
                          height: 1,
                          color: AppColor.primaryColor,
                        ),
                      ],
                    ),
                    errorText: "Kode otp salah",
                    errorTextStyle: TextStyle(
                      color: Colors.red,
                      fontSize: isTablet
                          ? (AppFontSize.medium - 1).sp
                          : (AppFontSize.small - 1).sp,
                    ),
                    errorPinTheme: errorPinTheme,
                    validator: (_) => null,
                    pinputAutovalidateMode: PinputAutovalidateMode.onSubmit,
                  ),

                  SizedBox(height: 4.h),

                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        "Belum dapat kode? ",
                        style: TextStyle(
                          fontSize: isTablet
                              ? (AppFontSize.medium - 1).sp
                              : (AppFontSize.small - 1).sp,
                          color: Colors.grey,
                        ),
                      ),
                      TextButton(
                        onPressed: null,
                        child: Text(
                          "Minta ulang (00:30)",
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: isTablet
                                ? (AppFontSize.medium - 1).sp
                                : (AppFontSize.small - 1).sp,
                            color: AppColor.primaryColor,
                          ),
                        ),
                      ),
                    ],
                  ),

                  SizedBox(height: 4.h),

                  ElevatedButton(
                    onPressed: () =>
                        context.pushNamed(AppRoute.changePasswordScreen),
                    style: ElevatedButton.styleFrom(
                      backgroundColor: AppColor.primaryColor,
                      minimumSize: Size(double.infinity, 7.h),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(3.w),
                      ),
                      elevation: 2,
                    ),
                    child: Text(
                      "Verifikasi Sekarang",
                      style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: AppFontSize.medium.sp,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
