// ignore_for_file: sized_box_for_whitespace, avoid_unnecessary_containers

import 'package:flutter/material.dart';
import 'package:tour_travel_agr/helper/components_widget/custom_appbar.dart';
import 'package:tour_travel_agr/helper/widget_responsive.dart';
import 'package:tour_travel_agr/resource/assets.dart';
import 'package:tour_travel_agr/resource/colors.dart';
import 'package:tour_travel_agr/resource/font.dart';
import 'package:tour_travel_agr/resource/size.dart';
import 'package:tour_travel_agr/resource/style.dart';

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

  @override
  State<OtpVerificationView> createState() => _OtpVerificationViewState();
}

class _OtpVerificationViewState extends State<OtpVerificationView> {
  final TextEditingController otp1Controller = TextEditingController();
  final TextEditingController otp2Controller = TextEditingController();
  final TextEditingController otp3Controller = TextEditingController();
  final TextEditingController otp4Controller = TextEditingController();

  String finalText = "";
  bool disabledInput = false;

  @override
  void initState() {
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorManager.backgroundColor,
      body: ScreenResponsive(
        widget: bodyWidget(context),
        widthScreen: MediaQuery.of(context).size.width,
      ),
    );
  }

  Container bodyWidget(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        top: AppPadding.safeAreaTop(context),
        left: AppPadding.p20,
        right: AppPadding.p20,
        bottom: AppPadding.safeAreaBot(context),
      ),
      child: Column(
        children: [
          const CustomAppBar(
            text: "OTP Verification",
          ),
          Expanded(
            child: SingleChildScrollView(
                child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  margin: EdgeInsets.only(
                    top: AppMargin.m12,
                  ),
                  child: Center(
                    child: Image(
                      width: 267,
                      height: 243,
                      image: AssetImage(
                        Assets.verificationHeadImage,
                      ),
                    ),
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(
                    top: 40,
                  ),
                  child: Text(
                    "Enter OTP",
                    style: getSemiBoldStyle(
                      color: Colors.black,
                      fontSize: FontSize.s22,
                    ),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(
                    top: AppMargin.m4,
                  ),
                  child: Text(
                    "An 4 digit code has been sent to +91 9995380399",
                    style: getMediumStyle(
                      color: ColorManager.grey,
                    ),
                  ),
                ),
                const CountDownTimeOtp(),
                Container(
                  margin: EdgeInsets.only(
                    top: AppMargin.m16,
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      inputOtpMethod(
                        context,
                        otp1Controller,
                      ),
                      inputOtpMethod(
                        context,
                        otp2Controller,
                      ),
                      inputOtpMethod(
                        context,
                        otp3Controller,
                      ),
                      inputOtpMethod(
                        context,
                        otp4Controller,
                      )
                    ],
                  ),
                ),
              ],
            )),
          )
        ],
      ),
    );
  }

  Container inputOtpMethod(
      BuildContext context, TextEditingController controller) {
    return Container(
      width: 67,
      height: 67,
      child: TextField(
        autofocus: false,
        textAlign: TextAlign.center,
        keyboardType: TextInputType.number,
        maxLength: 1,
        enabled: !disabledInput,
        cursorColor: Colors.black,
        decoration: InputDecoration(
          border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
          counterText: '',
          hintStyle: const TextStyle(
            color: Colors.black,
            fontSize: 20.0,
          ),
          filled: disabledInput,
          fillColor: Colors.grey.withOpacity(0.8),
        ),
        controller: controller,
        onChanged: (value) {
          setState(() {
            finalText =
                "${otp1Controller.text}${otp2Controller.text}${otp3Controller.text}${otp4Controller.text}";
            if (finalText.length >= 4) {
              setState(() {
                disabledInput = true;
              });
              FocusScope.of(context).unfocus();
              // TODO: KALAU DAH LENGKAP NGAPAIN

            } else {
              if (controller.text.isNotEmpty) {
                FocusScope.of(context).nextFocus();
              }
            }
          });
        },
      ),
    );
  }
}

class CountDownTimeOtp extends StatelessWidget {
  const CountDownTimeOtp({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(
        top: AppMargin.m6,
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            "00 :",
            style: getRegularStyle(
              color: ColorManager.primary,
              fontSize: 20,
              fontFamily: FontConstants.mulish,
            ),
          ),
          Text(
            " 02 :",
            style: getRegularStyle(
              color: ColorManager.primary,
              fontSize: 20,
              fontFamily: FontConstants.mulish,
            ),
          ),
          Text(
            " 59",
            style: getRegularStyle(
              color: ColorManager.primary,
              fontSize: 20,
              fontFamily: FontConstants.mulish,
            ),
          )
        ],
      ),
    );
  }
}
