import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:mypoint_flutter_app/networking/restful_api_client_all_request.dart';
import 'package:mypoint_flutter_app/screen/game/models/game_bundle_item_model.dart';

import '../../networking/restful_api_viewmodel.dart';
import '../../configs/constants.dart';
import 'models/game_bundle_response.dart';

class GameTabViewModel extends RestfulApiViewModel {
  final RxList<GameBundleItemModel> games = <GameBundleItemModel>[].obs;
  final RxString turnsNumberText = "".obs;
  void Function(String message)? onShowAlertError;
  void Function(GameBundleItemModel data)? gotoGameDetail;

  void getGames() {
    callApi<GameBundleResponse>(
      request: () => client.getGames(),
      onSuccess: (data, _) {
        games.assignAll(data.games ?? []);
        turnsNumberText.value = data.turnsNumberText ?? "";
      },
      onFailure: (msg, _, _) async {
        onShowAlertError?.call(msg);
      },
    );
  }

  void getGameDetail(String gameId) {
    callApi<GameBundleItemModel>(
      request: () => client.getGameDetail(gameId),
      onSuccess: (data, _) {
        gotoGameDetail?.call(data);
      },
      onFailure: (msg, _, _) async {
        onShowAlertError?.call(msg);
      },
    );
  }
}