import 'package:flutter/material.dart';

import 'active_announcements_sub_tab.dart';
import 'draft_announcements_sub_tab.dart';

const Color _gold = Color.fromARGB(255, 204, 117, 4);
const Color _green = Color.fromARGB(255, 2, 105, 48);
const Color _border = Color(0xFFE3E8EE);

class StaffAnnouncementsMainTab extends StatefulWidget {
  const StaffAnnouncementsMainTab({super.key});

  @override
  State<StaffAnnouncementsMainTab> createState() => _StaffAnnouncementsMainTabState();
}

class _StaffAnnouncementsMainTabState extends State<StaffAnnouncementsMainTab> {
  int _selected = 0;
  bool _drawerOpen = false;
  bool _showHeader = true;

  final List<_AnnouncementMenuItem> _items = const [
    _AnnouncementMenuItem('Active Announcements', Icons.campaign_rounded),
    _AnnouncementMenuItem('Drafts & Scheduled', Icons.edit_calendar_rounded),
  ];

  Widget _current() {
    switch (_selected) {
      case 1:
        return const DraftAnnouncementsSubTab(key: ValueKey('announcement-drafts'));
      default:
        return const ActiveAnnouncementsSubTab(key: ValueKey('announcement-active'));
    }
  }

  void _select(int index) {
    setState(() {
      _selected = index;
      _drawerOpen = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF3F5F7),
      body: LayoutBuilder(builder: (context, constraints) {
        final desktop = constraints.maxWidth >= 920;
        return Stack(children: [
          Column(children: [
            AnimatedSize(
              duration: const Duration(milliseconds: 260),
              curve: Curves.easeOutCubic,
              child: _showHeader
                  ? _HeaderBar(
                      desktop: desktop,
                      onMenu: () => setState(() => _drawerOpen = !_drawerOpen),
                      onCollapse: () => setState(() => _showHeader = false),
                    )
                  : const SizedBox(width: double.infinity, height: 0),
            ),
            Expanded(
              child: Row(children: [
                if (desktop)
                  SizedBox(
                    width: 235,
                    child: _PermanentNavigation(
                      items: _items,
                      selected: _selected,
                      onSelect: _select,
                    ),
                  ),
                if (desktop) const VerticalDivider(width: 1, color: _border),
                Expanded(
                  child: Padding(
                    padding: EdgeInsets.fromLTRB(desktop ? 18 : 10, 12, desktop ? 18 : 10, 0),
                    child: AnimatedSwitcher(
                      duration: const Duration(milliseconds: 220),
                      switchInCurve: Curves.easeOut,
                      switchOutCurve: Curves.easeIn,
                      transitionBuilder: (child, animation) => FadeTransition(
                        opacity: animation,
                        child: SlideTransition(
                          position: Tween<Offset>(begin: const Offset(.025, 0), end: Offset.zero).animate(animation),
                          child: child,
                        ),
                      ),
                      child: _current(),
                    ),
                  ),
                ),
              ]),
            ),
          ]),
          if (!_showHeader)
            Positioned(
              top: 12,
              right: 16,
              child: _FloatingHeaderButton(onTap: () => setState(() => _showHeader = true)),
            ),
          if (!desktop && !_showHeader)
            Positioned(
              top: 12,
              left: 16,
              child: _FloatingMenuButton(onTap: () => setState(() => _drawerOpen = true)),
            ),
          if (!desktop && _drawerOpen)
            Positioned.fill(
              child: GestureDetector(
                onTap: () => setState(() => _drawerOpen = false),
                child: Container(color: Colors.black.withOpacity(.28)),
              ),
            ),
          if (!desktop)
            AnimatedPositioned(
              duration: const Duration(milliseconds: 260),
              curve: Curves.easeOutCubic,
              top: 0,
              bottom: 0,
              left: _drawerOpen ? 0 : -300,
              child: SizedBox(
                width: 290,
                child: _MobileNavigation(
                  items: _items,
                  selected: _selected,
                  onSelect: _select,
                  onClose: () => setState(() => _drawerOpen = false),
                ),
              ),
            ),
        ]);
      }),
    );
  }
}

class _HeaderBar extends StatelessWidget {
  final bool desktop;
  final VoidCallback onMenu;
  final VoidCallback onCollapse;
  const _HeaderBar({required this.desktop, required this.onMenu, required this.onCollapse});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: desktop ? 24 : 14, vertical: 15),
      decoration: const BoxDecoration(color: Colors.white, border: Border(bottom: BorderSide(color: _border))),
      child: Row(children: [
        if (!desktop) ...[
          IconButton(onPressed: onMenu, icon: const Icon(Icons.menu_rounded, color: _green)),
          const SizedBox(width: 5),
        ],
        Container(
          width: 44,
          height: 44,
          decoration: BoxDecoration(color: _green.withOpacity(.07), borderRadius: BorderRadius.circular(14)),
          child: const Icon(Icons.campaign_rounded, color: _green),
        ),
        const SizedBox(width: 12),
        Expanded(
          child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            RichText(
              text: TextSpan(children: [
                TextSpan(text: 'ROCKVIEW ', style: TextStyle(color: _green, fontSize: desktop ? 21 : 17, fontWeight: FontWeight.w900, letterSpacing: 1)),
                TextSpan(text: 'ANNOUNCEMENTS', style: TextStyle(color: _gold, fontSize: desktop ? 21 : 17, fontWeight: FontWeight.w500, letterSpacing: 1)),
              ]),
            ),
            const SizedBox(height: 4),
            const Text('Professional institutional notices • Live discussions • Scheduling • Notifications', maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 10.8, color: Color(0xFF66727E), fontWeight: FontWeight.w600)),
          ]),
        ),
        IconButton(onPressed: onCollapse, tooltip: 'Collapse header', icon: const Icon(Icons.keyboard_arrow_up_rounded, color: _gold)),
      ]),
    );
  }
}

