import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../model/flood_station.dart';
import '../services/overlay_service.dart';
import '../widgets/flood_station_list_view.dart';
import '../services/flood_station_provider.dart';
import '../widgets/station_filter.dart';
import 'flood_station_page.dart';

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

  @override
  State<LandingPage> createState() => _LandingPageState();
}

// uses mixin OverlayService to show loading overlay
class _LandingPageState extends State<LandingPage> with OverlayService {
  late FloodStationProvider _floodStationProvider;

  @override
  void dispose() {
    super.dispose();
    removeLoadingNotifier(); // Clean up overlay if needed
  }

  @override
  Widget build(BuildContext context) {
    _floodStationProvider = context.watch<FloodStationProvider>();
    return Column(
      children: [
        StationFilter(
          onChanged: (filterText) => _handleFilterChange(filterText),
        ),
        _buildStationList(),
      ],
    );
  }

  // if the list of all Stations is empty the method returns a button to load them
  // else returns a list of FloodStations
  Widget _buildStationList() {
    if (!_shouldShowList) {
      return Expanded(
        child: Center(
          child: ElevatedButton(
            onPressed: _handleLoadAllStations,
            child: const Text('Load all Stations'),
          ),
        ),
      );
    }

    return Expanded(
      child: FloodStationListView(
        stations: _floodStationProvider.filtered
            ? _floodStationProvider.filteredStations
            : _floodStationProvider.allStations,
        onItemTapped: _navigateToStationDetail,
      ),
    );
  }

  void _handleFilterChange(String filterText) {
    if (filterText.isEmpty) {
      _floodStationProvider.filtered = false;
      setState(() {});
      return;
    }

    showLoadingNotifier(
      context: context,
      message: 'Loading',
      onDismiss: () {
        _floodStationProvider.cancelFilterLoading();
      },
    );

    _floodStationProvider.loadFilteredStations(filterText);
    _floodStationProvider.filteredStationsFuture
        ?.then((_) => removeLoadingNotifier());
    _floodStationProvider.filtered = true;
  }

  void _handleLoadAllStations() {
    showLoadingNotifier(
      context: context,
      message: 'Loading',
      onDismiss: () {
        _floodStationProvider.cancelFilterLoading();
      },
    );

    _floodStationProvider
        .loadAllStations()
        .whenComplete(() => removeLoadingNotifier());
  }

  void _navigateToStationDetail(FloodStation station) {
    _floodStationProvider.selectedStation = station;
    Navigator.of(context).pushNamed(FloodStationPage.routeName);
  }

  // returns boolean to decide whether the list of stations should be shown
  // if the list of stations is empty and is not filtered either, the function returns false
  bool get _shouldShowList {
    if (!_floodStationProvider.filtered &&
        _floodStationProvider.allStations.isNotEmpty) {
      return true;
    }
    return _floodStationProvider.filtered;
  }
}
