import 'package:easy_localization/easy_localization.dart';
import 'package:mc_common_app/api/api_client.dart';
import 'package:mc_common_app/classes/app_state.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/config/dependency_injection.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/models/general_models/generic_resp_model.dart';
import 'package:mc_common_app/models/setting_utils_models/app_info_model.dart';
import 'package:mc_common_app/models/setting_utils_models/contact_infos_model.dart';
import 'package:mc_common_app/models/setting_utils_models/faqs_model.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/utils/utils.dart';

abstract class SettingOptionsRepo {
  Future<List<FAQsModel>> getAllFaqs();

  Future<List<ContactInfoModel>> getAllContactInfos();

  Future<List<AppInfoModel>> getAppInfoList();

  Future<List<AppInfoModel>> getTermsAndConditions();

  Future<void> appInvitationCreate({required int channelId});
}

class SettingOptionsRepoImp extends SettingOptionsRepo {
  ApiClient apiClient = injector.get<ApiClient>();
  AppState appState = injector.get<AppState>();

  @override
  Future<List<FAQsModel>> getAllFaqs() async {
    int channel = 0;

    if (AppState().currentAppType == AppType.provider) {
      channel = 2;
    } else {
      channel = 3;
    }
    final params = {
      "Channel": channel.toString(),
    };

    String token = appState.getUser.data!.accessToken ?? "";

    GenericRespModel genericRespModel = await injector.get<ApiClient>().getJsonForObject(
          (json) => GenericRespModel.fromJson(json),
          ApiConsts.getAllFAQs,
          token: token,
          queryParameters: params,
        );

    if (genericRespModel.messageStatus != 1 || genericRespModel.data == null) {
      Utils.showToast(genericRespModel.message ?? LocaleKeys.somethingWrong.tr());
      return [];
    }

    List<FAQsModel> list = List.generate(genericRespModel.data.length, (index) => FAQsModel.fromJson(genericRespModel.data[index]));

    return list;
  }

  @override
  Future<List<ContactInfoModel>> getAllContactInfos() async {
    int channel = 0;

    if (AppState().currentAppType == AppType.provider) {
      channel = 2;
    } else {
      channel = 3;
    }
    final params = {"Channel": channel.toString()};

    String token = appState.getUser.data!.accessToken ?? "";

    GenericRespModel genericRespModel = await injector.get<ApiClient>().getJsonForObject(
          (json) => GenericRespModel.fromJson(json),
          ApiConsts.getContactInfo,
          queryParameters: params,
          token: token,
        );

    if (genericRespModel.messageStatus != 1 || genericRespModel.data == null) {
      Utils.showToast(genericRespModel.message ?? LocaleKeys.somethingWrong.tr());
      return [];
    }

    List<ContactInfoModel> list = List.generate(genericRespModel.data.length, (index) => ContactInfoModel.fromJson(genericRespModel.data[index]));

    return list;
  }

  @override
  Future<List<AppInfoModel>> getAppInfoList() async {
    String token = appState.getUser.data!.accessToken ?? "";

    int channel = 0;

    if (AppState().currentAppType == AppType.provider) {
      channel = 2;
    } else {
      channel = 3;
    }
    final params = {
      "Channel": channel.toString(),
    };

    GenericRespModel genericRespModel = await injector.get<ApiClient>().getJsonForObject(
          (json) => GenericRespModel.fromJson(json),
          ApiConsts.getAppInfo,
          token: token,
          queryParameters: params,
        );

    if (genericRespModel.messageStatus != 1 || genericRespModel.data == null) {
      Utils.showToast(genericRespModel.message ?? LocaleKeys.somethingWrong.tr());
      return [];
    }

    List<AppInfoModel> list = List.generate(genericRespModel.data.length, (index) => AppInfoModel.fromJson(genericRespModel.data[index]));

    return list;
  }

  @override
  Future<List<AppInfoModel>> getTermsAndConditions() async {
    String token = appState.getUser.data!.accessToken ?? "";

    int channel = 0;

    if (AppState().currentAppType == AppType.provider) {
      channel = 2;
    } else {
      channel = 3;
    }
    final params = {
      "Channel": channel.toString(),
    };

    GenericRespModel genericRespModel = await injector.get<ApiClient>().getJsonForObject(
          (json) => GenericRespModel.fromJson(json),
          ApiConsts.getTermsAndConditions,
          token: token,
          queryParameters: params,
        );

    if (genericRespModel.messageStatus != 1 || genericRespModel.data == null) {
      Utils.showToast(genericRespModel.message ?? LocaleKeys.somethingWrong.tr());
      return [];
    }

    List<AppInfoModel> list = List.generate(genericRespModel.data.length, (index) => AppInfoModel.fromJson(genericRespModel.data[index], isForTermsAndCondition: true));

    return list;
  }

  @override
  Future<void> appInvitationCreate({required int channelId}) async {
    String token = appState.getUser.data!.accessToken ?? "";

    final params = {
      "channelID": channelId.toString(),
      "comments": "string",
    };

    if (AppState().currentAppType == AppType.provider) {
      params.addAll({
        "customerID": (appState.getUser.data!.userInfo!.customerId ?? "0").toString(),
      });
    } else {
      params.addAll({
        "providerID": (appState.getUser.data!.userInfo!.providerId ?? "0").toString(),
      });
    }

    GenericRespModel genericRespModel = await injector.get<ApiClient>().postJsonForObject(
          (json) => GenericRespModel.fromJson(json),
          ApiConsts.createAppInvitation,
          token: token,
          params,
        );
  }
}
