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/global_config.dart';
import 'package:zhiying_comm/util/log/let_log.dart';
import 'package:zhiying_comm/util/mob_util/mob_util.dart';
import 'package:zhiying_comm/util/net_util.dart';
import 'package:zhiying_comm/models/user/user_info_model.dart';
import '../../login_style_util.dart';
import 'bloc.dart';

///
/// 账户登陆
///
class LoginBindPhoneRepository {
  final Map<String, dynamic> data;

  LoginBindPhoneRepository(this.data);

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

  /// 获取验证码
  Future<bool> fetchGetVcode(LoginBindPhoneGetVCodeEvent event) async {
    print('mobile = ${event.mobile}');
    var result = await MobUtil.getTextCode(event?.mobile, smsCodeType: SMSCodeType.AUTO);
    return result;
  }

  /// 验证码登陆
  Future<UserInfoModel> loginTypeVCode(LoginBindPhoneTypeVCodeEvent event) async {
    try {
      String third = data['thirdType'];
      if (EmptyUtil.isEmpty(third)) return null;

      /// 请求参数
      Map<String, dynamic> params = {};
      params['mobile'] = event.mobile;
      params['zone'] = '86';
      params['captcha'] = event.captcha;

      /// 淘宝登录
      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/sign/fast/in', params: params);
      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;
        }
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }
}
