import 'package:SEFER/views/auth/login_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.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/controller/profile/profile_controller.dart';
import 'package:SEFER/main.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 'package:SEFER/views/widgets/mycircular.dart';

import '../../../controller/auth/login_controller.dart';
import '../../../controller/functions/log_out.dart';

class PassengerProfilePage extends StatelessWidget {
  PassengerProfilePage({super.key});
  LogOutController logOutController = Get.put(LogOutController());
  @override
  Widget build(BuildContext context) {
    Get.put(ProfileController());

    return MyScafolld(
      isleading: true,
      title: 'My Profile'.tr,
      body: [
        GetBuilder<ProfileController>(
            builder: (controller) => controller.isloading
                ? const MyCircularProgressIndicator()
                : Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 15),
                    child: SizedBox(
                      height: Get.height,
                      child: SingleChildScrollView(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              'Edit Profile'.tr,
                              style: AppStyle.headTitle2,
                            ),
                            ListTile(
                              title: Text(
                                'Name'.tr,
                                style: AppStyle.title,
                              ),
                              leading: const Icon(
                                Icons.person_pin_rounded,
                                size: 35,
                              ),
                              trailing: const Icon(Icons.arrow_forward_ios),
                              subtitle: Text(
                                  '${controller.prfoileData['first_name']} ${controller.prfoileData['last_name']}'),
                              onTap: () {
                                controller.updatField(
                                    'first_name', TextInputType.name);
                              },
                            ),
                            ListTile(
                              title: Text(
                                'Gender'.tr,
                                style: AppStyle.title,
                              ),
                              leading: Image.asset(
                                'assets/images/gender.png',
                                width: 35,
                              ),
                              trailing: const Icon(Icons.arrow_forward_ios),
                              subtitle: Text(
                                  controller.prfoileData['gender'].toString()),
                              onTap: () {
                                Get.defaultDialog(
                                    title: 'Update Gender'.tr,
                                    content: Column(
                                      children: [
                                        GenderPicker(),
                                        MyElevatedButton(
                                          title: 'Update'.tr,
                                          onPressed: () {
                                            controller.updateColumn({
                                              'id': controller.prfoileData['id']
                                                  .toString(),
                                              'gender': controller.gender,
                                            });
                                            Get.back();
                                          },
                                        )
                                      ],
                                    ));
                                // controller.updatField('gender');
                              },
                            ),
                            ListTile(
                              title: Text(
                                'Education'.tr,
                                style: AppStyle.title,
                              ),
                              leading: Image.asset(
                                'assets/images/education.png',
                                width: 35,
                              ),
                              trailing: const Icon(Icons.arrow_forward_ios),
                              subtitle: Text(controller.prfoileData['education']
                                  .toString()),
                              onTap: () {
                                Get.defaultDialog(
                                    barrierDismissible: true,
                                    title: 'Update Education'.tr,
                                    content: SizedBox(
                                      height: 200,
                                      child: Column(
                                        children: [
                                          EducationDegreePicker(),
                                        ],
                                      ),
                                    ),
                                    confirm: MyElevatedButton(
                                      title: 'Update Education'.tr,
                                      onPressed: () {
                                        controller.updateColumn({
                                          'id': controller.prfoileData['id']
                                              .toString(),
                                          'education':
                                              controller.selectedDegree,
                                        });
                                        Get.back();
                                      },
                                    ));
                              },
                            ),
                            ListTile(
                              title: Text(
                                'Employment Type'.tr,
                                style: AppStyle.title,
                              ),
                              leading: Image.asset(
                                'assets/images/employmentType.png',
                                width: 35,
                              ),
                              trailing: const Icon(Icons.arrow_forward_ios),
                              subtitle: Text(controller
                                  .prfoileData['employmentType']
                                  .toString()),
                              onTap: () {
                                controller.updatField(
                                    'employmentType', TextInputType.name);
                              },
                            ),
                            ListTile(
                              title: Text(
                                'Marital Status'.tr,
                                style: AppStyle.title,
                              ),
                              leading: Image.asset(
                                'assets/images/maritalStatus.png',
                                width: 35,
                              ),
                              trailing: const Icon(Icons.arrow_forward_ios),
                              subtitle: Text(controller
                                  .prfoileData['maritalStatus']
                                  .toString()),
                              onTap: () {
                                controller.updatField(
                                    'maritalStatus', TextInputType.name);
                              },
                            ),
                            ListTile(
                              title: Text(
                                'SOS Phone'.tr,
                                style: AppStyle.title,
                              ),
                              leading: const Icon(
                                Icons.sos,
                                color: AppColor.redColor,
                                size: 35,
                              ),
                              trailing: const Icon(Icons.arrow_forward_ios),
                              subtitle: Text(controller.prfoileData['sosPhone']
                                  .toString()),
                              onTap: () async {
                                await controller.updatField(
                                    'sosPhone', TextInputType.phone);
                                box.write(BoxName.sosPhonePassenger,
                                    controller.prfoileData['sosPhone']);
                              },
                            ),
                            // const Spacer(),
                          ],
                        ),
                      ),
                    ),
                  )),
        Positioned(
          left: 70,
          bottom: 100,
          right: 70,
          child: MyElevatedButton(
              title: 'Sign Out'.tr,
              onPressed: () {
                LogOutController().logOutPassenger();
              }),
        ),
        Positioned(
          left: 70,
          bottom: 10,
          right: 70,
          child: GetBuilder<LogOutController>(builder: (logOutController) {
            return MyElevatedButton(
                title: 'Delete My Account'.tr,
                onPressed: () {
                  Get.defaultDialog(
                      title: 'Are you sure to delete your account?'.tr,
                      content: Form(
                        key: logOutController.formKey1,
                        child: MyTextForm(
                          controller: logOutController.emailTextController,
                          label: 'Type your Email'.tr,
                          hint: 'Type your Email'.tr,
                          type: TextInputType.emailAddress,
                        ),
                      ),
                      confirm: MyElevatedButton(
                          title: 'Delete My Account'.tr,
                          kolor: AppColor.redColor,
                          onPressed: () async {
                            await logOutController.deletePassengerAccount();
                          }),
                      cancel: MyElevatedButton(
                          title: 'No I want'.tr,
                          onPressed: () {
                            logOutController.emailTextController.clear();
                            logOutController.update();
                            Get.back();
                          }));
                });
          }),
        )
      ],
    );
  }
}

