/*
 * @author lsy
 * @date   2019-09-04
 **/
import 'package:gmalpha_flutter/commonModel/net/Responce/SimpleResponce.dart';
import 'package:gmalpha_flutter/userModel/service/local/UserLocal.dart';
import 'package:gmalpha_flutter/userModel/service/remote/UserRemote.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/TestUserEntity.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/UserEntity.dart';
import 'package:rxdart/rxdart.dart';

class UserRepository {
  UserRemote _remote;
  UserLocal _local;

  static UserRepository _userRepository;

  UserRepository._() {
    _remote = UserRemote.getInstance();
    _local = UserLocal.getInstance();
  }

  static UserRepository getInstance() {
    if (_userRepository == null) {
      _userRepository = UserRepository._();
    }
    return _userRepository;
  }

  Observable<UserEntity> getUserInfo(String token) {
    _remote.getUserInfo(token).map((value) {
      if (value != null) {
        _local.saveUserInfo(value);
      }
      return value;
    });
  }

  Observable<TestUserEntity> resignUser(
      String name, String word, String reword) {
    return _remote.resignUser(name, word, reword).map((value) {
      if (value != null) {
        _local.saveUser(value);
      }
      return value;
    });
  }

  Observable<SimpleResponce> logout() {
    return _remote.logout().map((value) {
      if (value != null) {
        _local.logout();
      }
      return value;
    });
  }
}
