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;
  final Map<String, dynamic> data;

  LoginInviteRepository(this.data);

  /// 获取数据，如果缓存有，则获取缓存的数据
  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 {
    Logger.log("提交的数据");
    Logger.log(data);
    try {
      Map<String, dynamic> params = {};
      params['mobile'] = event.mobile;
      params['zone'] = '86';
      params['parent_uid'] = event.num;

      if(!EmptyUtil.isEmpty(data['captcha'])){
        params['captcha'] = data['captcha'];
      }
      if (!EmptyUtil.isEmpty(data)) {
        String third = data['thirdType'];

        /// 淘宝登录
        if (GlobalConfig.LOGIN_THIRD_ALI == third) {
          Map<String, String> taobaoParam = {};
          taobaoParam['nick_name'] = data['nick'];
          taobaoParam['avatar_url'] = data['avatarUrl'];
          taobaoParam['open_id'] = data['openId'];
          taobaoParam['sid'] = data['openSid'];
          taobaoParam['access_token'] = data['topAccessToken'];
          taobaoParam['auth_code'] = data['topAuthCode'];
          params['taobao'] = taobaoParam;
        }

        /// 苹果登录
        if (GlobalConfig.LOGIN_THIRD_APPLE == third) {
          // identityTokenStr
          Map<String, String> appleParam = {};
          appleParam['token'] = data['identityTokenStr'];
          params['apple'] = appleParam;
        }

        /// 微信登录
        if (GlobalConfig.LOGIN_THIRD_WECHAT == third) {
          Map<String, String> qqParam = {};
          qqParam['nickname'] = data['nickname']?.toString();
          qqParam['gender'] = data['gender']?.toString();
          qqParam['avatar_url'] = data['avatar_url']?.toString();
          qqParam['open_id'] = data['open_id']?.toString();
          qqParam['token'] = data['token']?.toString();
          qqParam['unionid'] = data['unionid']?.toString();
          params['wechat'] = qqParam;
        }

        /// QQ登录
        if (GlobalConfig.LOGIN_THIRD_QQ == third) {
          Map<String, String> qqParam = {};
          qqParam['nickname'] = data['nickname']?.toString();
          qqParam['gender'] = data['gender']?.toString();
          qqParam['avatar_url'] = data['avatar_url']?.toString();
          qqParam['open_id'] = data['open_id']?.toString();
          qqParam['token'] = data['token']?.toString();
          qqParam['unionid'] = data['unionid']?.toString();
          params['qq'] = qqParam;
        }
      }
      var result = await NetUtil.post('/api/v1/user/invite/ack', params: params);
      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;
  }
}
