import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:zhiying_comm/pages/login_page/model/login_style_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import '../login_style_util.dart';

class LoginRepository {
  /// 获取页面数据
  Future<LoginStyleModel> fetchNetPageData() async {
    return await LoginStyleUtil.fetchNetPageData();
  }

  /// 获取缓存的页面数据
  Future<LoginStyleModel> fetchCachePageData() async {
    return await LoginStyleUtil.fetchCachePageData();
  }

  /// apple 登录
  Future<UserInfoModel> appleLogin({@required final String identityToken}) async {
    try{
      Map<String, String> params = {};
      params['token'] = identityToken;

      var result = await NetUtil.post('/api/v1/sign/fast/in',
      queryParameters: {'third_party_login': 'apple'},
        params: {'apple' : params},
        method: NetMethod.POST,
      );
      if(NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])){
        return UserInfoModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      }

    }catch(e,s){
      Logger.error(e,s);
    }
    return null;
  }

  /// 淘宝登录
  Future<UserInfoModel> aliLogin(
      {@required final String nick, @required final String avatarUrl, @required final String openId, final String openSid, final String topAccessToken, final String topAuthCode}) async {
    try {
      Map<String, String> params = {};
      params['nick_name'] = nick;
      params['avatar_url'] = avatarUrl;
      params['open_id'] = openId;
      params['access_token'] = topAccessToken;
      params['sid'] = openSid;
      params['auth_code'] = topAuthCode;
      var result = await NetUtil.post(
        '/api/v1/sign/fast/in',
        queryParameters: {'third_party_login': 'taobao'},
        params: {'taobao': params},
        method: NetMethod.POST,
      );
      if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
        return UserInfoModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }


  /// 微信登录
  Future<UserInfoModel> weChatLogin(final Map<dynamic, dynamic> data) async {
    try {
      var dbInfo = jsonDecode(data['dbInfo']);
      Map<String, String> params = {};
      params['nickname'] = dbInfo['nickname']?.toString();
      params['gender'] = dbInfo['gender']?.toString() == '0' ? '1' : '2';
      params['avatar_url'] = dbInfo['icon']?.toString();
      params['open_id'] = dbInfo['userID']?.toString();
      params['unionid'] = dbInfo['unionid']?.toString();
      params['token'] = dbInfo['token']?.toString();

      var result = await NetUtil.post(
        '/api/v1/sign/fast/in',
        params: {'wechat' :params},
        queryParameters: {'third_party_login': 'wechat'},
        method: NetMethod.POST,
      );

      if(NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])){
        return UserInfoModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }

  /// QQ 登录
  Future<UserInfoModel> qqLogin(final Map<dynamic, dynamic> data) async {
    try {
      var dbInfo = jsonDecode(data['dbInfo']);

      Map<String, String> params = {};
      params['nickname'] = dbInfo['nickname']?.toString();
      params['gender'] = dbInfo['gender']?.toString() == '0' ? '1' : '2';
      params['avatar_url'] = dbInfo['icon']?.toString();
      params['open_id'] = dbInfo['userID']?.toString();
      params['unionid'] = dbInfo['unionid']?.toString();
      params['token'] = dbInfo['token']?.toString();

      var result = await NetUtil.post(
        '/api/v1/sign/fast/in',
        params: {'qq': params},
        queryParameters: {'third_party_login': 'qq'},
        method: NetMethod.POST,
      );

      if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
        return UserInfoModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      }
    } catch (e, s) {
      Logger.error(e, s);
    }
    return null;
  }
}
