import 'dart:convert';
import 'dart:io';

import 'package:mohem_flutter_app/models/account.dart';
import 'package:mohem_flutter_app/models/response_models.dart';
import 'package:mohem_flutter_app/services/backend_service.dart';
import 'package:injector/injector.dart';

abstract class IAcRepository {
  Future<Account> getAccountList();

  Future<BackendResponse> updateAccount(String dataAsJson);
}

class AcRepository implements IAcRepository {
  static const String ACCOUNT_API_CONTROLLER_MOBILE =
      "AccountApiControllerMobile/";

  static const String ACCOUNT_LIST = ACCOUNT_API_CONTROLLER_MOBILE + "list";
  static const String UPDATE_LIST =
      ACCOUNT_API_CONTROLLER_MOBILE + "saveaccountselected";

  @override
  Future<Account> getAccountList() async {
    BackendResponse response = await Injector.appInstance
        .getDependency<IBackendApiService>()
        .getAuthenticatedAPI(ACCOUNT_LIST);

    if (response != null && response.isOk) {
      return Account.fromJson(response.result);
    } else {
      throw Exception();
    }
  }

  @override
  Future<BackendResponse> updateAccount(String dataAsJson) async {
    BackendResponse response = await Injector.appInstance
        .getDependency<IBackendApiService>()
        .postAuthenticatedAPI(UPDATE_LIST, dataAsJson);

    if (response != null && response.isOk) {
      //if parsing failed, throw exception
      return response;
    } else {
      throw Exception();
    }
  }
}
