// GENERATED CODE - DO NOT MODIFY BY HAND

// **************************************************************************
// ServiceGenerator
// **************************************************************************

import 'dart:convert';

import 'dart:io';

import 'package:rxdart/rxdart.dart';

import 'package:gmalpha_flutter/userModel/service/remote/entity/TestUserEntity.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/UserEntity.dart';
import 'package:gmalpha_flutter/commonModel/net/Responce/SimpleResponce.dart';

import 'package:gmalpha_flutter/commonModel/net/DioUtil.dart';

class UserLoginApiImpl {
  factory UserLoginApiImpl() => _sharedInstance();

  static UserLoginApiImpl _instance;

  UserLoginApiImpl._() {}

  static UserLoginApiImpl _sharedInstance() {
    if (_instance == null) {
      _instance = UserLoginApiImpl._();
    }
    return _instance;
  }

  Observable<TestUserEntity> resign(
      String name, String word, String repassword) {
    return Observable.fromFuture(DioUtil().post('user/register', data: {
      'username': name,
      'password': word,
      'repassword': repassword
    })).map((value) {
      if (value.statusCode == 200) {
        Map map = json.decode(value.toString());
        return TestUserEntity.fromJson(map);
      } else {
        throw HttpException("statusCode error :${value.statusCode}");
      }
    });
  }

  Observable<UserEntity> getUserInfo(String token) {
    return Observable.fromFuture(
        DioUtil().get('user/info', data: {'token': token})).map((value) {
      if (value.statusCode == 200) {
        Map map = json.decode(value.toString());
        return UserEntity.fromJson(map);
      } else {
        throw HttpException("statusCode error :${value.statusCode}");
      }
    });
  }

  Observable<SimpleResponce> logout() {
    return Observable.fromFuture(DioUtil().get('user/logout/json'))
        .map((value) {
      if (value.statusCode == 200) {
        Map map = json.decode(value.toString());
        return SimpleResponce.fromJson(map);
      } else {
        throw HttpException("statusCode error :${value.statusCode}");
      }
    });
  }
}
