//
//  ScreenStreamPlugin.m
//  Runner
//
//  Created by Drew on 2019/5/9.
//  Copyright © 2019 The Chromium Authors. All rights reserved.
//

#import "ScreenStreamPlugin.h"
#import <ReplayKit/ReplayKit.h>
#import <NotificationCenter/NotificationCenter.h>
#import "SCLAlertView.h"

@interface ScreenStreamPlugin() <FlutterPlugin>
@property (nonatomic) UIViewController *viewController;
@end

@implementation ScreenStreamPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

    FlutterMethodChannel* channel = [FlutterMethodChannel
                                     methodChannelWithName:@"screen_stream"
                                     binaryMessenger:[registrar messenger]];
    UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;
    ScreenStreamPlugin* instance = [[ScreenStreamPlugin alloc] initWithViewController:viewController];
    [registrar addMethodCallDelegate:instance channel:channel];
}

- (instancetype)initWithViewController:(UIViewController *)viewController {
    self = [super init];
    if(self) {
        self.viewController = viewController;
    }
    return self;
}

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
    if ([@"start" isEqualToString:call.method]) {
        NSString *playerInfoId = call.arguments[@"playerInfoId"];
        NSString *rtmpUrl = [NSString stringWithFormat:@"rtmp://202.79.174.56:1935/myapp/%@?playerInfoId=%@", playerInfoId, playerInfoId];
        NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.long.dianjing"];
        [userDefaults setValue:rtmpUrl forKey:@"rtmpUrl"];
        [userDefaults synchronize];
        CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("PUSH_START"), NULL, nil, YES);

        SCLAlertView *alert = [[SCLAlertView alloc] init];
        alert.labelTitle.font = [UIFont systemFontOfSize:14];
        NSString *appName = [[NSBundle mainBundle] infoDictionary][@"CFBundleName"];
        [alert addButton:@"OK" actionBlock:^{
            result(@"success");
        }];
        if (@available(iOS 12.0, *) ) {
            RPSystemBroadcastPickerView *pickerView = [[RPSystemBroadcastPickerView alloc] initWithFrame:CGRectMake(0, 0, 216, 80)];
            pickerView.preferredExtension = @"com.long.dianjing.ScreenRecorder";
            [alert addCustomView:pickerView];
            [alert showNotice:self.viewController title:[NSString stringWithFormat:@"点击下方图标开始录屏，或在上拉控制中心选择%@开始录屏", appName] subTitle:nil closeButtonTitle:nil duration:0.0f];
        } else {
            [alert showNotice:self.viewController title:[NSString stringWithFormat:@"请上拉呼出控制中心选择%@开始录屏", appName] subTitle:nil closeButtonTitle:nil duration:0.0f];
        }
    } else if ([@"stop" isEqualToString:call.method]) {
        NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.long.dianjing"];
        [userDefaults removeObjectForKey:@"rtmpUrl"];
        CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("PUSH_STOP"), NULL, nil, YES);
        result(@"success");
    } else if ([@"prepare" isEqualToString:call.method]) {
        result(@"success");
    }
}
@end
