import 'dart:math';

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

import '../../login_util.dart';
import 'bloc.dart';

///
/// 账户登陆
///
class LoginAccountRepository {
  /// 获取数据，如果缓存有，则获取缓存的数据
  Future<LoginModel> fetchData() async {
    var result = await LoginUtil.getLoginModel();
    if (!EmptyUtil.isEmpty(result)) {
      return result;
    }
    return null;
  }

  /// 获取验证码
  Future<bool> fetchGetVcode(LoginAccountGetVcodeEvent event) async {
    print('mobile = ${event.mobile}');
    var result = await NetUtil.post('/api/v1/sign/sms/fast/in', params: {'mobile': event.mobile});
    if (NetUtil.isSuccess(result)) {
      return true;
    }
    return false;
  }

  /// 密码登陆
  Future<UserInfoModel> loginTypePass(LoginAccountTypePasswordEvent event) async {
    var result = await NetUtil.post('/api/v1/sign/in', params: {'username': event.username, 'password': event.password});
    if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
      UserInfoModel loginUser = UserInfoModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      if (null != loginUser) {
        loginUser.mobile = event.username;
        return loginUser;
      }
      return null;
    }
    return null;
  }

  /// 验证码登陆
  Future<UserInfoModel> loginTypeVcode(LoginAccountTypeVcodeEvent event) async {
    var result = await NetUtil.post('/api/v1/sign/fast/in', params: {'mobile': event.mobile, 'captcha': event.captcha});
    if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
      UserInfoModel loginUser = UserInfoModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      if (null != loginUser) {
        loginUser.mobile = event.mobile;
        return loginUser;
      }
      return null;
    }
    return null;
  }

  /// 检查验证码
  Future<bool> checkVcode(LoginAccountCheckVcodeEvent event) async {}
}
