import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ride/constant/box_name.dart';
import 'package:ride/constant/colors.dart';
import 'package:ride/constant/style.dart';
import 'package:ride/controller/profile/profile_controller.dart';
import 'package:ride/main.dart';
import 'package:ride/views/widgets/elevated_btn.dart';
import 'package:ride/views/widgets/my_scafold.dart';
import 'package:ride/views/widgets/mycircular.dart';

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

class PassengerProfilePage extends StatelessWidget {
  const PassengerProfilePage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(ProfileController());
    return MyScafolld(
      isleading: true,
      action: MyElevatedButton(
          title: 'Delete My Account'.tr,
          onPressed: () {
            LogOut().deleteMyAccount(box.read(BoxName.passengerID).toString());
          }),
      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');
                              },
                            ),
                            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');
                              },
                            ),
                            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');
                              },
                            ),
                            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');
                                box.write(BoxName.sosPhonePassenger,
                                    controller.prfoileData['sosPhone']);
                              },
                            ),
                          ],
                        ),
                      ),
                    ),
                  )),
        Positioned(
          left: 70,
          bottom: 5,
          right: 70,
          child: MyElevatedButton(
              title: 'Sign Out'.tr,
              onPressed: () {
                LogOut().logOut();
              }),
        )
      ],
    );
  }
}

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(),
      ),
    );
  }
}
