import 'package:domain/repository/demo_repository.dart';
import 'package:flutter/material.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import 'package:shared/utils/log_utils.dart';

import 'visitor_state.dart';

part 'visitor_view_model.g.dart';

@riverpod
class VisitorViewModel extends _$VisitorViewModel {
  late DemoRepository demoRepository;

  @override
  VisitorState build() {
    demoRepository = ref.read(demoRepositoryProvider);
    return VisitorState();
  }

  /// 获取服务器时间测试
  void fetchServerTime() async {
    //请求网络
    final result = await demoRepository.fetchServerTime("");
    //校验成功失败
    if (result.isSuccess) {
      state = state.copyWith(serverTime: result.data);
      ToastEngine.show("当前服务器时间戳:${result.data?.timestamps}");
    } else {
      ToastEngine.show(result.errorMsg ?? "Network Load Error");
    }
  }
}
