//
//  KMVerificationActivateViewController.m
//  PDF Reader Pro Edition
//
//  Created by 王帅 on 2018/5/8.
//

#import "KMVerificationActivateViewController.h"
#import <PDF_Reader_Pro-Swift.h>

@interface KMVerificationActivateViewController ()

@property (nonatomic,assign) IBOutlet NSTextField *label;
@property (nonatomic,assign) IBOutlet NSTextField *sublabel;

@property (nonatomic,assign) IBOutlet NSView *textFieldView;
@property (nonatomic,assign) IBOutlet NSTextField *textField;

@property (nonatomic,assign) IBOutlet NSTextField *textLabel;
@property (nonatomic,assign) IBOutlet NSButton *clickHereButton;

@property (nonatomic,assign) IBOutlet KMCustomButton *backButton;
@property (nonatomic,assign) IBOutlet KMCustomButton *continueButton;
@property (nonatomic, retain) CALayer *continueButtonLayer;

@end

@implementation KMVerificationActivateViewController

#pragma mark Init Methods

- (instancetype)init {
    if (self = [super initWithNibName:@"KMVerificationActivateViewController" bundle:nil]) {
        
    }
    return self;
}

- (void)dealloc {
    
}

#pragma mark View Methods

- (void)loadView {
    [super loadView];
    // Do view setup here.
    self.textFieldView.wantsLayer = YES;
    self.textFieldView.layer.masksToBounds = YES;
    self.textFieldView.layer.cornerRadius = 1.0;
    self.textFieldView.layer.borderWidth = 1.0;
    self.textFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor;
        
    self.backButton.wantsLayer = YES;
    self.backButton.layer.masksToBounds = YES;
    self.backButton.layer.cornerRadius = 1.0;
    
    self.continueButton.wantsLayer = YES;
    self.continueButton.layer.masksToBounds = YES;
    self.continueButton.layer.cornerRadius = 1.0;

    self.label.stringValue = NSLocalizedString(@"Please enter your license", nil);
    self.sublabel.stringValue = NSLocalizedString(@"If you have already purchased PDF Reader Pro for Mac, you should find your license in an email confirmation.", nil);
    self.textLabel.stringValue = NSLocalizedString(@"Can't find your license?", nil);
    
    [self.textField.cell setPlaceholderString:NSLocalizedString(@"License Number", nil)];
    
    NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"Click here", nil)];
    NSRange range = NSMakeRange(0, attributedTitle.length);
    [attributedTitle addAttribute:NSFontAttributeName value:self.clickHereButton.font range:range];
    [attributedTitle addAttribute:NSForegroundColorAttributeName value:[KMAppearance KMColor_Layout_H0] range:range];
    [attributedTitle addAttribute:NSUnderlineStyleAttributeName value:@(YES) range:range];
    self.clickHereButton.attributedTitle = attributedTitle;
    self.backButton.title = NSLocalizedString(@"Cancel", nil);
    self.continueButton.title = NSLocalizedString(@"Continue", nil);
    [self.backButton setTitleColor:[KMAppearance KMColor_Layout_H0]];
    [self.continueButton setTitleColor:[NSColor whiteColor]];
    
    self.continueButtonLayer = [CALayer layer];
    [self.continueButton.layer addSublayer:self.continueButtonLayer];
    self.continueButtonLayer.frame = CGRectMake(0, 0, CGRectGetWidth(_continueButton.bounds), CGRectGetHeight(_continueButton.bounds));
    self.continueButtonLayer.backgroundColor = [KMAppearance KMColor_Status_Sel].CGColor;
    self.continueButtonLayer.cornerRadius = 0;
    self.continueButtonLayer.hidden = YES;
    __block KMVerificationActivateViewController *blockSelf = self;
    self.continueButton.mouseMoveCallback = ^(BOOL mouseEntered) {
        if (mouseEntered)
            blockSelf.continueButtonLayer.hidden = NO;
        else
            blockSelf.continueButtonLayer.hidden = YES;
    };
    self.backButton.mouseMoveCallback = nil;
    
    [self.label setTextColor:[KMAppearance KMColor_Layout_H0]];
    [self.sublabel setTextColor:[KMAppearance KM_242424_Color75]];
    [self.textLabel setTextColor:[KMAppearance KMColor_Layout_H0]];
    
    self.type = KMVerificationTypeActivate;
}

- (void)viewDidAppear {
    [super viewDidAppear];
    [self.textField becomeFirstResponder];
}

- (void)setType:(KMVerificationType)type {
    if (type == KMVerificationTypeActivateAIInfo) {
        self.textLabel.hidden = YES;
        self.clickHereButton.hidden = YES;
        self.label.stringValue = NSLocalizedString(@"Please Enter AI License", nil);
        self.sublabel.stringValue = NSLocalizedString(@"For PDF Reader Pro Permanent/Premium License, please click PDF Reader Pro on the menubar, then tap \"Enter License\".", nil);
        [self.textField.cell setPlaceholderString:NSLocalizedString(@"AI-XXXX-XXXX-XXXX-XXXX", nil)];
    } else {
        self.textLabel.hidden = NO;
        self.clickHereButton.hidden = NO;
        self.label.stringValue = NSLocalizedString(@"Please enter your license", nil);
        self.sublabel.stringValue = NSLocalizedString(@"If you have already purchased PDF Reader Pro for Mac, you should find your license in an email confirmation.", nil);
        [self.textField.cell setPlaceholderString:NSLocalizedString(@"XXXX-XXXX-XXXX-XXXX", nil)];
    }
}

#pragma mark Button Actions

- (IBAction)backButtonAction:(id)sender {
    if (self.callback) {
        self.callback(0, self.textField.stringValue);
    }
}

- (IBAction)continueButtonAction:(id)sender {
    NSString *lineString = self.textField.stringValue;
    lineString = [lineString stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (lineString.length > 0) {
        self.textFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor;
    } else {
        self.textFieldView.layer.borderColor = [NSColor redColor].CGColor;
        NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Activation Error", nil)
                                         defaultButton:NSLocalizedString(@"Try Again", nil)
                                       alternateButton:nil
                                           otherButton:nil
                             informativeTextWithFormat:NSLocalizedString(@"License number can not be empty.", nil), nil];
        [alert runModal];
        return;
    }
    if (self.callback) {
        self.callback(1, lineString);
    }
}

- (IBAction)clickHereButtonAction:(id)sender {
    NSURL *url = [NSURL URLWithString:@"https://www.pdfreaderpro.com/license_retrieval"];
    [[NSWorkspace sharedWorkspace] openURL:url];
}

- (void)setEnabled:(BOOL)enabled {
    self.backButton.enabled = enabled;
    self.continueButton.enabled = enabled;
    self.textField.enabled = enabled;
}

#pragma mark NSTextFieldDelegate

- (void)controlTextDidChange:(NSNotification *)notification {
    self.textFieldView.layer.borderColor = [NSColor lightGrayColor].CGColor;
}

@end