import 'package:zhiying_comm/pages/login_page/invite/model/login_invite_user.dart';
import 'package:zhiying_comm/pages/login_page/model/login_model.dart';
import 'package:zhiying_comm/util/empty_util.dart';
import 'package:zhiying_comm/util/net_util.dart';
import 'package:zhiying_comm/util/global_config.dart';
import 'package:zhiying_comm/models/user/user_info_model.dart';
import '../../login_util.dart';
import 'bloc.dart';

class LoginInviteRepository {
  LoginModel pageModel;

  /// 获取数据，如果缓存有，则获取缓存的数据
  Future<LoginModel> fetchData() async {
    pageModel = await LoginUtil.getLoginModel();
    if (!EmptyUtil.isEmpty(pageModel)) {
      return pageModel;
    }
    return null;
  }

  /// 获取邀请人信息
  Future<LoginInviteUser> fetchInviteUserInfo(LoginInviteQueryEvent event) async {
    var result = await NetUtil.post('/api/v1/user/invite/${event.num}', method: NetMethod.GET);
    if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
      LoginInviteUser model = LoginInviteUser.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      if(null != model)
        return model;
      else return null;
    } else {
      return null;
    }
  }

  /// 提交
  Future<UserInfoModel> submitInvite(LoginInviteSubmitEvent event) async {
    var result = await NetUtil.post('/api/v1/user/invite/ack', params: {'mobile': event.mobile, 'parent_uid': event.num});
    if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
      UserInfoModel model = UserInfoModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      if (null != model) {
        model.mobile = event.mobile;
        return model;
      }
      return null;
    }
    return null;
  }
}
