import 'package:zhiying_comm/pages/login_page/invite/model/login_invite_user.dart';
import 'package:zhiying_comm/pages/login_page/model/login_style_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 'package:zhiying_comm/util/log/let_log.dart';
import '../../login_style_util.dart';
import 'bloc.dart';

class LoginInviteRepository {
  LoginStyleModel pageModel;
  LoginInviteUser lastInviteUser;

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

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

  /// 提交
  Future<UserInfoModel> submitInvite(LoginInviteSubmitEvent event) async {
    try {
      var result = await NetUtil.post('/api/v1/user/invite/ack', params: {
        'mobile': event.mobile,
        'zone': '86',
        '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;
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }
}
