import 'package:SEFER/constant/api_key.dart';
import 'package:SEFER/constant/links.dart';
import 'package:SEFER/constant/style.dart';
import 'package:SEFER/controller/home/captin/home_captain_controller.dart';
import 'package:SEFER/views/notification/available_rides_page.dart';
import 'package:SEFER/views/widgets/mycircular.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.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/main.dart';
import 'package:SEFER/views/auth/captin/logout_captain.dart';
import 'package:SEFER/views/home/Captin/history/history_captain.dart';
import 'package:SEFER/views/home/Captin/home_captain/help_captain.dart';
import 'package:SEFER/views/home/Captin/settings_captain.dart';
import 'package:SEFER/views/home/my_wallet/walet_captain.dart';
import 'package:SEFER/views/home/profile/profile_captain.dart';
import 'package:SEFER/views/notification/notification_captain.dart';

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

class DrawerCaptain extends StatelessWidget {
  ImageController imageController = Get.put(ImageController());

  @override
  Widget build(BuildContext context) {
    String writeText(int times) {
      return '*' * times;
    }

    return Drawer(
      child: ListView(
        children: [
          // Other drawer items
          UserAccountsDrawerHeader(
            decoration: const BoxDecoration(
              color: AppColor.greenColor,
              gradient: LinearGradient(
                colors: [
                  AppColor.primaryColor,
                  AppColor.secondaryColor,
                ],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
            ),
            currentAccountPictureSize: const Size.square(100),
            arrowColor: AppColor.deepPurpleAccent,
            accountName: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 10),
              child: Row(
                children: [
                  Text(
                    box.read(BoxName.nameDriver).toString(),
                    style: AppStyle.title,
                  ),
                  const Spacer(),
                  Column(
                    children: [
                      Text(
                        Get.find<HomeCaptainController>().rating.toString(),
                        style: AppStyle.number.copyWith(color: Colors.amber),
                      ),
                      Container(
                        // width: Get.width * .4,
                        padding: const EdgeInsets.symmetric(
                            horizontal: 4, vertical: 1),
                        color: AppColor.greenColor,
                        child: RatingBar.builder(
                            initialRating: 5,
                            minRating: 1,
                            direction: Axis.horizontal,
                            itemCount: 5,
                            itemSize: 20,
                            itemPadding:
                                const EdgeInsets.symmetric(horizontal: 2),
                            itemBuilder: (context, _) => const Icon(
                                  Icons.star,
                                  color: Colors.amber,
                                ),
                            onRatingUpdate: (rating) {}),
                      ),
                    ],
                  ),
                ],
              ),
            ),
            accountEmail: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 10),
              child: Text(
                box.read(BoxName.emailDriver),
                style: AppStyle.title,
              ),
            ),
            currentAccountPicture: Stack(
              children: [
                GetBuilder<ImageController>(builder: (imageController) {
                  return imageController.isloading
                      ? const MyCircularProgressIndicator()
                      : CircleAvatar(
                          radius: 45,
                          backgroundImage: NetworkImage(
                              '${AK.serverPHP}/portrate_captain_image/${box.read(BoxName.driverID)}.jpg'),
                        );
                }),
                Positioned(
                  right: 0,
                  top: 0,
                  child: IconButton(
                    onPressed: () {
                      imageController.choosImage(AppLink.uploadImage1);
                    },
                    icon: const Icon(Icons.edit),
                  ),
                ),
              ],
            ),
          ),

          ListTile(
            leading: const Icon(Icons.account_balance_wallet),
            title: Text(
              'Wallet'.tr,
              style: AppStyle.title,
            ),
            onTap: () {
              // Handle wallet item tap
              Get.to(() => WalletCaptain(), transition: Transition.native);
            },
          ),
          ListTile(
            leading: const Icon(Icons.person),
            title: Text(
              'Profile'.tr,
              style: AppStyle.title,
            ),
            onTap: () {
              // Handle profile item tap
              Get.to(() => const ProfileCaptain(),
                  transition: Transition.rightToLeftWithFade);
            },
          ),
          ListTile(
            leading: const Icon(
              Icons.history,
            ),
            title: Text(
              'History of Trip'.tr,
              style: AppStyle.title,
            ),
            onTap: () {
              Get.to(() => const HistoryCaptain(),
                  transition: Transition.downToUp);
              // Handle history of trip item tap
            },
          ),
          ListTile(
            leading: const Icon(Icons.phonelink_ring_rounded),
            title: Text(
              'Available for rides'.tr,
              style: AppStyle.title,
            ),
            onTap: () {
              Get.to(() => const AvailableRidesPage(),
                  transition: Transition.rightToLeftWithFade);
              // Handle history of trip item tap
            },
          ),
          ListTile(
            leading: const Icon(Icons.notifications),
            title: Text(
              'Notifications'.tr,
              style: AppStyle.title,
            ),
            onTap: () {
              // Handle notifications item tap
              Get.to(() => const NotificationCaptain(),
                  transition: Transition.upToDown);
            },
          ),
          ListTile(
            leading: const Icon(Icons.help),
            title: Text(
              'Helping Center'.tr,
              style: AppStyle.title,
            ),
            onTap: () {
              // Handle helping center item tap
              Get.to(() => HelpCaptain(), transition: Transition.size);
            },
          ),
          ListTile(
            leading: const Icon(Icons.settings),
            title: Text(
              'Settings'.tr,
              style: AppStyle.title,
            ),
            onTap: () {
              // Handle settings item tap
              Get.to(() => const SettingsCaptain(),
                  transition: Transition.cupertino);
            },
          ),
          ListTile(
            leading: const Icon(Icons.exit_to_app),
            title: Text(
              'Sign Out'.tr,
              style: AppStyle.title,
            ),
            onTap: () {
              // Handle sign out item tap
              Get.to(() => const LogoutCaptain(),
                  transition: Transition.cupertinoDialog);
            },
          ),
        ],
      ),
    );
  }
}
