// ignore_for_file: sized_box_for_whitespace

import 'package:flutter/material.dart';
import 'package:tour_travel_agr/api/all_api.dart';
import 'package:tour_travel_agr/helper/argument_route/verification_otp.dart';
import 'package:tour_travel_agr/helper/components_widget/custom_appbar.dart';
import 'package:tour_travel_agr/helper/components_widget/password_input.dart';
import 'package:tour_travel_agr/helper/components_widget/widget_button.dart';
import 'package:tour_travel_agr/helper/components_widget/widget_text_field.dart';
import 'package:tour_travel_agr/helper/modal_dialog.dart';
import 'package:tour_travel_agr/helper/widget_responsive.dart';
import 'package:tour_travel_agr/resource/colors.dart';
import 'package:tour_travel_agr/resource/routes.dart';
import 'package:tour_travel_agr/resource/size.dart';
import 'package:tour_travel_agr/resource/style.dart';

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

  @override
  State<RegisterView> createState() => _RegisterViewState();
}

class _RegisterViewState extends State<RegisterView> {
  final TextEditingController nameController = TextEditingController();

  final TextEditingController nikController = TextEditingController();

  final TextEditingController noHpController = TextEditingController();

  final TextEditingController passwordController = TextEditingController();

  final TextEditingController confirmPasswordController =
      TextEditingController();

  bool isChecked = false;
  bool buttonRregisterActive = false;

  @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: "Register",
          ),
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                children: [
                  const SizedBox(
                    height: 12,
                  ),
                  InputTextField(
                    controller: nameController,
                    labelText: "Nama Lengkap",
                    labelColor: ColorManager.primary,
                    borderSideActive: true,
                    hintText: "Nama anda",
                  ),
                  InputTextField(
                    controller: nikController,
                    labelText: "NIK",
                    labelColor: ColorManager.primary,
                    borderSideActive: true,
                    hintText: "Nomor induk kependudukan",
                    marginTop: AppMargin.m12,
                  ),
                  InputTextField(
                    controller: noHpController,
                    labelText: "Nomor Handphone",
                    labelColor: ColorManager.primary,
                    borderSideActive: true,
                    hintText: "628...",
                    marginTop: AppMargin.m12,
                  ),
                  PasswordInput(
                    passwordController: passwordController,
                    labelColor: ColorManager.primary,
                    marginTop: 12,
                    borderSideActive: true,
                  ),
                  PasswordInput(
                    passwordController: confirmPasswordController,
                    labelColor: ColorManager.primary,
                    marginTop: 12,
                    borderSideActive: true,
                    labelText: "Konfirmasi password",
                  ),
                  Container(
                    margin: EdgeInsets.only(
                      top: AppMargin.m25,
                    ),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                          width: 20,
                          height: 20,
                          child: Checkbox(
                            checkColor: Colors.white,
                            activeColor: ColorManager.primary,
                            value: isChecked,
                            onChanged: (bool? value) {
                              setState(() {
                                buttonRregisterActive = value!;
                                isChecked = value;
                              });
                            },
                          ),
                        ),
                        Expanded(
                          child: Container(
                            margin: const EdgeInsets.only(
                              left: 4,
                            ),
                            child: Text(
                              "Dengan mendaftar layanan ini, anda menyetujui syarat & ketentuan yang berlaku",
                              style: getRegularStyle(
                                color: Colors.black,
                                fontSize: 12,
                              ),
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(
                      top: AppMargin.m25,
                      bottom: AppMargin.m20,
                    ),
                    child: CustomButton(
                      text: "Register",
                      colorButton: buttonRregisterActive
                          ? ColorManager.primary
                          : Colors.grey,
                      onTap: () {
                        if (buttonRregisterActive) {
                          // if (passwordController.text !=
                          //     confirmPasswordController.text) {
                          //   modalDialogGlobal(
                          //     context: context,
                          //     size: MediaQuery.of(context).size,
                          //     title: "Gagal",
                          //     contentBody: "Password tidak sama",
                          //     buttonText: "Ok",
                          //     tapButton: () => Navigator.pop(context),
                          //   );
                          // }
                          // Api.register(
                          //   nameController.text,
                          //   nikController.text,
                          //   noHpController.text,
                          //   passwordController.text,
                          //   confirmPasswordController.text,
                          // ).then((apiResponse) {
                          //   if (apiResponse.error) {
                          //     modalDialogGlobal(
                          //       context: context,
                          //       size: MediaQuery.of(context).size,
                          //       title: "Gagal",
                          //       contentBody: apiResponse.msg,
                          //       buttonText: "Ok",
                          //       tapButton: () => Navigator.pop(context),
                          //     );
                          //     return;
                          //   }

                          //   Navigator.pushNamed(
                          //     context,
                          //     Routes.verificationRoute,
                          //     arguments: apiResponse.data['register_id'],
                          //   );
                          // });

                          Navigator.pushNamed(
                            context,
                            Routes.verificationRoute,
                            arguments: VerificationArguments(
                              id: "ASAS",
                              phone: noHpController.text,
                            ),
                          );
                        }
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
