//
//  CloudPhoneViewController+RBDConnect.m
//  VclustersGemini
//
//  Created by APPLE on 2021/8/16.
//  Copyright © 2021 APPLE. All rights reserved.
//

#import "PlayerViewController+AppDelegate.h"
#import "AppDelegate.h"
#import <SystemConfiguration/CaptiveNetwork.h>
#import <RCCloudPhoneSDK/RCAudioPlayer.h>
#import "ShortcutManager.h"
#import <objc/runtime.h>

@interface PlayerViewController(AppDelegate)

@end

@implementation PlayerViewController(AppDelegate)
@dynamic nowSSid;

- (void)setNowSSid:(NSString *)nowSSid {
    objc_setAssociatedObject(self, @selector(nowSSid), nowSSid, OBJC_ASSOCIATION_COPY);
}

- (NSString *)nowSSid {
    return objc_getAssociatedObject(self, @selector(nowSSid));
}

#pragma mark - UIApplication Delegate
- (void)applicationWillResignActive:(NSNotification *)notification
{
    self.nowSSid = [self observeWiFi];
    
    if (USEAAC)
    {
        if (aacPlayer)
        {
            [aacPlayer stop];
       
            aacPlayer = nil;
        }
    }
    
    [self setShowImgAndVoiceTypeFun:NO];
}

- (void)applicationDidEnterBackground:(NSNotification *)notification {
    // 当前控制器是PlayerViewController app进入后台时 移除alert
    UIViewController *currentVC = [[ShortcutManager shareInstance] findCurrentShowingViewController];
    if ([currentVC isKindOfClass:[UIAlertController class]]) {
        UIAlertController *alertVC = (UIAlertController *)currentVC;
        NSMutableAttributedString *attriMsg = [alertVC valueForKey:@"_attributedMessage"];
        if ([attriMsg.string containsString:@"授权方已收回控制权，您进入观看屏幕模式。"] ||
            [attriMsg.string containsString:@"当前云手机正在受控，是否请求获取云手机控制权？"] ||
            [attriMsg.string containsString:@"当前云手机正在受控，进入云手机后，只能预览屏幕"]) {
            return;
        }

        [currentVC dismissViewControllerAnimated:NO completion:nil];
    }
    
    self.needToDissconnectType = YES;
    [self disconnectVideoServer];
}

- (void)applicationDidBecomeActive:(NSNotification *)notification
{
    NSString *tempSSID = [self observeWiFi];
    
    self.nowSSid = tempSSID;
    
    if ([wattingView superview])
    {
        [self removeNewIndicator];
        [self showNewIndicator];
    }
    
    if (![tempSSID isEqualToString:self.nowSSid] && ![@"<<NONE>>" isEqualToString:tempSSID] && ![@"<<NONE>>" isEqualToString:self.nowSSid])
    {
        [self showNewIndicator];
        [self tryAgain];
        
        return;
    }
    
    if(self.needToDissconnectType){
        self.needToDissconnectType = NO;
        [self tryAgain];
    }
    else{
        [self requestIFrame];
    }
    
    
    [self setShowImgAndVoiceTypeFun:YES];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    
    [self disconnectVideoServer];
}

- (NSString *)observeWiFi{
        NSString *currentSSID = @"Not Found";
        CFArrayRef myArray = CNCopySupportedInterfaces();
        if (myArray != nil){
            NSDictionary* myDict = (__bridge NSDictionary *)CNCopyCurrentNetworkInfo((CFStringRef)CFArrayGetValueAtIndex(myArray,0));
            if (myDict!=nil){
                currentSSID=[myDict valueForKey:@"SSID"];
            } else {
                currentSSID=@"<<NONE>>";
            }
        } else {
            currentSSID=@"<<NONE>>";
        }
    
    if (myArray)
    {
        CFRelease(myArray);
    }
    
        return currentSSID;
}

@end
