import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:praresi/presentation/controllers/auth_controller.dart';

class RegisterView extends GetView<AuthController> {
  RegisterView({super.key});

  final nameC = TextEditingController();
  final emailC = TextEditingController();
  final passC = TextEditingController();
  final confirmPassC = TextEditingController();

  // RxBool untuk toggle password
  final RxBool isPassHidden = true.obs;
  final RxBool isConfirmPassHidden = true.obs;

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;

    return Scaffold(
      body: Container(
        width: size.width,
        height: size.height,
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            colors: [Color(0xFF1976D2), Color(0xFF42A5F5)], // biru tua → biru muda
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
          ),
        ),
        child: SingleChildScrollView(
          child: Column(
            children: [
              const SizedBox(height: 80),
              const Icon(Icons.person_add_alt_1, size: 80, color: Colors.white),
              const SizedBox(height: 10),
              const Text(
                "Buat Akun Baru",
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 26,
                  fontWeight: FontWeight.bold,
                ),
              ),
              const SizedBox(height: 30),

              // Card form register
              Container(
                width: size.width * 0.9,
                padding: const EdgeInsets.all(20),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(20),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.1),
                      blurRadius: 10,
                      offset: const Offset(0, 5),
                    )
                  ],
                ),
                child: Column(
                  children: [
                    TextField(
                      controller: nameC,
                      decoration: InputDecoration(
                        labelText: "Nama Lengkap",
                        prefixIcon: const Icon(Icons.person_outline),
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12),
                        ),
                      ),
                    ),
                    const SizedBox(height: 15),
                    TextField(
                      controller: emailC,
                      decoration: InputDecoration(
                        labelText: "Email",
                        prefixIcon: const Icon(Icons.email_outlined),
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12),
                        ),
                      ),
                    ),
                    const SizedBox(height: 15),

                    // password
                    Obx(() => TextField(
                          controller: passC,
                          obscureText: isPassHidden.value,
                          decoration: InputDecoration(
                            labelText: "Password",
                            prefixIcon: const Icon(Icons.lock_outline),
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(12),
                            ),
                            suffixIcon: IconButton(
                              icon: Icon(
                                isPassHidden.value
                                    ? Icons.visibility_off
                                    : Icons.visibility,
                              ),
                              onPressed: () =>
                                  isPassHidden.value = !isPassHidden.value,
                            ),
                          ),
                        )),
                    const SizedBox(height: 15),

                    // konfirmasi password
                    Obx(() => TextField(
                          controller: confirmPassC,
                          obscureText: isConfirmPassHidden.value,
                          decoration: InputDecoration(
                            labelText: "Konfirmasi Password",
                            prefixIcon: const Icon(Icons.lock_outline),
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(12),
                            ),
                            suffixIcon: IconButton(
                              icon: Icon(
                                isConfirmPassHidden.value
                                    ? Icons.visibility_off
                                    : Icons.visibility,
                              ),
                              onPressed: () => isConfirmPassHidden.value =
                                  !isConfirmPassHidden.value,
                            ),
                          ),
                        )),
                    const SizedBox(height: 20),

                    // Tombol Register
                    Obx(() {
                        final isLoading = controller.isLoading.value;

                        return SizedBox(
                          width: double.infinity,
                          height: 50,
                          child: ElevatedButton(
                            style: ElevatedButton.styleFrom(
                              backgroundColor: const Color(0xFF1976D2),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(12),
                              ),
                              elevation: 3,
                            ),
                            onPressed: isLoading
                                ? null // ⛔ disable saat loading (cegah double klik)
                                : () {

                                    // ==============================
                                    // VALIDASI FIELD KOSONG
                                    // ==============================
                                    if (nameC.text.trim().isEmpty ||
                                        emailC.text.trim().isEmpty ||
                                        passC.text.trim().isEmpty ||
                                        confirmPassC.text.trim().isEmpty) {

                                      Get.snackbar(
                                        "Registrasi Gagal",
                                        "Semua field wajib diisi.",
                                        snackPosition: SnackPosition.TOP,
                                        backgroundColor: Colors.white,
                                        colorText: Colors.black,
                                      );
                                      return;
                                    }

                                    // ==============================
                                    // VALIDASI FORMAT EMAIL
                                    // ==============================
                                    if (!GetUtils.isEmail(emailC.text.trim())) {
                                      Get.snackbar(
                                        "Registrasi Gagal",
                                        "Format email tidak valid.",
                                        snackPosition: SnackPosition.TOP,
                                        backgroundColor: Colors.white,
                                        colorText: Colors.black,
                                      );
                                      return;
                                    }

                                    // ==============================
                                    // VALIDASI PASSWORD MINIMAL
                                    // ==============================
                                    if (passC.text.trim().length < 6) {
                                      Get.snackbar(
                                        "Registrasi Gagal",
                                        "Password minimal 6 karakter.",
                                        snackPosition: SnackPosition.TOP,
                                        backgroundColor: Colors.white,
                                        colorText: Colors.black,
                                      );
                                      return;
                                    }

                                    // ==============================
                                    // VALIDASI KONFIRMASI PASSWORD
                                    // ==============================
                                    if (passC.text.trim() != confirmPassC.text.trim()) {
                                      Get.snackbar(
                                        "Registrasi Gagal",
                                        "Konfirmasi password tidak sesuai.",
                                        snackPosition: SnackPosition.TOP,
                                        backgroundColor: Colors.white,
                                        colorText: Colors.black,
                                      );
                                      return;
                                    }

                                    // ==============================
                                    // JIKA SEMUA VALID, PANGGIL REGISTER
                                    // ==============================
                                    controller.register(
                                      nameC.text.trim(),
                                      emailC.text.trim(),
                                      passC.text.trim(),
                                    );
                                  },
                            child: isLoading
                                ? const SizedBox(
                                    height: 22,
                                    width: 22,
                                    child: CircularProgressIndicator(
                                      strokeWidth: 2,
                                      color: Colors.white,
                                    ),
                                  )
                                : const Text(
                                    "Register",
                                    style: TextStyle(
                                      fontSize: 16,
                                      fontWeight: FontWeight.bold,
                                      color: Colors.white,
                                    ),
                                  ),
                          ),
                        );
                      }),

                  ],
                ),
              ),

              const SizedBox(height: 20),
              TextButton(
                onPressed: () => Get.back(),
                child: const Text(
                  "Sudah punya akun? Login",
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}