//
//  KMConvertFunctionPayViewController.m
//  PDF Reader Pro Edition
//
//  Created by 丁林圭 on 2019/5/23.
//

#import "KMConvertComparePayViewController.h"
//#import "NSButton+TitleColor.h"
#import "NSImage_SKExtensions.h"

#import <PDF_Reader_Pro-Swift.h>

@interface KMConvertComparePayViewController ()<NSPageControllerDelegate>

@property (assign) IBOutlet NSView *contendView;
@property (assign) IBOutlet NSTextField *titleLabel;
@property (assign) IBOutlet NSTextField *contentLabel;

@property (assign) IBOutlet KMBox *purchaseBox;
@property (assign) IBOutlet NSTextField *priceLabel;
@property (assign) IBOutlet NSTextField *priceDesLabel;
@property (assign) IBOutlet KMButton *purchaseButton;

@property (assign) IBOutlet HyperLinkButton *restoreBtn;
@property (assign) IBOutlet HyperLinkButton *privacyButton;
@property (assign) IBOutlet HyperLinkButton *termOfSerButton;

@property (assign) IBOutlet NSTextView *purchaseContenxtTextView;
 

@property (nonatomic,retain)  NSArray *imageArray;

@property (nonatomic,assign) NSInteger indexPage;

@property (nonatomic,assign) NSInteger selectIndex;


@end

@implementation KMConvertComparePayViewController
- (void)dealloc {
    _block = nil;
    [NSDistributedNotificationCenter.defaultCenter removeObserver:self];
}

- (instancetype)initWithSelectIndex:(NSInteger)selectIndex {
    if (self = [super initWithNibName:@"KMConvertComparePayViewController" bundle:nil]) {
        self.indexPage = selectIndex;
    }
    return self;
}

- (void)loadView {
    [super loadView];
     
    self.contendView.wantsLayer = YES;
    
    self.purchaseBox.wantsLayer = YES;
    self.purchaseBox.layer.masksToBounds = YES;
    self.purchaseBox.layer.cornerRadius = CGRectGetHeight(self.purchaseBox.frame)/2.;
    
    self.purchaseContenxtTextView.wantsLayer = YES;
    self.purchaseContenxtTextView.layer.backgroundColor = [NSColor clearColor].CGColor;
    self.purchaseContenxtTextView.backgroundColor = [NSColor clearColor];
    
    [self localizedLanguage];
    
    [self updateViewColor];
     
    [self reloadData];
    [NSDistributedNotificationCenter.defaultCenter addObserver:self selector:@selector(themeChanged:) name:@"AppleInterfaceThemeChangedNotification" object: nil];
    
}

- (void)localizedLanguage {
    
    self.titleLabel.font = [NSFont UbuntuBoldFontWithSize:20];
    self.contentLabel.font = [NSFont SFProTextRegularFont:14];
    self.priceLabel.font = [NSFont UbuntuBoldFontWithSize:20];
    self.priceDesLabel.font = [NSFont SFProTextSemiboldFont:13];
    self.restoreBtn.font = [NSFont SFProTextRegularFont:13];
    self.privacyButton.font = [NSFont SFProTextRegularFont:13];
    self.termOfSerButton.font = [NSFont SFProTextRegularFont:13];
    self.purchaseContenxtTextView.font = [NSFont SFProTextRegularFont:12];
    
    self.titleLabel.stringValue = NSLocalizedString(@"Purchase PDF to Office Pack", nil);
    self.contentLabel.stringValue = [NSString stringWithFormat:@"%@\n%@",NSLocalizedString(@"The premium version can only convert first 10 pages.", nil),NSLocalizedString(@"Purchase PDF to Office Pack to convert all pages in high quality.", nil)];
    
    self.restoreBtn.title = [NSString stringWithFormat:@"%@",NSLocalizedString(@"Restore", nil)];
    self.privacyButton.title = [NSString stringWithFormat:@"%@",NSLocalizedString(@"Privacy Policy", nil)];
    self.termOfSerButton.title = [NSString stringWithFormat:@"%@",NSLocalizedString(@"Terms of Service", nil)];
    self.restoreBtn.toolTip = [NSString stringWithFormat:@"%@",NSLocalizedString(@"Restore", nil)];
    self.privacyButton.toolTip = [NSString stringWithFormat:@"%@",NSLocalizedString(@"Privacy Policy", nil)];
    self.termOfSerButton.toolTip = [NSString stringWithFormat:@"%@",NSLocalizedString(@"Terms of Service", nil)];
    
    for (HyperLinkButton *button in @[self.restoreBtn, self.privacyButton, self.termOfSerButton]) {
        button.mouseMoveCallback = ^(BOOL mouseEntered) {
            if ([KMAppearance isDarkMode]) {
                if (mouseEntered) {
                    [button setTitleColor:[KMAppearance KMColor_Layout_W70]];
                } else {
                    [button setTitleColor:[KMAppearance KMColor_Layout_H2]];
                }
            } else {
                if (mouseEntered) {
                    [button setTitleColor:[KMAppearance KMColor_Layout_H0]];
                } else {
                    [button setTitleColor:[KMAppearance KMColor_Layout_H2]];
                }
            }
        };
        
    }
    
    self.purchaseButton.wantsLayer = YES;
    __weak typeof(self) weakSelf = self;
    self.purchaseButton.mouseMoveCallback = ^(BOOL mouseEntered) {
        if (weakSelf.purchaseButton.enabled) {
            if (mouseEntered) {
                weakSelf.purchaseButton.layer.backgroundColor = [[NSColor blackColor] colorWithAlphaComponent:0.15].CGColor;
            } else {
                weakSelf.purchaseButton.layer.backgroundColor = [NSColor clearColor].CGColor;
            }
        }
    };
    

    NSString * price = @"";
    if ([IAPProductsManager defaultManager].pdfToOfficePackPermanent_pro.isOffers) {
        price = [IAPProductsManager defaultManager].pdfToOfficePackPermanent_pro.offersPrice;
    } else {
        price = [IAPProductsManager defaultManager].pdfToOfficePackPermanent_pro.price;
    }
    
    self.priceLabel.stringValue = price ? : @"";
 
    self.priceDesLabel.stringValue = NSLocalizedString(@"one-time purchase", nil);
}

