import 'package:flutter/material.dart';
import 'package:lunasea/core.dart';
import 'package:lunasea/extensions/navigator_state.dart';
import 'package:lunasea/modules/radarr.dart';

class RadarrEditMovieActionBar extends StatelessWidget {
  const RadarrEditMovieActionBar({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return LunaBottomActionBar(
      actions: [
        LunaButton(
          type: LunaButtonType.TEXT,
          text: 'lunasea.Update'.tr(),
          icon: Icons.edit_rounded,
          loadingState: context.watch<RadarrMoviesEditState>().state,
          onTap: () async => _updateOnTap(context),
        )
      ],
    );
  }

  Future<void> _updateOnTap(BuildContext context) async {
    if (context.read<RadarrMoviesEditState>().canExecuteAction) {
      context.read<RadarrMoviesEditState>().state = LunaLoadingState.ACTIVE;
      if (context.read<RadarrMoviesEditState>().movie != null) {
        RadarrMovie movie = context
            .read<RadarrMoviesEditState>()
            .movie!
            .updateEdits(context.read<RadarrMoviesEditState>());
        bool result = await RadarrAPIHelper().updateMovie(
          context: context,
          movie: movie,
        );
        if (result) Navigator.of(context).safetyPop();
      }
    }
  }
}