class GenderPicker extends StatelessWidget {
  final ProfileController controller = Get.put(ProfileController());

  final List<String> genderOptions = ['Male'.tr, 'Female'.tr, 'Other'.tr];

  GenderPicker({super.key});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 100,
      child: CupertinoPicker(
        itemExtent: 32.0,
        onSelectedItemChanged: (int index) {
          controller.setGender(genderOptions[index]);
        },
        children: genderOptions.map((String value) {
          return Text(value);
        }).toList(),
      ),
    );
  }
}

class EducationDegreePicker extends StatelessWidget {
  final ProfileController controller = Get.put(ProfileController());

  final List<String> degreeOptions = [
    'High School Diploma'.tr,
    'Associate Degree'.tr,
    'Bachelor\'s Degree'.tr,
    'Master\'s Degree'.tr,
    'Doctoral Degree'.tr,
  ];

  EducationDegreePicker({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 200,
      child: CupertinoPicker(
        // backgroundColor: AppColor.accentColor,
        // looping: true,
        squeeze: 2,
        // diameterRatio: 5,
        itemExtent: 32,
        onSelectedItemChanged: (int index) {
          controller.setDegree(degreeOptions[index]);
        },
        children: degreeOptions.map((String value) {
          return Text(value);
        }).toList(),
      ),
    );
  }
}

class CountryPicker extends StatelessWidget {
  final ProfileController controller = Get.put(ProfileController());

