import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:netrain_flutter_app/douwen/custom/http/AdRepository.dart';

import 'ViewState.dart';

class BaseViewModel extends ChangeNotifier {
  var _viewState;
  var num = ValueNotifier<int>(0);
  var showText = ValueNotifier<String>("稍等");

  set viewState(ViewState viewState) => setViewState(viewState);

  get viewState => _viewState;

  void click() {
    num.value = (num.value ?? 0) + 1;
    num.notifyListeners();
  }

  void setNormal() {
    viewState = ViewState.Normal;
  }

  void setEmpty() {
    viewState = ViewState.Empty;
  }

  void setLoading() {
    viewState = ViewState.Loading;
  }

  void setError() {
    viewState = ViewState.Error;
  }

  setViewState(ViewState viewState) {
    if (_viewState == viewState) {
      return;
    }
    _viewState = viewState;
    notifyListeners();
  }

  void login() {
    AdRepository.instance.login((str) {
      Fluttertoast.showToast(msg: str);
    }, (iEntity) {
      showText.post(iEntity.msg);
      Fluttertoast.showToast(msg: iEntity.msg);
    });
  }


}

extension ValueNotifierValue<T> on ValueNotifier<T>{
  void post(T t){
    value = t;
    notifyListeners();
  }
}