//
//  PTTWindowViewController.m
//  GMEDemoAudio
//
//  Created by RoonieChen on 2020/12/1.
//  Copyright © 2020 tobinchen. All rights reserved.
//

#import "PTTWindowViewController.h"

@interface PTTWindowViewController ()<NSWindowDelegate>

@property (nonatomic, strong) PTTViewController *pttVC;

@end

@implementation PTTWindowViewController

- (instancetype)init {
    if (self) {
        NSRect frame = [self defaultFrame];
        NSWindow * window = [[NSWindow alloc] initWithContentRect:frame styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskMiniaturizable backing:NSBackingStoreBuffered defer:NO screen:[NSScreen screens].firstObject];
        self.window = window;
        CGSize minSize = frame.size;
        [window setMinSize:minSize];
        [window setWindowController:self];
        [self.window setDelegate:self];
        self.pttVC = [[PTTViewController alloc] initWithNibName:@"PTTViewController" bundle:nil];
        self.window.contentViewController = self.pttVC;
        self.window.title = @"PTT";
    }
    return self;
}

- (NSRect)defaultFrame {
    NSRect defaultRect = NSZeroRect;
    
    NSRect screenFrame = [[NSScreen mainScreen] frame];
    defaultRect.size =  NSMakeSize(420, 710);
    defaultRect.origin = NSMakePoint(floorf(NSWidth(screenFrame) - NSWidth(defaultRect)) / 2.0, floorf(NSHeight(screenFrame) - NSHeight(defaultRect)) / 2.0);
    
    return defaultRect;
}

- (void)windowDidLoad {
    [super windowDidLoad];
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
{
    return NO;
}

- (void)close {
    NSArray *sheetsArray = [self.window.sheets copy];
    for (NSWindow *window in sheetsArray) {
        [self.window endSheet:window];
    }
    [self.window makeFirstResponder:nil];
    [self.window orderOut:nil];
    [self.window close];
    if (self.pttVC) {
        [self.pttVC windowShouldClose];
        self.pttVC = nil;
    }
}

- (BOOL)windowShouldClose:(NSWindow *)sender {
    [self close];
    return YES;
}

@end