  final List<String> countryOptions = [
    'Jordan',
    'USA',
    'Egypt',
    'Turkey',
    'Saudi Arabia',
    'Qatar',
    'Bahrain',
    'Kuwait',
  ];

  CountryPicker({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ProfileController>(builder: (controller) {
      return ListView(
        children: [
          const SizedBox(
            height: 20,
          ),
          Text(
            "Select Your Country".tr,
            style: AppStyle.headTitle2,
            textAlign: TextAlign.center,
          ),
          // const SizedBox(
          //   height: 20,
          // ),
          Padding(
            padding: const EdgeInsets.all(10),
            child: Text(
              "To ensure you receive the most accurate information for your location, please select your country below. This will help tailor the app experience and content to your country."
                  .tr,
              style: AppStyle.title,
              textAlign: TextAlign.center,
            ),
          ),
          SizedBox(
            height: 200,
            child: CupertinoPicker(
              itemExtent: 32,
              onSelectedItemChanged: (int index) {
                controller.setCountry(countryOptions[index]);
                box.write(BoxName.countryCode,
                    countryOptions[index]); // Save in English
              },
              children: List.generate(
                countryOptions.length,
                (index) => Center(
                  child: Text(
                    countryOptions[index]
                        .tr, // Display translated if not English
                    style: AppStyle.title,
                  ),
                ),
              ),
            ),
          ),

          MyElevatedButton(
            title: 'Select Country'.tr, // Use translated text for button
            onPressed: () {
              Get.find<LoginController>().saveCountryCode(controller
                  .selectedCountry
                  .toString()); // No conversion needed
              box.write(
                  BoxName.countryCode, //
                  controller.selectedCountry); // Already saved in English
              Get.snackbar(controller.selectedCountry.toString().tr, '');
              Get.off(LoginPage());
            },
          )
        ],
      );
    });
  }
}

class CountryPickerFromSetting extends StatelessWidget {
  final ProfileController controller = Get.put(ProfileController());
  final LoginController loginController = Get.put(LoginController());

  final List<String> countryOptions = [
    'Jordan',
    'USA',
    'Egypt',
    'Turkey',
    'Saudi Arabia',
    'Qatar',
    'Bahrain',
    'Kuwait',
  ];

  CountryPickerFromSetting({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ProfileController>(builder: (controller) {
      return ListView(
        children: [
          const SizedBox(
            height: 20,
          ),
          Text(
            "Select Your Country".tr,
            style: AppStyle.headTitle2,
            textAlign: TextAlign.center,
          ),
          // const SizedBox(
          //   height: 20,
          // ),
          Padding(
            padding: const EdgeInsets.all(10),
            child: Text(
              "To ensure you receive the most accurate information for your location, please select your country below. This will help tailor the app experience and content to your country."
                  .tr,
              style: AppStyle.title,
              textAlign: TextAlign.center,
            ),
          ),
          SizedBox(
            height: 200,
            child: CupertinoPicker(
              itemExtent: 32,
              onSelectedItemChanged: (int index) {
                controller.setCountry(countryOptions[index]);
                box.write(BoxName.countryCode,
                    countryOptions[index]); // Save in English
              },
              children: List.generate(
                countryOptions.length,
                (index) => Center(
                  child: Text(
                    countryOptions[index]
                        .tr, // Display translated if not English
                    style: AppStyle.title,
                  ),
                ),
              ),
            ),
          ),

          MyElevatedButton(
            title: 'Select Country'.tr, // Use translated text for button
            onPressed: () async {
              loginController.saveCountryCode(controller.selectedCountry
                  .toString()); // No conversion needed
              box.write(
                  BoxName.countryCode, //
                  controller.selectedCountry); // Already saved in English
              Get.snackbar(controller.selectedCountry.toString().tr, '',
                  backgroundColor: AppColor.greenColor);
              // Get.back();//
              // Get.back();
            },
          )
        ],
      );
    });
  }
}