class _PermanentNavigation extends StatelessWidget {
  final List<_AnnouncementMenuItem> items;
  final int selected;
  final ValueChanged<int> onSelect;
  const _PermanentNavigation({required this.items, required this.selected, required this.onSelect});

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
        Container(
          padding: const EdgeInsets.fromLTRB(18, 22, 18, 16),
          child: const Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            Text('ANNOUNCEMENTS', style: TextStyle(fontSize: 10, color: _green, fontWeight: FontWeight.w900, letterSpacing: 1)),
            SizedBox(height: 5),
            Text('Communication workspace', style: TextStyle(fontSize: 11, color: Color(0xFF66727E))),
          ]),
        ),
        ...List.generate(items.length, (index) => _NavTile(item: items[index], selected: selected == index, onTap: () => onSelect(index))),
        const Spacer(),
        Container(
          margin: const EdgeInsets.all(14),
          padding: const EdgeInsets.all(13),
          decoration: BoxDecoration(color: _green.withOpacity(.04), borderRadius: BorderRadius.circular(16), border: Border.all(color: _green.withOpacity(.12))),
          child: const Text('Notices are linked to the signed-in account and campus automatically.', style: TextStyle(fontSize: 10.5, color: Color(0xFF66727E), height: 1.45)),
        ),
      ]),
    );
  }
}

class _MobileNavigation extends StatelessWidget {
  final List<_AnnouncementMenuItem> items;
  final int selected;
  final ValueChanged<int> onSelect;
  final VoidCallback onClose;
  const _MobileNavigation({required this.items, required this.selected, required this.onSelect, required this.onClose});

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.white,
      elevation: 12,
      child: Column(children: [
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 20),
          decoration: const BoxDecoration(color: _green, border: Border(bottom: BorderSide(color: _gold, width: 4))),
          child: Row(children: [const Expanded(child: Text('ANNOUNCEMENTS', style: TextStyle(color: Colors.white, fontWeight: FontWeight.w900, letterSpacing: 1))), IconButton(onPressed: onClose, icon: const Icon(Icons.close_rounded, color: Colors.white))]),
        ),
        const SizedBox(height: 10),
        ...List.generate(items.length, (index) => _NavTile(item: items[index], selected: selected == index, onTap: () => onSelect(index))),
      ]),
    );
  }
}

class _NavTile extends StatelessWidget {
  final _AnnouncementMenuItem item;
  final bool selected;
  final VoidCallback onTap;
  const _NavTile({required this.item, required this.selected, required this.onTap});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 3),
        padding: const EdgeInsets.symmetric(horizontal: 13, vertical: 14),
        decoration: BoxDecoration(
          color: selected ? _green.withOpacity(.07) : Colors.transparent,
          borderRadius: BorderRadius.circular(14),
          border: Border(left: BorderSide(color: selected ? _gold : Colors.transparent, width: 4)),
        ),
        child: Row(children: [
          Icon(item.icon, size: 20, color: selected ? _green : Colors.grey.shade500),
          const SizedBox(width: 11),
          Expanded(child: Text(item.title, style: TextStyle(fontSize: 12.5, fontWeight: selected ? FontWeight.w900 : FontWeight.w700, color: selected ? _green : Colors.grey.shade700))),
        ]),
      ),
    );
  }
}

class _FloatingHeaderButton extends StatelessWidget {
  final VoidCallback onTap;
  const _FloatingHeaderButton({required this.onTap});
  @override
  Widget build(BuildContext context) => Material(color: Colors.white, elevation: 2, borderRadius: BorderRadius.circular(13), child: InkWell(onTap: onTap, borderRadius: BorderRadius.circular(13), child: const Padding(padding: EdgeInsets.all(10), child: Icon(Icons.keyboard_arrow_down_rounded, color: _green))));
}

class _FloatingMenuButton extends StatelessWidget {
  final VoidCallback onTap;
  const _FloatingMenuButton({required this.onTap});
  @override
  Widget build(BuildContext context) => Material(color: Colors.white, elevation: 2, borderRadius: BorderRadius.circular(13), child: InkWell(onTap: onTap, borderRadius: BorderRadius.circular(13), child: const Padding(padding: EdgeInsets.all(10), child: Icon(Icons.menu_rounded, color: _green))));
}

class _AnnouncementMenuItem {
  final String title;
  final IconData icon;
  const _AnnouncementMenuItem(this.title, this.icon);
}
