//
//  XXGUCenterViewController.m
//  XXGPlayKit
//
//  Created by apple on 2025/3/17.
//

#import "XXGUCenterNewViewController.h"
#import "XXGWindowManager.h"
#import "XXGFloatView.h"
#import "UIColor+XXGColor.h"
#import "UIDevice+XXGDevice.h"
#import "UIImage+XXGImage.h"
#import "XXGPlayKitConfig.h"


@implementation XXGUCenterNewViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = XXGUIDriver.xxpk_ucenter_bangs_color ? [UIColor xxpk_colorWithHexString:XXGUIDriver.xxpk_ucenter_bangs_color]:UIColor.whiteColor;

    self.xxpk_router = self.xxpk_object;
    [XXGFloatView xxpk_hide];
    
//    self.view.backgroundColor = UIColor.clearColor;
    self.xxpk_wkview.opaque = NO;
    self.view.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
    
    [self.xxpk_wkview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(xxpk_refresh) name:XXGUIDriver.xxpk_data_ui.xxpk_ntf_ucenter_refresh object:nil];
}

- (void)xxpk_refresh {
    [self.xxpk_wkview reload];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self xxpk_setOrientation];
}

//- (void)viewDidAppear:(BOOL)animated {
//    [super viewDidAppear:animated];
//    [self xxpk_setOrientation];
//}

-(void)viewWillDisappear:(BOOL)animated {
    if ([XXGUIDriver xxpk_comeinedBoxName] && XXGPlayKitConfig.shared.xxpk_adaptionCof.xxpk_docker.xxpk_status) {
        [XXGFloatView xxpk_show];
    }
    [super viewWillDisappear:animated];
    NSLog(@"%s",__func__);
}

- (CGSize)xxpk_ucenterSize {
    UIWindow *currentWindow = [[XXGWindowManager shared] xxpk_currentWindow];
    UIEdgeInsets safe = currentWindow.safeAreaInsets;
    CGFloat ucnenter_size = 0;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
#pragma clang diagnostic pop
    // 竖屏
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        if (![UIDevice hasNotch] || [UIDevice isIPad]) {//无刘海
            ucnenter_size = UIScreen.mainScreen.bounds.size.height*XXGUIDriver.xxpk_size_percent;
        }else {// 有刘海
            ucnenter_size = (UIScreen.mainScreen.bounds.size.height - safe.bottom)*XXGUIDriver.xxpk_size_percent;
        }
        return CGSizeMake(UIScreen.mainScreen.bounds.size.width, ucnenter_size);
        
    }else { // 横屏
        if (![UIDevice hasNotch] || [UIDevice isIPad]) {//无刘海
            ucnenter_size = UIScreen.mainScreen.bounds.size.width * XXGUIDriver.xxpk_size_percent;
        }else {// 有刘海
            if (orientation == UIInterfaceOrientationLandscapeRight) {
                ucnenter_size = (UIScreen.mainScreen.bounds.size.width - safe.left) * XXGUIDriver.xxpk_size_percent;
            }else {
                ucnenter_size = (UIScreen.mainScreen.bounds.size.width - safe.right) * XXGUIDriver.xxpk_size_percent;
            }
        }
        return CGSizeMake(ucnenter_size, UIScreen.mainScreen.bounds.size.height);
    }
}

- (void)xxpk_setOrientation {
    UIWindow *currentWindow = [[XXGWindowManager shared] xxpk_currentWindow];
    UIEdgeInsets safe = currentWindow.safeAreaInsets;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
#pragma clang diagnostic pop
    
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        [self.view mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.bottom.mas_equalTo(0);
            make.size.mas_equalTo(self.xxpk_ucenterSize);
        }];
    }else {
        
        if (orientation == UIInterfaceOrientationLandscapeRight) {
            
            [self.view mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(0);
                make.left.mas_equalTo(safe.left);
                make.size.mas_equalTo(self.xxpk_ucenterSize);
            }];
        }else {
            [self.view mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.left.mas_equalTo(0);
                make.size.mas_equalTo(self.xxpk_ucenterSize);
            }];
        }
    }
}

- (void)xxpk_touchesBlank:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super xxpk_touchesBlank:touches withEvent:event];
    [[XXGWindowManager shared] xxpk_dismissWindowWithRootViewController:self.navigationController];
}

@end
