//
//  PCMagicWindow.m
//  PCMClient4
//
//  Created by Hanyongying on 17/3/20.
//  Copyright © 2017年 太平洋网络. All rights reserved.
//

#import "PCMagicWindow.h"
#import "MWApi.h"

#define MAGICWINDOWSHOWVIEW   @"magicwindowshowview"
//需要注册的mLink
#define mLinkArr     @[@"liveroom",@"topicPage",@"topicDetail",@"live",@"live_terminal"]

@implementation PCMagicWindow

//注册魔窗appKey

+(void)registerApp
{
    [MWApi registerApp:@"JUL0NR8ZHJ8SBC6M8SB6MEC8MH93ROUC"];
}

//魔窗跳转展示页
+(void)registerMLinkHandler
{
    /*mLink:path=控制器的协议:/id:/参数:/参数:/参数*/
    for (NSString *mLink in mLinkArr) {
        [MWApi registerMLinkHandlerWithKey:mLink handler:^(NSURL *url, NSDictionary *params) {
            NSString *path = [params objectForKey:@"path"];//协议地址
            NSString *urlId = [params objectForKey:@"id"];
            NSString *toUrl = [NSString stringWithFormat:@"%@/%@",path,urlId];
            [PCMagicWindow handlerMLink:toUrl ext:params];
        }];
    }
}

+(void)handlerMLink:(NSString *)toUrl ext:(NSDictionary *)ext;
{
    BOOL isLaunch = [[UIApplication sharedApplication].keyWindow.rootViewController isKindOfClass:NSClassFromString(@"PCTabBarController")];
    if (isLaunch) {
        //已经启动了没有启动图，可以直接跳转
        [[PCViewController new] navigateTo:toUrl ext:ext];
    }else{
        NSDictionary *dict = @{@"toUrl":toUrl,@"ext":ext};
        [[NSUserDefaults standardUserDefaults]setObject:dict forKey:MAGICWINDOWSHOWVIEW];
    }
}


//启动图完毕之后是否跳转到指定页面
+(void)showMagicWindowViewAfterLaunch
{
    id object = [[NSUserDefaults standardUserDefaults]objectForKey:MAGICWINDOWSHOWVIEW];
    if (![object isEqual:@""]) {
        NSDictionary *dict = (NSDictionary *)object;
        NSString *toUrl = dict[@"toUrl"];
        NSDictionary *ext = dict[@"ext"];
        if (toUrl.length > 0) {
            [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:MAGICWINDOWSHOWVIEW];
            [[PCViewController new] navigateTo:toUrl ext:ext];
        }
    }
}


+(void)routeMLink:(NSURL *)url
{
    [MWApi routeMLink:url];
}
+(BOOL)continueUserActivity:(nonnull NSUserActivity *)userActivity
{
    return [MWApi continueUserActivity:userActivity];
}




@end
