import 'package:diameter/models/log_event_type.dart';
import 'package:diameter/navigation.dart';
import 'package:diameter/screens/log/log_event/log_event_type_detail.dart';
import 'package:flutter/material.dart';

class LogEventTypeListScreen extends StatefulWidget {
  static const String routeName = '/log-event-types';
  const LogEventTypeListScreen({Key? key}) : super(key: key);

  @override
  _LogEventTypeListScreenState createState() => _LogEventTypeListScreenState();
}

class _LogEventTypeListScreenState extends State<LogEventTypeListScreen> {
  List<LogEventType> _logEventTypes = [];

  final ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    reload();
  }

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

  void reload({String? message}) {
    setState(() {
      _logEventTypes = LogEventType.getAll();
    });
    setState(() {
      if (message != null) {
        var snackBar = SnackBar(
          content: Text(message),
          duration: const Duration(seconds: 2),
        );
        ScaffoldMessenger.of(context)
          ..removeCurrentSnackBar()
          ..showSnackBar(snackBar);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Log Event Types'), actions: <Widget>[
        IconButton(onPressed: reload, icon: const Icon(Icons.refresh))
      ]),
      drawer:
          const Navigation(currentLocation: LogEventTypeListScreen.routeName),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Expanded(
            child: _logEventTypes.isNotEmpty
                ? Scrollbar(
                    controller: _scrollController,
                    child: ListView.builder(
                      controller: _scrollController,
                      padding: const EdgeInsets.all(10.0),
                      itemCount: _logEventTypes.length,
                      itemBuilder: (context, index) {
                        final logEventType = _logEventTypes[index];
                        return Card(
                          child: ListTile(
                            onTap: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                  builder: (context) => EventTypeDetailScreen(
                                      id: logEventType.id),
                                ),
                              ).then((result) => reload(message: result?[0]));
                            },
                            title: Text(
                              logEventType.value.toUpperCase(),
                              style: Theme.of(context).textTheme.subtitle2,
                            ),
                            subtitle: Text(logEventType.notes ?? ''),
                            trailing: Row(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                IconButton(
                                  onPressed: () async {
                                    LogEventType.remove(logEventType.id);
                                    reload(message: 'Log Event Type deleted');
                                  },
                                  icon: const Icon(Icons.delete,
                                      color: Colors.blue),
                                )
                              ],
                            ),
                          ),
                        );
                      },
                    ),
                  )
                : const Center(
                    child:
                        Text('You have not created any Log Event Types yet!'),
                  ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => const EventTypeDetailScreen(),
            ),
          ).then((result) => reload(message: result?[0]));
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}
