import 'package:get/get.dart';
import 'package:vnoteapp/architecture/defines.dart';
import 'package:vnoteapp/architecture/utils/prompt_box.dart';
import 'package:vnoteapp/managers/interfaces/account.dart';
import 'package:vnoteapp/rpc.dart';
import 'state.dart';

class SettingsController extends FControllerBase {
  final state = SettingsState();

  void onSubmit() async {
    try {
      final uri = Uri.parse(state.gateway);
      rpc.setServerHost("${uri.host}:${uri.port}", uri.scheme == 'https');
      rpc.clearCache();
      PromptBox.success("保存成功");
    } catch (e) {
      PromptBox.error("保存失败");
    }
  }

  /// 登出
  Future<void> logOut() async {
    final result = await Get.find<IAccountManager>().logout();
    if (result) {
      Get.offAllNamed("/login");
    }
  }

  @override
  void onInit() {
    state.gateway = rpc.currentHostAddress;
    state.version = "2.0.0";
    super.onInit();
  }

  @override
  void onReady() {
    //
    super.onReady();
  }

  @override
  void onClose() {
    _doDispose();
    super.onClose();
  }

  void _doDispose() {}
}
