//
//  KMRecommondPopWindow.m
//  PDF Reader Pro Edition
//
//  Created by Niehaoyu on 2023/07/12.
//

#import "KMRecommondPopWindow.h"
#import "PDF_Reader_Pro-Swift.h"

@interface KMRecommondPopWindow ()<
NSWindowDelegate>

@property (assign) IBOutlet NSView *contendView;
@property (assign) IBOutlet NSImageView *infoImg;
@property (assign) IBOutlet NSTextField *titleLbl;
@property (assign) IBOutlet NSTextField *subTitleLbl;
@property (assign) IBOutlet NSTextField *expireDateLbl;
@property (assign) IBOutlet NSButton *buyBtn;

@property (nonatomic, strong) KMAdvertisementItemInfo *info;


@end

@implementation KMRecommondPopWindow

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

+ (KMRecommondPopWindow *)defaultWindow {
    static KMRecommondPopWindow *singleton = nil;
    static dispatch_once_t pred;
    dispatch_once(&pred, ^{
        singleton = [[KMRecommondPopWindow alloc] init];
    });
    return singleton;
}

- (id)init {
    if (self = [super initWithWindowNibName:@"KMRecommondPopWindow"]) {
        
    }
    return self;
}

- (void)setRecommondInfo:(id)recommondInfo {
    _recommondInfo = recommondInfo;
    
    _info = (KMAdvertisementItemInfo *)recommondInfo;
}

- (void)windowDidLoad {
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    
    self.window.delegate = self;
    
    [self.window standardWindowButton:NSWindowMiniaturizeButton].hidden = YES;
    [self.window standardWindowButton:NSWindowZoomButton].hidden = YES;
    
    self.titleLbl.textColor = [KMAppearance KMColor_Layout_H0];
    self.subTitleLbl.textColor = [KMAppearance KMColor_Layout_H0];
    self.expireDateLbl.textColor = [KMAppearance KMColor_Layout_H0];
    self.buyBtn.wantsLayer = YES;
    self.buyBtn.layer.backgroundColor = [KMAppearance KMColor_Interactive_M0].CGColor;
    NSURL *url = [NSURL URLWithString: [KMAdvertisementModelTransition transitionImagePathWithImage:_info.image highlight: YES]];
    
    __weak typeof(self)weakSelf = self;
    [KMAdvertisementImage imageWithURLWithUrl:url completion:^(NSImage *image) {
        weakSelf.infoImg.image = image;
    }];

    self.titleLbl.stringValue = [KMAdvertisementModelTransition transitionLanguageWithLangeuage:_info.name];
    NSString *subT = [KMAdvertisementModelTransition transitionLanguageWithLangeuage:_info.subTitle];
    if ([subT containsString:@"\\n"]) {
        subT = [subT stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    }
    self.subTitleLbl.stringValue = subT;
    self.expireDateLbl.stringValue = [KMAdvertisementModelTransition transitionLanguageWithLangeuage:_info.otherTitle];
    
    [self.buyBtn setTitle:[KMAdvertisementModelTransition transitionLanguageWithLangeuage:_info.btnTitle]];
    [self.buyBtn setTitleColor:[KMAppearance KMColor_Layout_W0]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recommondInfoUpdateNoti:) name:@"KMRecommondInfoUpdateNoti" object:nil];

}

#pragma mark - Setter


#pragma mark - IBAction
- (IBAction)buyAction:(NSButton *)sender {
    if (_info.version) {
        [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[KMAdvertisementModelTransition transitionLanguageWithLangeuage:_info.linkURL]]];
        
        if (_info.firebase != nil) {
            [KMAdvertisementModelTransition sendFireBaseEventWithFirebase:_info.firebase];
        }
    }
}

#pragma mark - NSWindowDelegate
- (BOOL)windowShouldClose:(NSWindow *)sender {
    if (self.closeHandle) {
        self.closeHandle(self);
    }
    return YES;
}

#pragma mark - Noti
- (void)recommondInfoUpdateNoti:(NSNotification *)noti {
    if ([noti.object[@"unique"] isEqualToString:_info.version]) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.infoImg.image = _info.iconImage;
        });
    }
}

@end