import 'package:auto_route/auto_route.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
import 'package:telnow_mobile_new/main.dart';
import 'package:telnow_mobile_new/src/api/api_auth_provider.dart';
import 'package:telnow_mobile_new/src/api/jwt_token.dart';
import 'package:telnow_mobile_new/src/layouts/components/template.dart';
import 'package:telnow_mobile_new/src/storage/sharedpreferences/shared_preferences_manager.dart';
import 'package:telnow_mobile_new/src/utils/U.dart';
import 'package:telnow_mobile_new/src/utils/provider.dart';

class AccountFunction{
  final ApiAuthProvider apiAuthProvider = ApiAuthProvider();
  final NotificationClass notification = NotificationClass();
  final JwtToken token = JwtToken();
  SharedPreferencesManager _sharedPreferencesManager = SharedPreferencesManager();
  getUser(BuildContext context) async {
    var res = await token.getUserData(context);
    if (res != null) {
      var ver = await apiAuthProvider.getDataNoAuth('/api/license');
      Provider.of<UserModule>(context, listen: false).setSerialNum(ver['serialNumber']);
      Provider.of<UserModule>(context, listen: false).setList(res);
    } else {
      Provider.of<UserModule>(context, listen: false).setResetData(true);
    }
  }

  switchLang(BuildContext context, code, user) async {
    showLoading(context);
    try{
      var res = await apiAuthProvider.patchData('/api/informants/' + user['id'].toString(), {'userId': user['userId'], 'language': code.toUpperCase()}, context);
      print(res);
      if (res != null) {
        closeLoading(context);
        context.setLocale(Locale(code));
        notification.startNotification(context);
        Navigator.of(context).pop();
      } else {
        closeLoading(context);
      }
    }catch(e){
      closeLoading(context);
    }
  }

  Future<bool> setDndStatus(BuildContext context, bool dnd)async{
    var res = await apiAuthProvider.patchData('/api/informants/${Provider.of<UserModule>(context, listen: false).user()['id']}/setDnd', null, context, params: {'dnd': '${!dnd}'});
    if (res != null) {
      Provider.of<UserModule>(context, listen: false).setDndStatus(dnd);
      return true;
    }
    return false;
  }
  
  logoutAction(BuildContext context)async{
    showLoading(context, text: '');
    String pid = U.getPidFromUrl(context.router.currentUrl);

    try {
      var tkn = await _sharedPreferencesManager.getString(SharedPreferencesManager.keyFcmToken);
      // var tkn = await U.getFcmToken();
      if (tkn == null) {
        Navigator.of(context).pop();
        closeLoading(context);
        token.logout();
        // context.router.removeLast();
        context.navigateToPath("/app/$pid/login");
      } else {
        var param = {'token': tkn, 'whiteListToken': false};
        var res = await apiAuthProvider.postData('/api/fcmTokens/remove/', null, param, context);
        if (res != null && res['success']) {
          Navigator.of(context).pop();
          closeLoading(context);
          token.logout();
          // context.router.removeLast();
          context.navigateToPath("/app/$pid/login");
        }
        else{
          Future.delayed(const Duration(seconds: 5), () => closeLoading(context));
        }
      }
    } catch(e) {
      print(e.toString());
      // context.router.removeLast();
      context.navigateToPath("/app/$pid/login");
    }
  }
}