//
//  GMThirdPartyLoginHelper.m
//  Gengmei
//
//  Created by licong on 2017/8/2.
//  Copyright © 2017年 更美互动信息科技有限公司. All rights reserved.
//

#import "GMThirdPartyLoginHelper.h"
#import <WechatOpenSDK/WXApi.h>
#import <Weibo_SDK/WeiboSDK.h>
#import "GMThirdPartyDefine.h"
#import "GMThirdPartyOauthManager.h"
#import <TencentOpenAPI/TencentOAuth.h>

@implementation GMThirdPartyUser

@end

@implementation GMThirdPartyLoginHelper


+ (void)loginByPlatform:(GMSharePlatform)platform
             onUserSync:(GMThirdPartyUserHandler)userHandler{
    [GMThirdPartyOauthManager shareInstance].user.platformType = platform;
    if (platform == GMSharePlatformQQ) {
        NSArray* permissions = [NSArray arrayWithObjects:@"get_user_info",@"get_simple_userinfo", @"add_t", nil];
        [[GMThirdPartyOauthManager shareInstance].tencentOAuth setAuthShareType: AuthShareType_QQ];
        [[GMThirdPartyOauthManager shareInstance].tencentOAuth authorize:permissions inSafari:NO];
        __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:kQQLogin object:nil queue: [NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
            GMShareResponseState state = (GMShareResponseState )[note.userInfo[@"state"] integerValue];
            userHandler(state, note.object);
            [[NSNotificationCenter defaultCenter] removeObserver:observer];
        }];
    } else if (platform == GMSharePlatformWechat) {
        if ([WXApi isWXAppInstalled]){
            [GMThirdPartyLoginHelper wechatLogin];
            __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:kWechatLogin object:nil queue: [NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
                GMShareResponseState state = (GMShareResponseState )[note.userInfo[@"state"] integerValue];
                userHandler(state, note.object);
                [[NSNotificationCenter defaultCenter] removeObserver:observer];
            }];
        }
    } else if (platform  == GMSharePlatformSinaWeibo){
        WBAuthorizeRequest *request = [WBAuthorizeRequest request];
        request.redirectURI = SINAWEIBO_REDIRECTURI ;
        //http://open.weibo.com/wiki/Scope
        request.scope = @"all";
        request.userInfo = @{@"flag": @"authorize"};
        [WeiboSDK sendRequest:request];
        __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:kSinaLogin object:nil queue: [NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
            GMShareResponseState state = (GMShareResponseState )[note.userInfo[@"state"] integerValue];
            userHandler(state, note.object);
            [[NSNotificationCenter defaultCenter] removeObserver:observer];
        }];
    } else if (platform == GMSharePlatformApple) {
        if (@available(iOS 13.0, *)) {
            // 基于用户的Apple ID授权用户，生成用户授权请求的一种机制
            ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
            // 创建新的AppleID 授权请求
            ASAuthorizationAppleIDRequest *appleIDRequest = [appleIDProvider createRequest];
            // 在用户授权期间请求的联系信息
            appleIDRequest.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
            // 由ASAuthorizationAppleIDProvider创建的授权请求 管理授权请求的控制器
            ASAuthorizationController *authorizationController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[appleIDRequest]];
            // 设置授权控制器通知授权请求的成功与失败的代理
            authorizationController.delegate = [GMThirdPartyOauthManager shareInstance];
            // 设置提供 展示上下文的代理，在这个上下文中 系统可以展示授权界面给用户
            authorizationController.presentationContextProvider = [GMThirdPartyOauthManager shareInstance];
            // 在控制器初始化期间启动授权流
            [authorizationController performRequests];
            
            __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:kAppleLogin object:nil queue: [NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
                GMShareResponseState state = (GMShareResponseState )[note.userInfo[@"state"] integerValue];
                userHandler(state, note.object);
                [[NSNotificationCenter defaultCenter] removeObserver:observer];
            }];
        } else {
            userHandler(GMShareResponseStateFail, nil);
        }
    }
}

+ (void)wechatLogin {
    if ([WXApi isWXAppInstalled]){
        //构造SendAuthReq结构体
        SendAuthReq* req = [[SendAuthReq alloc ] init];
        req.scope = @"snsapi_userinfo" ;
        req.state = @"Gengmei" ;
        // 第三方向微信终端发送一个SendAuthReq消息结构
        [WXApi sendReq:req completion:^(BOOL success) {
            
        }];
    }
}

+ (void)thirdPartyLogout {
    if ([GMThirdPartyOauthManager shareInstance].user.platformType == GMSharePlatformQQ){
        [GMThirdPartyOauthManager shareInstance].tencentOAuth = [[TencentOAuth alloc] initWithAppId:QQ_APPID andDelegate:[GMThirdPartyOauthManager shareInstance]];
    }
}

@end
