import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:rasadyar_auth/presentation/widget/captcha/view.dart';
import 'package:rasadyar_auth/presentation/widget/clear_button.dart';
import 'package:rasadyar_auth/presentation/widget/logo_widget.dart';
import 'package:rasadyar_core/core.dart';

import 'logic.dart';

class AuthPage extends GetView<AuthLogic> {
  const AuthPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: [
            SizedBox(height: 80),
            LogoWidget(),
            ObxValue((types) {
              switch (types.value) {
                case AuthType.otp:
                //return otpForm();
                case AuthType.useAndPass:
                  return useAndPassFrom();
              }
            }, controller.authType),

            /*            SizedBox(height: 50),
            RichText(
              text: TextSpan(
                children: [
                  TextSpan(
                    text: 'مطالعه بیانیه ',
                    style: AppFonts.yekan14.copyWith(
                      color: AppColor.darkGreyDark,
                    ),
                  ),
                  TextSpan(
                    recognizer: TapGestureRecognizer()
                      ..onTap = () {},
                    text: 'حریم خصوصی',
                    style: AppFonts.yekan14.copyWith(
                      color: AppColor.blueNormal,
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(height: 18),

            ObxValue((types) {
              return RichText(
                text: TextSpan(
                  children: [
                    TextSpan(
                      recognizer:
                      TapGestureRecognizer()
                        ..onTap = () {
                          if (controller.authType.value == AuthType.otp) {
                            controller.authType.value = AuthType.useAndPass;
                            if (controller.otpStatus.value !=
                                OtpStatus.init) {
                              controller.otpStatus.value = OtpStatus.init;
                            }
                          } else {
                            controller.authType.value = AuthType.otp;
                          }
                        },
                      text:
                      controller.authType.value == AuthType.otp
                          ? 'ورود با رمز ثابت'
                          : 'ورود با رمز یکبار مصرف',

                      style: AppFonts.yekan14.copyWith(
                        color: AppColor.blueNormal,
                      ),
                    ),
                  ],
                ),
              );
            }, controller.authType),*/
          ],
        ),
      ),
    );
  }

  Widget useAndPassFrom() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 30, vertical: 50),
      child: Form(
        key: controller.formKey,
        child: Column(
          children: [
            RTextField(
              label: 'نام کاربری',
              maxLength: 11,
              maxLines: 1,
              controller: controller.phoneNumberController.value,
              keyboardType: TextInputType.number,
              initText: controller.phoneNumberController.value.text,
              onChanged: (value) async {
                controller.phoneNumberController.value.text = value;
                controller.phoneNumberController.refresh();
                if (value.length == 11) {
                  await controller.getUserInfo(value);
                }
              },
              prefixIcon: Padding(
                padding: const EdgeInsets.fromLTRB(0, 8, 6, 8),
                child: Assets.vec.callSvg.svg(width: 12, height: 12),
              ),
              suffixIcon:
                  controller.phoneNumberController.value.text.trim().isNotEmpty
                  ? clearButton(() {
                      controller.phoneNumberController.value.clear();
                      controller.phoneNumberController.refresh();
                    })
                  : null,
              validator: (value) {
                if (value == null || value.isEmpty) {
                  return '⚠️ شماره موبایل را وارد کنید';
                } else if (value.length < 11) {
                  return '⚠️ شماره موبایل باید 11 رقم باشد';
                }
                return null;
              },
              style: AppFonts.yekan13,
              errorStyle: AppFonts.yekan13.copyWith(color: AppColor.redNormal),
              labelStyle: AppFonts.yekan13,
              boxConstraints: const BoxConstraints(
                maxHeight: 40,
                minHeight: 40,
                maxWidth: 40,
                minWidth: 40,
              ),
            ),
            const SizedBox(height: 26),
            ObxValue(
              (passwordController) => RTextField(
                label: 'رمز عبور',
                filled: false,
                obscure: true,
                controller: passwordController.value,
                variant: RTextFieldVariant.password,
                initText: passwordController.value.text,
                onChanged: (value) {
                  passwordController.refresh();
                },
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '⚠️ رمز عبور را وارد کنید';
                  }
                  return null;
                },
                style: AppFonts.yekan13,
                errorStyle: AppFonts.yekan13.copyWith(
                  color: AppColor.redNormal,
                ),
                labelStyle: AppFonts.yekan13,
                prefixIcon: Padding(
                  padding: const EdgeInsets.fromLTRB(0, 8, 8, 8),
                  child: Assets.vec.keySvg.svg(width: 12, height: 12),
                ),
                boxConstraints: const BoxConstraints(
                  maxHeight: 34,
                  minHeight: 34,
                  maxWidth: 34,
                  minWidth: 34,
                ),
              ),
              controller.passwordController,
            ),
            SizedBox(height: 26),
            CaptchaWidget(),
            SizedBox(height: 23),

            Obx(() {
              return RElevated(
                text: 'ورود',
                isLoading: controller.isLoading.value,
                onPressed: controller.isDisabled.value
                    ? null
                    : () async {
                        await controller.submitLoginForm2();
                      },
                width: Get.width,
                height: 48,
              );
            }),
          ],
        ),
      ),
    );
  }

  /*
  Widget sendCodeForm() {
    return ObxValue((data) {
      return Form(
        key: data.value,
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 30, vertical: 50),
          child: Column(
            children: [
              SizedBox(height: 26),
              ObxValue((phoneController) {
                return TextFormField(
                  controller: phoneController.value,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(8),
                      gapPadding: 11,
                    ),
                    labelText: 'شماره موبایل',
                    labelStyle: AppFonts.yekan13,
                    errorStyle: AppFonts.yekan13.copyWith(
                      color: AppColor.redNormal,
                    ),
                    prefixIconConstraints: BoxConstraints(
                      maxHeight: 40,
                      minHeight: 40,
                      maxWidth: 40,
                      minWidth: 40,
                    ),
                    prefixIcon: Padding(
                      padding: const EdgeInsets.fromLTRB(0, 8, 6, 8),
                      child: vecWidget(Assets.vecCallSvg),
                    ),
                    suffix:
                        phoneController.value.text.trim().isNotEmpty
                            ? clearButton(() {
                              phoneController.value.clear();
                              phoneController.refresh();
                            })
                            : null,
                    counterText: '',
                  ),
                  keyboardType: TextInputType.numberWithOptions(
                    decimal: false,
                    signed: false,
                  ),
                  maxLines: 1,
                  maxLength: 11,
                  onChanged: (value) {
                    if (controller.isOnError.value) {
                      controller.isOnError.value = !controller.isOnError.value;
                      data.value.currentState?.reset();
                      data.refresh();
                      phoneController.value.text = value;
                    }
                    phoneController.refresh();
                  },
                  textInputAction: TextInputAction.next,
                  validator: (value) {
                    if (value == null) {
                      return '⚠️ شماره موبایل را وارد کنید';
                    } else if (value.length < 11) {
                      return '⚠️ شماره موبایل باید 11 رقم باشد';
                    }
                    return null;
                  },
                  style: AppFonts.yekan13,
                );
              }, controller.phoneOtpNumberController),

              SizedBox(height: 26),

              CaptchaWidget(),

              SizedBox(height: 23),
              RElevated(
                text: 'ارسال رمز یکبار مصرف',
                onPressed: () {
                  if (data.value.currentState?.validate() == true) {
                    controller.otpStatus.value = OtpStatus.sent;
                    controller.startTimer();
                  }
                },
                width: Get.width,
                height: 48,
              ),
            ],
          ),
        ),
      );
    }, controller.formKeyOtp);
  }

  Widget confirmCodeForm() {
    return ObxValue((data) {
      return Form(
        key: data.value,
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 30, vertical: 50),
          child: Column(
            children: [
              SizedBox(height: 26),

              ObxValue((passwordController) {
                return TextFormField(
                  controller: passwordController.value,
                  obscureText: controller.hidePassword.value,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(8),
                      gapPadding: 11,
                    ),
                    labelText: 'رمز عبور',
                    labelStyle: AppFonts.yekan13,
                    errorStyle: AppFonts.yekan13.copyWith(
                      color: AppColor.redNormal,
                    ),

                    prefixIconConstraints: BoxConstraints(
                      maxHeight: 34,
                      minHeight: 34,
                      maxWidth: 34,
                      minWidth: 34,
                    ),
                    prefixIcon: Padding(
                      padding: const EdgeInsets.fromLTRB(0, 8, 8, 8),
                      child: vecWidget(Assets.vecKeySvg),
                    ),
                    suffix:
                        passwordController.value.text.trim().isNotEmpty
                            ? GestureDetector(
                              onTap: () {
                                controller.hidePassword.value =
                                    !controller.hidePassword.value;
                              },
                              child: Icon(
                                controller.hidePassword.value
                                    ? CupertinoIcons.eye
                                    : CupertinoIcons.eye_slash,
                              ),
                            )
                            : null,
                    counterText: '',
                  ),
                  textInputAction: TextInputAction.done,
                  keyboardType: TextInputType.visiblePassword,
                  maxLines: 1,
                  onChanged: (value) {
                    if (controller.isOnError.value) {
                      controller.isOnError.value = !controller.isOnError.value;
                      data.value.currentState?.reset();
                      passwordController.value.text = value;
                    }
                    passwordController.refresh();
                  },
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return '⚠️ رمز عبور را وارد کنید'; // "Please enter the password"
                    }
                    return null;
                  },
                  style: AppFonts.yekan13,
                );
              }, controller.passwordController),

              SizedBox(height: 23),

              ObxValue((timer) {
                if (timer.value == 0) {
                  return TextButton(
                    onPressed: () {
                      controller.otpStatus.value = OtpStatus.reSend;
                      controller.startTimer();
                    },
                    child: Text(
                      style: AppFonts.yekan13.copyWith(
                        color: AppColor.blueNormal,
                      ),
                      'ارسال مجدد کد یکبار مصرف',
                    ),
                  );
                } else {
                  return Text(
                    'اعتبار رمز ارسال شده ${controller.timeFormatted}',
                    style: AppFonts.yekan13,
                  );
                }
              }, controller.secondsRemaining),

              RichText(
                text: TextSpan(
                  children: [
                    TextSpan(
                      text: ' کد ارسال شده به شماره ',
                      style: AppFonts.yekan14.copyWith(
                        color: AppColor.darkGreyDark,
                      ),
                    ),
                    TextSpan(
                      text: controller.phoneOtpNumberController.value.text,
                      style: AppFonts.yekan13Bold.copyWith(
                        color: AppColor.darkGreyDark,
                      ),
                    ),
                    TextSpan(
                      recognizer:
                          TapGestureRecognizer()
                            ..onTap = () {
                              controller.otpStatus.value = OtpStatus.init;
                            },
                      text: '   ویرایش',
                      style: AppFonts.yekan14.copyWith(
                        color: AppColor.blueNormal,
                      ),
                    ),
                  ],
                ),
              ),

              SizedBox(height: 23),
              RElevated(
                text: 'ورود',
                onPressed: () {
                  if (controller.formKeyOtp.value.currentState?.validate() ==
                      true) {}
                },
                width: Get.width,
                height: 48,
              ),
            ],
          ),
        ),
      );
    }, controller.formKeySentOtp);
  }*/
}
