import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:google_fonts/google_fonts.dart';
// import 'package:http/http.dart' as ref;
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:nhance_partner/presentation/providers/userRoleProvider.dart';

import '../../core/routing/routes.dart';
import '../layouts/responsive_layout.dart';
import '../providers/manager_provider.dart';
import '../providers/userRoleProvider.dart';
import '../screens/staff/Enquiry/Proposal_QuickCreation/Proposal_QuickCreation.dart';
import '../themes/indicators/side_Drawer_Panel.dart';

class TopBar extends ConsumerStatefulWidget implements PreferredSizeWidget {
  final String title;
  final bool isMobile;
  final VoidCallback? onLogout;
  final void Function(TapDownDetails details)? onProfile;
  final VoidCallback? onMenuPressed;
  final VoidCallback? onNotifications;

  const TopBar({
    super.key,
    required this.title,
    this.isMobile = false,
    this.onLogout,
    this.onProfile,
    this.onNotifications,
    this.onMenuPressed,
  });

  @override
  ConsumerState<TopBar> createState() => _TopBarState();
  // State<TopBar> createState() => _TopBarState();

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}

// class _TopBarState extends State<TopBar>
class _TopBarState extends ConsumerState<TopBar> {
  String? role;

  @override
  void initState() {
    super.initState();
    _loadUser();
  }

  Future<void> _loadUser() async {
    final prefs = await SharedPreferences.getInstance();
    setState(() {
      role = prefs.getString('userRole') ?? "Guest";
    });
    print('Tpbn - $role');
  }

  @override
  Widget build(BuildContext context) {
    return AppBar(
      backgroundColor: const Color(0xFFD6F6F4), // light cyan
      elevation: 0,
      automaticallyImplyLeading: false,
      titleSpacing: widget.isMobile ? 0 : 30,
      title: Row(
        children: [
          IconButton(
            icon: const Icon(Icons.menu, color: Colors.black87),
            onPressed: widget.onMenuPressed,
          ),
          if (!widget.isMobile)
            Image.asset(
              "assets/login/nhance-partner-logo.png",
              height: 60,
              width: 120,
            ),
          if (widget.isMobile) ...[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Image.asset(
                "assets/login/nhance-partner-logo.png",
                height: 40,
                width: 90,
              ),
            ),
            // IconButton(
            //   icon: const Icon(
            //     Icons.arrow_back_ios,
            //     color: Colors.black87,
            //     size: 18,
            //   ),
            //   onPressed: onBack ?? () => Navigator.pop(context),
            // ),
            // SizedBox(width: 5),
            // Expanded(
            //   child: Text(
            //     title,
            //     style: const TextStyle(
            //       color: Colors.black87,
            //       fontWeight: FontWeight.w500,
            //       fontSize: 16,
            //     ),
            //   ),
            // ),
          ],

          Spacer(),

          // if (onMenuPressed != null)
          if (role == 'agent') ...[
            GestureDetector(
              onTap: () async {
                final prefs = await SharedPreferences.getInstance();

                await prefs.remove('enqAgentDataId');
                ref.read(enquiryIdProvider.notifier).state = null;
                context.go(AppRoutes.tabEnquiry);
                // print('Export');
              },
              child: Container(
                padding: EdgeInsets.all(8.0),
                decoration: BoxDecoration(
                  color: Color(0xFF425B5B),
                  borderRadius: BorderRadius.circular(8.0),
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(
                      'Raise Enquiry',
                      style: GoogleFonts.inter(
                        color: Colors.white,
                        fontWeight: FontWeight.w600,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
          SizedBox(width: 10),
          // if (role != 'Accounts') ...[
          if (!['Accounts', 'agent'].contains(role)) ...[
            // Proposal button
            Container(
              width: 36,
              height: 36,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
                border: Border.all(color: Colors.grey.shade200, width: 1),
              ),
              child: IconButton(
                padding: EdgeInsets.zero,
                icon: const Icon(Icons.quora, color: Colors.black87, size: 20),
                onPressed: () {
                  SideDrawerPanel.show(
                    context: context,
                    title: 'Quote Creation',
                    child: CreateProposal_Quick(),
                  );
                },
                tooltip: 'Quick Quote',
              ),
            ),

            // const SizedBox(width: 12),
          ],
          // Container(
          //   width: 40,
          //   height: 40,
          //   decoration: BoxDecoration(
          //     color: Colors.white,
          //     borderRadius: BorderRadius.circular(50.0),
          //   ),
          //   child: Tooltip(
          //     message: 'Notifications',
          //     child: IconButton(
          //       icon: const Icon(
          //         Icons.notifications_none,
          //         color: Colors.black87,
          //       ),
          //       onPressed: widget.onNotifications,
          //     ),
          //   ),
          // ),
          if (!widget.isMobile) ...[
            SizedBox(width: 10),
            Container(
              width: 40,
              height: 40,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(50.0),
              ),
              child: Tooltip(
                message: 'Profile',
                child: GestureDetector(
                  onTapDown: (details) => widget.onProfile!(details),
                  child: const Icon(
                    Icons.person_outline,
                    color: Colors.black87,
                  ),
                ),
              ),
            ),

            // IconButton(
            //   icon: const Icon(Icons.person_outline, color: Colors.black87),
            //   onPressed: onProfile,
            // ),
          ],
          SizedBox(width: 10),
          Container(
            width: 40,
            height: 40,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(50.0),
            ),
            child: Tooltip(
              message: 'Logout',
              child: IconButton(
                icon: const Icon(Icons.logout, color: Colors.black87),
                onPressed: widget.onLogout,
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}
