import 'package:flutter/material.dart';
import 'package:supplier_new/resources/resources_fleet.dart';
import 'package:supplier_new/resources/resources_drivers.dart';
import 'package:supplier_new/resources/resources_sources.dart';
import '../common/settings.dart';

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

  @override
  State<ResourcesMainScreen> createState() => _ResourcesMainScreenState();
}

class _ResourcesMainScreenState extends State<ResourcesMainScreen>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  final List<String> _labels = const ['Fleet', 'Drivers', 'Sources'];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(
      length: _labels.length,
      vsync: this,
      initialIndex: AppSettings.resourcesInitialTab,
    );
    AppSettings.resourcesInitialTab = 0;
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // keep entire screen white
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
              child: AnimatedBuilder(
                animation: _tabController,
                builder: (context, _) {
                  return Row(
                    children: List.generate(_labels.length, (index) {
                      final isSelected = _tabController.index == index;
                      return Expanded(
                        child: GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () =>
                              setState(() => _tabController.index = index),
                          child: AnimatedContainer(
                            duration: const Duration(milliseconds: 180),
                            curve: Curves.easeInOut,
                            margin: const EdgeInsets.symmetric(horizontal: 6),
                            padding: const EdgeInsets.symmetric(vertical: 10),
                            decoration: BoxDecoration(
                              color: isSelected
                                  ? const Color(0xFFF1F1F1)
                                  : Colors.white,
                              borderRadius:
                                  BorderRadius.circular(16), // radius 16 always
                            ),
                            alignment: Alignment.center,
                            child: Text(
                              _labels[index],
                              style: fontTextStyle(
                                12,
                                isSelected ? const Color(0xFF101214) : const Color(0xFF646464),
                                isSelected ? FontWeight.w700 : FontWeight.w400,
                              ),
                            ),
                          ),
                        ),
                      );
                    }),
                  );
                },
              ),
            ),
            const SizedBox(height: 8),
            Expanded(
              child: HeroMode(
                enabled: false,
                child: TabBarView(
                  controller: _tabController,
                  children: const [
                    ResourcesFleetScreen(),
                    ResourcesDriverScreen(),
                    ResourcesSourceScreen(),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
      /*floatingActionButton: AnimatedBuilder(
        animation: _tabController.animation!,
        builder: (context, _) =>
            _buildFAB(_tabController.index) ?? const SizedBox.shrink(),
      ),*/
    );
  }
}