- (void)updateViewColor {
    
    if ([KMAppearance isDarkMode]) {
        self.contendView.layer.backgroundColor = [NSColor colorWithRed:24/255. green:22/255. blue:31/255. alpha:1].CGColor;
        self.titleLabel.textColor = [KMAppearance KMColor_Layout_W0];
        self.contentLabel.textColor = [KMAppearance KMColor_Layout_W0];
        self.purchaseContenxtTextView.textColor = [[NSColor whiteColor] colorWithAlphaComponent:0.3];
        
    } else {
        self.contendView.layer.backgroundColor = [NSColor colorWithRed:247/255. green:245/255. blue:255/255. alpha:1].CGColor;
        self.titleLabel.textColor = [NSColor colorWithRed:14/255. green:17/255. blue:20/255. alpha:1];
        self.contentLabel.textColor = [KMAppearance KMColor_Layout_H0];
        self.purchaseContenxtTextView.textColor = [[NSColor blackColor] colorWithAlphaComponent:0.3];
        
    }
    self.priceLabel.textColor = [NSColor whiteColor];
    self.priceDesLabel.textColor = [[NSColor whiteColor] colorWithAlphaComponent:0.8];
    
    self.purchaseBox.fillColor = [NSColor colorWithRed:1 green:94/255. blue:44/255. alpha:1];
    
    [self.restoreBtn setTitleColor:[KMAppearance KMColor_Layout_H2]];
    [self.privacyButton setTitleColor:[KMAppearance KMColor_Layout_H2]];
    [self.termOfSerButton setTitleColor:[KMAppearance KMColor_Layout_H2]];
      
    
}

- (void)removeObserver {
    [NSDistributedNotificationCenter.defaultCenter removeObserver:self];
}

- (void)reloadData {
#ifndef VERSION_DMG
    if ([KMMemberInfo shared].isPermitConvert) {
        [self.purchaseButton setEnabled:NO];
        self.purchaseButton.layer.backgroundColor = [[NSColor whiteColor] colorWithAlphaComponent:0.4].CGColor;
    } else {
        [self.purchaseButton setEnabled:YES];
    }
#endif
}

#pragma mark - IBAction
- (IBAction)buttonItemClick_Subscribe1:(id)sender {
    KMConvertComparePayType type =  KMConvertComparePayType_Refresh;
    
#if VERSION_DMG
    type = KMConvertComparePayType_Refresh;
#else
    type = KMConvertComparePayType_Restore;
#endif
    if (self.block) {
        self.block(type);
    }
}

- (IBAction)buttonItemClick_Subscribe2:(id)sender {
    KMConvertComparePayType type =  KMConvertComparePayType_STORE;
    
#if VERSION_DMG
    type = KMConvertComparePayType_DMG;
#else
    type = KMConvertComparePayType_STORE;
#endif
    if (self.block) {
        self.block(type);
    }
}

- (IBAction)privacyButtonAction:(id)sender {
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.pdfreaderpro.com/privacy-policy"]];
}

- (IBAction)termOfSerButtonAction:(id)sender {
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.pdfreaderpro.com/terms_of_service"]];
}

- (void)themeChanged:(NSNotification *)notification {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self updateViewColor];
    });
}

@end