import "package:client/view_models.dart";
import "package:client/widgets.dart";
import "package:flutter/material.dart";

class Sidebar extends ReusableReactiveWidget<HomeModel> {
  const Sidebar(super.model);

  @override
  Widget build(BuildContext context, HomeModel model) => DefaultTabController(
    length: 2,
    child: Card(
      clipBehavior: Clip.hardEdge,
      child: Column(
        children: [
          const SizedBox(height: 16),
          Text(
            "Select routes",
            maxLines: 1,
            style: context.textTheme.titleLarge,
            textAlign: TextAlign.center,
          ),
          Text(
            "Or click anywhere on the map",
            maxLines: 1,
            style: context.textTheme.bodyMedium,
            textAlign: TextAlign.center,
          ),
          const SizedBox(height: 8),
          if (model.shouldShowMarkers) TabBar(
            tabs: [
              for (final (provider, _) in model.providers)
                Tab(text: provider),
            ],
          ),
          if (!model.shouldShowMarkers && model.pathText != null)
            Center(child: Text(model.pathText!, style: context.textTheme.bodySmall)),
          if (!model.shouldShowMarkers && model.pathText == null)
            const Center(child: Text("Choose start or end location")),
          if (model.shouldShowMarkers) Expanded(
            child: TabBarView(
              children: [
                for (final (_, routesList) in model.providers)
                  ListView(
                    children: [
                      for (final route in routesList) CheckboxListTile(
                        title: Text(route, maxLines: 1),
                        subtitle: Text(
                          "${model.stopCounts[route] ?? 0} stops",
                          maxLines: 1,
                        ),
                        value: model.routesToShow.contains(route),
                        onChanged: (value) => model.showRoute(route, shouldShow: value!),
                      ),
                    ],
                  ),
              ],
            ),
          ),
        ],
      ),
    ),
  );
}
