import 'package:flutter/cupertino.dart';
import 'package:mc_common_app/main.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/repositories/setting_options_repo.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/view_models/base_view_model.dart';
import 'package:share_plus/share_plus.dart';

class SettingOptionsVM extends BaseVM {
  final SettingOptionsRepo settingOptionsRepo;

  SettingOptionsVM({required this.settingOptionsRepo});

  List<FAQsModel> faqsList = [];

  Future<void> getAllFaqs() async {
    setState(ViewState.busy);
    try {
      faqsList = await settingOptionsRepo.getAllFaqs();
      setState(ViewState.idle);
      notifyListeners();
    } catch (e) {
      logger.i(e.toString());
      Utils.showToast(e.toString());
      setState(ViewState.idle);
    }
  }

  List<ContactInfoModel> contactInfosList = [];

  Future<void> getAllContactInfosList() async {
    setState(ViewState.busy);
    try {
      contactInfosList = await settingOptionsRepo.getAllContactInfos();
      setState(ViewState.idle);
      notifyListeners();
    } catch (e) {
      logger.i(e.toString());
      Utils.showToast(e.toString());
      setState(ViewState.idle);
    }
  }

  List<AppInfoModel> appInfoList = [];

  Future<void> getAppInfoList() async {
    setState(ViewState.busy);
    try {
      appInfoList = await settingOptionsRepo.getAppInfoList();
      setState(ViewState.idle);
      notifyListeners();
    } catch (e) {
      logger.i(e.toString());
      Utils.showToast(e.toString());
      setState(ViewState.idle);
    }
  }

  List<AppInfoModel> termsAndConditionsList = [];

  Future<void> getTermsAndConditions() async {
    setState(ViewState.busy);
    try {
      termsAndConditionsList = await settingOptionsRepo.getTermsAndConditions();
      setState(ViewState.idle);
      notifyListeners();
    } catch (e) {
      logger.i(e.toString());
      Utils.showToast(e.toString());
      setState(ViewState.idle);
    }
  }

  Future<void> appInvitationCreate({required BuildContext context, required int channelId}) async {
    ShareResult result = await Utils.shareAppInvitation();

    if (result.status != ShareResultStatus.success) {
      return;
    }
    Utils.showLoading(context);
    try {
      await settingOptionsRepo.appInvitationCreate(channelId: channelId);
      Utils.hideLoading(context);
    } catch (e) {
      logger.i(e.toString());
      Utils.showToast(e.toString());
      Utils.hideLoading(context);
    }
  }
}
