import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:get/get.dart';
import 'package:SEFER/constant/box_name.dart';
import 'package:SEFER/constant/colors.dart';
import 'package:SEFER/constant/style.dart';
import 'package:SEFER/main.dart';
import 'package:SEFER/views/auth/captin/login_captin.dart';
import 'package:SEFER/views/widgets/elevated_btn.dart';
import 'package:SEFER/views/widgets/my_scafold.dart';
import 'package:SEFER/views/widgets/my_textField.dart';

import '../../constant/info.dart';
import '../../controller/auth/login_controller.dart';
import '../home/profile/passenger_profile_page.dart';
import '../widgets/mycircular.dart';
import 'register_page.dart';

class LoginPage extends StatelessWidget {
  final controller = Get.put(LoginController());

  LoginPage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(LoginController());
    return GetBuilder<LoginController>(
      builder: (controller) => MyScafolld(
        title: 'Login'.tr,
        isleading: false,
        action: GestureDetector(
            onLongPress: () {
              Get.defaultDialog(
                title: 'Admin DashBoard',
                titleStyle: AppStyle.title,
                content: Form(
                  key: controller.formKeyAdmin,
                  child: Column(
                    children: [
                      MyTextForm(
                          controller: controller.adminNameController,
                          label: 'Your name'.tr,
                          hint: 'Your name',
                          type: TextInputType.name),
                      const SizedBox(
                        height: 10,
                      ),
                      MyTextForm(
                          controller: controller.adminPasswordController,
                          label: 'Your password'.tr,
                          hint: 'Your password',
                          type: TextInputType.emailAddress),
                      const SizedBox(
                        height: 10,
                      ),
                    ],
                  ),
                ),
                confirm: MyElevatedButton(
                  title: 'title'.tr,
                  onPressed: () {
                    controller.adminDashboardOpen();
                  },
                ),
              );
            },
            child: const Icon(
              Icons.dashboard,
              color: AppColor.redColor,
            )),
        body: [
          if (box.read(BoxName.agreeTerms) != 'agreed')
            agreedPage()
          else if (box.read(BoxName.countryCode) == null)
            CountryPicker()
          else
            SingleChildScrollView(
              child: Column(
                children: [
                  Padding(
                      padding: const EdgeInsets.all(25),
                      child: Container(
                        decoration: const BoxDecoration(
                          boxShadow: [
                            BoxShadow(
                                offset: Offset(3, 3),
                                color: AppColor.accentColor,
                                blurRadius: 3)
                          ],
                          color: AppColor.secondaryColor,
                        ),
                        child: Form(
                          key: controller.formKey,
                          child: Padding(
                            padding: const EdgeInsets.all(16.0),
                            child: SingleChildScrollView(
                              child: Column(
                                children: [
                                  TextFormField(
                                    keyboardType: TextInputType.emailAddress,
                                    controller: controller.emailController,
                                    decoration: InputDecoration(
                                      focusedBorder: OutlineInputBorder(
                                        borderSide: const BorderSide(
                                          color: AppColor.primaryColor,
                                          width: 2.0,
                                        ),
                                        borderRadius: BorderRadius.circular(10),
                                      ),
                                      fillColor: AppColor.accentColor,
                                      hoverColor: AppColor.accentColor,
                                      focusColor: AppColor.accentColor,
                                      border: const OutlineInputBorder(
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(12))),
                                      labelText: 'Email'.tr,
                                      hintText: 'Enter your email address'.tr,
                                    ),
                                    validator: (value) {
                                      if (value!.isEmpty ||
                                          (!value.contains('@') ||
                                              !value.contains('.'))) {
                                        return 'Please enter Your Email.'.tr;
                                      }
                                      return null;
                                    },
                                  ),
                                  const SizedBox(
                                    height: 30,
                                  ),
                                  TextFormField(
                                    keyboardType: TextInputType.phone,
                                    cursorColor: AppColor.accentColor,
                                    controller: controller.phoneController,
                                    decoration: InputDecoration(
                                      focusedBorder: OutlineInputBorder(
                                        borderSide: const BorderSide(
                                          color: AppColor.primaryColor,
                                          width: 2.0,
                                        ),
                                        borderRadius: BorderRadius.circular(10),
                                      ),
                                      focusColor: AppColor.accentColor,
                                      fillColor: AppColor.accentColor,
                                      border: const OutlineInputBorder(
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(12))),
                                      labelText: 'Phone'.tr,
                                      hintText: 'Enter your phone number'.tr,
                                    ),
                                    validator: (value) {
                                      if (value!.isEmpty ||
                                          value.length != 10) {
                                        return 'Please enter your phone number.'
                                            .tr;
                                      }
                                      return null;
                                    },
                                  ),
                                  const SizedBox(
                                    height: 15,
                                  ),
                                  TextFormField(
                                    obscureText: true,
                                    keyboardType: TextInputType.emailAddress,
                                    controller: controller.passwordController,
                                    decoration: InputDecoration(
                                      focusedBorder: OutlineInputBorder(
                                        borderSide: const BorderSide(
                                          color: AppColor.primaryColor,
                                          width: 2.0,
                                        ),
                                        borderRadius: BorderRadius.circular(10),
                                      ),
                                      fillColor: AppColor.accentColor,
                                      hoverColor: AppColor.accentColor,
                                      focusColor: AppColor.accentColor,
                                      border: const OutlineInputBorder(
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(12))),
                                      labelText: 'Password'.tr,
                                      hintText:
                                          'Please enter your phone number.'.tr,
                                    ),
                                    validator: (value) {
                                      if (value!.isEmpty) {
                                        return 'Please enter Your Password.'.tr;
                                      }
                                      if (value.length < 6) {
                                        return 'Password must br at least 6 character.'
                                            .tr;
                                      }
                                      return null;
                                    },
                                  ),
                                  GetBuilder<LoginController>(
                                    builder: (controller) => controller
                                            .isloading
                                        ? const MyCircularProgressIndicator()
                                        : MyElevatedButton(
                                            onPressed: () {
                                              if (controller
                                                  .formKey.currentState!
                                                  .validate()) {
                                                controller.login();
                                              }
                                            },
                                            title: 'Submit'.tr,
                                          ),
                                  )
                                ],
                              ),
                            ),
                          ),
                        ),
                      )),
                  Text(
                    'if you dont have account'.tr,
                    style: AppStyle.subtitle,
                  ),
                  AnimatedTextKit(
                    onTap: () => Get.to(() => const RegisterPage()),
                    animatedTexts: [
                      TypewriterAnimatedText(
                        'Register'.tr,
                        textStyle: AppStyle.headTitle2,
                        speed: const Duration(milliseconds: 200),
                      ),
                    ],
                    totalRepeatCount: 4,
                    pause: const Duration(milliseconds: 200),
                    displayFullTextOnTap: true,
                    stopPauseOnTap: true,
                  ),
                  // const Spacer(),
                  const SizedBox(
                    height: 100,
                  ),
                  GestureDetector(
                    onTap: () => Get.to(LoginCaptin()),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        const Icon(
                          Icons.drive_eta_rounded,
                          size: 50,
                        ),
                        Text(
                          'If You Want be Driver \nClick Here.'.tr,
                          style: AppStyle.headTitle2,
                          textAlign: TextAlign.center,
                        ),
                      ],
                    ),
                  ),
                  IconButton(
                    onPressed: () {
                      box.remove(BoxName.countryCode);
                    },
                    icon: const Icon(Icons.delete),
                  ),
                ],
              ),
            )
        ],
      ),
    );
  }

  Padding agreedPage() {
    return Padding(
      padding: const EdgeInsets.all(16),
      child: SingleChildScrollView(
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Image.asset(
                  'assets/images/notepad.png',
                  width: Get.width * .2,
                ),
                SizedBox(
                  width: Get.width * .7,
                  child: Text(
                    'Accept Ride\'s Terms & Review Privacy Notice'.tr,
                    style: AppStyle.headTitle2,
                  ),
                ),
              ],
            ),
            const SizedBox(
              height: 30,
            ),
            RichText(
              text: TextSpan(
                text:
                    'By selecting "I Agree" below, I have reviewed and agree to the Terms of Use and acknowledge the '
                        .tr,
                style: AppStyle.title,
                children: <TextSpan>[
                  TextSpan(
                      text: 'Privacy Notice'.tr,
                      style: const TextStyle(
                          decoration: TextDecoration.underline,
                          color: AppColor.blueColor),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          Get.defaultDialog(
                              title: ''.tr,
                              content: const SizedBox(
                                height: 400,
                                width: 400,
                                child: SingleChildScrollView(
                                  child:
                                      HtmlWidget(AppInformation.privacyPolicy),
                                ),
                              ));
                        }),
                  TextSpan(
                    text: '. I am at least 18 years of age.'.tr,
                  ),
                ],
              ),
            ),
            const SizedBox(
              height: 100,
            ),
            GetBuilder<LoginController>(
              builder: (controller) => Column(
                children: [
                  Row(
                    children: [
                      Checkbox.adaptive(
                        autofocus: true,
                        tristate: true,
                        splashRadius: 25,
                        activeColor: AppColor.primaryColor,
                        value: controller.isAgreeTerms,
                        onChanged: (value) => controller.changeAgreeTerm(),
                      ),
                      Text(
                        'I Agree'.tr,
                        style: controller.isAgreeTerms
                            ? AppStyle.title
                            : AppStyle.title
                                .copyWith(color: AppColor.accentColor),
                      ),
                    ],
                  ),
                  MyElevatedButton(
                      title: 'Submit'.tr,
                      onPressed: () => controller.saveAgreementTerms()),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
