import 'package:flutter/material.dart';

import 'past_events_sub_tab.dart';
import 'upcoming_events_sub_tab.dart';

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

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

  @override
  State<StaffEventsMainTab> createState() => _StaffEventsMainTabState();
}

class _StaffEventsMainTabState extends State<StaffEventsMainTab> {
  int _index = 0;
  bool _sidebarOpen = false;
  bool _showHeader = true;

  final List<String> _labels = const <String>[
    'Upcoming Events',
    'Past Events',
  ];

  final List<IconData> _icons = const <IconData>[
    Icons.event_available_rounded,
    Icons.history_rounded,
  ];

  Widget _current() {
    if (_index == 1) {
      return const RepaintBoundary(
        key: ValueKey('past-events'),
        child: PastEventsSubTab(),
      );
    }
    return const RepaintBoundary(
      key: ValueKey('upcoming-events'),
      child: UpcomingEventsSubTab(),
    );
  }

  void _select(int value) {
    setState(() {
      _index = value;
      _sidebarOpen = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF3F5F7),
      body: LayoutBuilder(
        builder: (context, constraints) {
          final desktop = constraints.maxWidth >= 900;
          return Stack(
            children: [
              Column(
                children: [
                  AnimatedSize(
                    duration: const Duration(milliseconds: 260),
                    curve: Curves.easeOutCubic,
                    child: _showHeader
                        ? Container(
                            width: double.infinity,
                            padding: EdgeInsets.symmetric(
                              horizontal: desktop ? 24 : 16,
                              vertical: desktop ? 18 : 14,
                            ),
                            decoration: const BoxDecoration(
                              color: Colors.white,
                              border: Border(
                                bottom: BorderSide(color: Color(0xFFE1E6EB)),
                              ),
                            ),
                            child: Row(
                              children: [
                                InkWell(
                                  onTap: () => setState(() => _sidebarOpen = !_sidebarOpen),
                                  borderRadius: BorderRadius.circular(12),
                                  child: Container(
                                    width: 43,
                                    height: 43,
                                    alignment: Alignment.center,
                                    decoration: BoxDecoration(
                                      color: const Color(0xFFF6F8FA),
                                      borderRadius: BorderRadius.circular(12),
                                      border: Border.all(color: const Color(0xFFE1E6EB)),
                                    ),
                                    child: const Icon(Icons.menu_rounded, color: _green),
                                  ),
                                ),
                                const SizedBox(width: 14),
                                Expanded(
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      RichText(
                                        text: TextSpan(
                                          children: [
                                            TextSpan(
                                              text: 'ROCKVIEW ',
                                              style: TextStyle(
                                                color: _green,
                                                fontSize: desktop ? 22 : 17,
                                                fontWeight: FontWeight.w900,
                                                letterSpacing: 1,
                                              ),
                                            ),
                                            TextSpan(
                                              text: 'CAMPUS EVENTS',
                                              style: TextStyle(
                                                color: _gold,
                                                fontSize: desktop ? 22 : 17,
                                                fontWeight: FontWeight.w500,
                                                letterSpacing: 1,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                      const SizedBox(height: 4),
                                      const Text(
                                        'Publish, discover and discuss university events across campuses.',
                                        style: TextStyle(
                                          color: Color(0xFF6A7580),
                                          fontSize: 11.5,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                if (desktop)
                                  IconButton(
                                    tooltip: 'Collapse header',
                                    onPressed: () => setState(() => _showHeader = false),
                                    icon: const Icon(Icons.keyboard_arrow_up_rounded, color: _gold),
                                  ),
                              ],
                            ),
                          )
                        : const SizedBox.shrink(),
                  ),
                  Expanded(
                    child: AnimatedSwitcher(
                      duration: const Duration(milliseconds: 260),
                      switchInCurve: Curves.easeOutCubic,
                      switchOutCurve: Curves.easeInCubic,
                      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(
                  left: 16,
                  top: 14,
                  child: Material(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12),
                    elevation: 2,
                    child: IconButton(
                      tooltip: 'Events menu',
                      onPressed: () => setState(() => _sidebarOpen = !_sidebarOpen),
                      icon: const Icon(Icons.menu_rounded, color: _green),
                    ),
                  ),
                ),
              if (!_showHeader)
                Positioned(
                  right: 16,
                  top: 14,
                  child: Material(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(999),
                    elevation: 2,
                    child: IconButton(
                      tooltip: 'Show header',
                      onPressed: () => setState(() => _showHeader = true),
                      icon: const Icon(Icons.keyboard_arrow_down_rounded, color: _gold),
                    ),
                  ),
                ),
              if (_sidebarOpen)
                Positioned.fill(
                  child: GestureDetector(
                    onTap: () => setState(() => _sidebarOpen = false),
                    child: Container(color: Colors.black.withOpacity(.32)),
                  ),
                ),
              AnimatedPositioned(
                duration: const Duration(milliseconds: 300),
                curve: Curves.easeOutCubic,
                left: _sidebarOpen ? 0 : -310,
                top: 0,
                bottom: 0,
                child: Container(
                  width: 300,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    boxShadow: [
                      if (_sidebarOpen)
                        BoxShadow(
                          color: Colors.black.withOpacity(.15),
                          blurRadius: 24,
                          offset: const Offset(5, 0),
                        ),
                    ],
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Container(
                        padding: const EdgeInsets.fromLTRB(20, 22, 12, 22),
                        decoration: const BoxDecoration(
                          color: _green,
                          border: Border(bottom: BorderSide(color: _gold, width: 4)),
                        ),
                        child: Row(
                          children: [
                            const Expanded(
                              child: Text(
                                'EVENTS MANAGER',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.w900,
                                  letterSpacing: 1.3,
                                ),
                              ),
                            ),
                            IconButton(
                              onPressed: () => setState(() => _sidebarOpen = false),
                              icon: const Icon(Icons.close_rounded, color: Colors.white),
                            ),
                          ],
                        ),
                      ),
                      Expanded(
                        child: ListView.builder(
                          padding: const EdgeInsets.symmetric(vertical: 14),
                          itemCount: _labels.length,
                          itemBuilder: (context, index) {
                            final selected = _index == index;
                            return InkWell(
                              onTap: () => _select(index),
                              child: Container(
                                padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 17),
                                decoration: BoxDecoration(
                                  color: selected ? _green.withOpacity(.07) : Colors.transparent,
                                  border: Border(
                                    left: BorderSide(
                                      color: selected ? _gold : Colors.transparent,
                                      width: 4,
                                    ),
                                  ),
                                ),
                                child: Row(
                                  children: [
                                    Icon(
                                      _icons[index],
                                      color: selected ? _green : Colors.grey.shade500,
                                    ),
                                    const SizedBox(width: 13),
                                    Text(
                                      _labels[index],
                                      style: TextStyle(
                                        color: selected ? _green : Colors.grey.shade700,
                                        fontWeight: selected ? FontWeight.w900 : FontWeight.w600,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            );
                          },
                        ),
                      ),
                      Container(
                        padding: const EdgeInsets.all(20),
                        decoration: const BoxDecoration(
                          border: Border(top: BorderSide(color: Color(0xFFE1E6EB))),
                        ),
                        child: const Text(
                          'Rockview University\nCampus Events & Community',
                          style: TextStyle(color: Color(0xFF8A949E), fontSize: 10.5, height: 1.5),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}
