//
//  KMGeneralAnnotationViewController.m
//  SignFlow
//
//  Created by wanjun on 2021/6/15.
//

#import "KMGeneralAnnotationViewController.h"
#import "KMColorPickerViewController.h"
#import "KMPopUpButton.h"
#import "KMAnnotationFontWindowController.h"
#import "KMSelfSignAnnotation.h"
#import "KMSelfSignAnnotationFreeText.h"
#import "CPDFMarkupAnnotation+PDFListView.h"
#import "CPDFLineAnnotation+PDFListView.h"

#import "KMGeneralButton.h"
#import "NSFont_SKExtensions.h"
#import "NSMenu+Category.h"
#import "KMPropertiesViewPopController.h"
#import <PDF_Master-Swift.h>

@interface KMGeneralAnnotationViewController () <NSTextViewDelegate, NSPopoverDelegate, KMSelectPopButtonDelegate>

@property (assign) IBOutlet NSView *generalImageBoxView;
@property (assign) IBOutlet NSImageView *generalImageView;
@property (weak) IBOutlet NSView *mianView;
@property (weak) IBOutlet NSLayoutConstraint *generaImageTopLayoutConstraint;

@property (weak) IBOutlet NSView *inkView;
@property (weak) IBOutlet NSButton *addInkButton;
@property (weak) IBOutlet NSButton *clearInkButton;

@property (assign) IBOutlet NSView *fontView; //字体
@property (assign) IBOutlet NSTextField *fontLabel;
@property (assign) IBOutlet NSButton *fontButton;
@property (assign) IBOutlet NSBox *fontNameBox;
@property (assign) IBOutlet NSBox *fontSizeBox;
@property (assign) IBOutlet NSBox *fontStyleBox;
@property (assign) IBOutlet NSBox *leftAlignBox;
@property (assign) IBOutlet NSBox *centerAlignBox;
@property (assign) IBOutlet NSBox *rightAlignBox;
@property (assign) IBOutlet NSBox *fontBox;
@property (assign) IBOutlet KMGeneralButton *fontColorButton;
@property (assign) IBOutlet NSButton *fontCustomColorButton;
@property (assign) IBOutlet NSLayoutConstraint *fontViewTopConstraint;

@property (weak) IBOutlet NSLayoutConstraint *fontDefaultTopConstraint;
@property (weak) IBOutlet NSButton *defaultMoreButton;
@property (weak) IBOutlet KMPopUpButton *defaultPopUpButton;
@property (weak) IBOutlet NSView *defaultFontView;

@property (assign) IBOutlet NSView *fillColorView; //填充颜色
@property (assign) IBOutlet NSTextField *fillColorLabel;
@property (assign) IBOutlet NSBox *fillColorBox;
@property (assign) IBOutlet KMColorPickerViewController *fillColorPickerVC;
@property (assign) IBOutlet NSLayoutConstraint *fillColorViewTopConstraint;
@property (weak) IBOutlet NSButton *fillButton;

//填充
@property (assign) IBOutlet NSView *opacityView; //透明度
@property (assign) IBOutlet NSSlider *opacitySlider;
@property (assign) IBOutlet NSComboBox *opacityComboBox;
@property (weak) IBOutlet NSButton *showOpacityButton;

@property (assign) IBOutlet NSView *borderColorView; //边框颜色
@property (assign) IBOutlet NSTextField *borderColorLabel;
@property (assign) IBOutlet NSBox *borderColorBox;
@property (assign) IBOutlet KMColorPickerViewController *borderColorPickerVC;
@property (assign) IBOutlet NSLayoutConstraint *borderColorViewTopConstraint;

@property (assign) IBOutlet NSView *borderOpacityView; //透明度
@property (assign) IBOutlet NSSlider *borderOpacitySlider;
@property (assign) IBOutlet NSComboBox *borderOpacityComboBox;
@property (weak) IBOutlet NSButton *showborderOpacityButton;

@property (assign) IBOutlet NSView *borderWidthView; //线段宽度
@property (assign) IBOutlet NSTextField *borderWidthLabel;
@property (assign) IBOutlet NSSlider *borderWidthSlider;
@property (assign) IBOutlet NSBox *borderWidthBox;
@property (assign) IBOutlet NSLayoutConstraint *borderWidthViewTopConstraint;

@property (strong) IBOutlet NSView *boderWidthStyleView;
@property (weak) IBOutlet NSButton *borderWidthStyleReal;
@property (weak) IBOutlet NSButton *borderWidthStyleDotted;
@property (assign) IBOutlet NSLayoutConstraint *boderWidthStyleViewTopConstraint;

@property (assign) IBOutlet NSView *rotateView; //旋转
@property (assign) IBOutlet NSTextField *rotateLabel;
@property (assign) IBOutlet NSBox *leftRotateBox;
@property (assign) IBOutlet NSBox *rightRotateBox;
@property (assign) IBOutlet NSLayoutConstraint *rotateViewTopConstraint;

@property (assign) IBOutlet NSView *noteView; //笔记
@property (assign) IBOutlet NSTextField *noteViewLabel;
@property (assign) IBOutlet NSBox *noteTextBox;
@property (assign) IBOutlet NSTextView *noteTextView;
@property (assign) IBOutlet NSLayoutConstraint *noteViewTopConstraint;

@property (assign) IBOutlet NSView *backgroundView;
@property (assign) IBOutlet NSTextField *backgroundLabel;
@property (assign) IBOutlet NSBox *backgroundBox;
@property (assign) IBOutlet KMColorPickerViewController *backgroundColorPickerVC;
@property (assign) IBOutlet NSLayoutConstraint *backgroundTopConstraint;

@property (weak) IBOutlet NSLayoutConstraint *figureTopConstraint;
@property (weak) IBOutlet NSView *figureView;
@property (weak) IBOutlet NSBox *squareBox;
@property (weak) IBOutlet NSBox *circleBox;
@property (weak) IBOutlet NSBox *arrowBox;
@property (weak) IBOutlet NSBox *lineBox;

@property (assign) IBOutlet NSView *typeView;
@property (assign) IBOutlet NSTextField *typeLabel;
@property (assign) IBOutlet NSButton *tureTypeButton;
@property (assign) IBOutlet NSButton *falseTypeButton;
@property (assign) IBOutlet NSButton *circletypeButton;
@property (assign) IBOutlet NSButton *lineTypeButton;
@property (assign) IBOutlet NSButton *dotTypeButton;
@property (assign) IBOutlet NSLayoutConstraint *typeTopConstraint;

@property (assign) IBOutlet NSView *dateView;
@property (assign) IBOutlet NSTextField *dateLabel;
@property (assign) IBOutlet KMPopUpButton *datePopupButton;
@property (assign) IBOutlet NSButton *dateButton;
@property (assign) IBOutlet NSLayoutConstraint *dateTopConstraint;

@property (weak) IBOutlet NSView *noteStyleView;
@property (weak) IBOutlet NSTextField *noteStyleTextField;
@property (weak) IBOutlet NSLayoutConstraint *noteStyleTopConstraint;
@property (weak) IBOutlet NSButton *noteStyleNoneButton;
@property (weak) IBOutlet NSButton *noteStyleButton1;
@property (weak) IBOutlet NSButton *noteStyleButton2;
@property (weak) IBOutlet NSButton *noteStyleButton3;
@property (weak) IBOutlet NSButton *noteStyleButton4;
@property (weak) IBOutlet NSButton *noteStyleButton5;
@property (weak) IBOutlet NSButton *noteStyleButton6;


@property (nonatomic, assign) CAnnotationType selfSignType;

@property (nonatomic, retain) NSMutableArray*fonts;

@property (nonatomic, assign) CAnnotationType annotationType;

@property (nonatomic, retain) KMGeneralAnnotationViewController * annotationViewController;

@property (nonatomic, retain) NSPopover *fillColorPopover;
@property (nonatomic, retain) NSPopover *borderColorPopover;

@property (nonatomic, retain) KMDesignPropertySelector *leftAlignVC;
@property (nonatomic, retain) KMDesignPropertySelector *centerAlignVC;
@property (nonatomic, retain) KMDesignPropertySelector *rightAlignVC;
@property (nonatomic, retain) KMDesignPropertySelector *leftRotateVC;
@property (nonatomic, retain) KMDesignPropertySelector *rightRotateVC;
@property (nonatomic, retain) KMDesignSelect *fontNameVC;
@property (nonatomic, retain) KMDesignSelect *fontSizeVC;
@property (nonatomic, retain) KMDesignSelect *fontStyleVC;
@property (nonatomic, retain) KMDesignSelect *borderWidthVC;
@property (nonatomic, retain) KMDesignPropertySelector *squareVC;
@property (nonatomic, retain) KMDesignPropertySelector *circleVC;
@property (nonatomic, retain) KMDesignPropertySelector *arrowVC;
@property (nonatomic, retain) KMDesignPropertySelector *lineVC;

@end

@implementation KMGeneralAnnotationViewController

#pragma mark - init Method

- (void)dealloc {
    if (_annotationModel.annotation &&
        _annotationModel.annotations.count == 1) {
        NSArray *keys = @[@"contents",@"markupText"];
        for (NSString *key in keys) {
            [_annotationModel.annotation removeObserver:self forKeyPath:key];
        }
    }
    [[NSColorPanel sharedColorPanel] setTarget:nil];
    [[NSColorPanel sharedColorPanel] setAction:nil];
    [[NSColorPanel sharedColorPanel] orderOut:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)viewDidAppear
{
    [super viewDidAppear];
    
    BOOL showConvertDetails = [KMPropertiesViewPopController showChangeColorDetails];
    if (showConvertDetails && [(NSDocument *)self.view.window.windowController.document fileURL]) {
        [[KMPropertiesViewPopController defaultManager] showChangeColorDetailsView:self.fillColorPickerVC.firstBox];
    }
}

#pragma mark - View Methods

- (void)loadView {
    [super loadView];

    self.fonts = [NSFont supportFonts];
    [self loadingUIAndLocalization];
        
    if (_annotationModel.annotation &&
        _annotationModel.annotations.count == 1) {
        CPDFAnnotation *annotation = _annotationModel.annotation;
        NSArray *keys = @[@"contents",@"markupText"];

        for (NSString *key in keys) {
            [annotation addObserver:self forKeyPath:key options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
        }
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alignmentTypeNotification:) name:@"KMAnnotationAlignmentTypeNotification1" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadingUIAndLocalization) name:@"KMPreferenceDidChangeNotificationName" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(annotationChangeNotification:) name:CPDFListViewAnnotationsAttributeHasChangeNotification object:nil];
}

- (void)loadingUIAndLocalization
{
    self.view.wantsLayer = YES;
    self.view.layer.backgroundColor = [NSColor colorWithRed:247.0/255.0 green:248.0/255.0 blue:250.0/255.0 alpha:1].CGColor;
    
    self.annotationType = self.annotationModel.annotationType;

    _generalImageBoxView.wantsLayer = YES;
    _generalImageBoxView.layer.borderColor = [NSColor colorWithRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1].CGColor;
    _generalImageBoxView.layer.borderWidth = 1.0f;
    _generalImageBoxView.layer.cornerRadius = 4;
        
    NSColor *color = [NSColor whiteColor];
    if (CAnnotationTypeFreeText == _annotationType ||
        CAnnotationTypeSignDot == _annotationType ||
        CAnnotationTypeSignCircle == _annotationType ||
        CAnnotationTypeSignLine == _annotationType ||
        CAnnotationTypeSignTure == _annotationType ||
        CAnnotationTypeSignFalse == _annotationType) {
        if (@available(macOS 10.14, *)) {
            NSAppearanceName appearanceName = [[NSApp effectiveAppearance] bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]];
            if ([appearanceName isEqualToString:NSAppearanceNameDarkAqua]) {
                color = [NSColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.15];
            }
        }
    }
    
    _generalImageBoxView.layer.backgroundColor = color.CGColor;
    _generalImageBoxView.layer.cornerRadius = 6.0f;
    
    _fontLabel.stringValue = NSLocalizedString(@"Fonts", nil);
    _fillColorLabel.stringValue = NSLocalizedString(@"Fill", nil);
    _borderColorLabel.stringValue = NSLocalizedString(@"Border", nil);
    _borderWidthLabel.stringValue = NSLocalizedString(@"Line", nil);
    _rotateLabel.stringValue = NSLocalizedString(@"Rotate", nil);
    _noteViewLabel.stringValue = NSLocalizedString(@"Note", nil);
    _typeLabel.stringValue = NSLocalizedString(@"Type", nil);
    _dateLabel.stringValue = NSLocalizedString(@"Date", nil);
    _backgroundLabel.stringValue = NSLocalizedString(@"Background Color", nil);
    _dateButton.title = NSLocalizedString(@"Time", nil);
    _noteStyleTextField.stringValue = NSLocalizedString(@"Style", nil);
    
    _fontLabel.textColor =
    _fillColorLabel.textColor =
    _borderColorLabel.textColor =
    _borderWidthLabel.textColor =
    _rotateLabel.textColor =
    _noteViewLabel.textColor =
    _dateLabel.textColor =
    _typeLabel.textColor =
    _noteStyleTextField.textColor =
    _backgroundLabel.textColor = [NSColor colorWithRed:97.0/255.0 green:100.0/255.0 blue:105.0/255.0 alpha:1];
    
    _fillColorBox.contentView = _fillColorPickerVC.view;
    _borderColorBox.contentView = _borderColorPickerVC.view;
    
    _noteTextView.backgroundColor = [NSColor whiteColor];
    _opacityComboBox.wantsLayer =
    _borderOpacityComboBox.wantsLayer =
    _falseTypeButton.wantsLayer =
    _tureTypeButton.wantsLayer =
    _circletypeButton.wantsLayer =
    _lineTypeButton.wantsLayer =
    _defaultPopUpButton.wantsLayer =
    _dotTypeButton.wantsLayer = YES;
    _defaultPopUpButton.layer.backgroundColor =
    _opacityComboBox.layer.backgroundColor =
    _borderOpacityComboBox.layer.backgroundColor = [NSColor clearColor].CGColor;
    _fontBox.borderColor = [NSColor colorWithRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1];
    _fontBox.fillColor = [NSColor clearColor];
    
    _opacityComboBox.layer.borderWidth =
    _noteTextBox.borderWidth = 0.5f;
    
    _opacityComboBox.layer.borderColor = [NSColor grayColor].CGColor;
    _noteTextBox.borderColor = [NSColor grayColor];
    
    _leftRotateBox.borderColor =
    _rightRotateBox.borderColor = [NSColor grayColor];
    
    _opacityComboBox.layer.cornerRadius =
    _falseTypeButton.layer.cornerRadius =
    _tureTypeButton.layer.cornerRadius =
    _circletypeButton.layer.cornerRadius =
    _lineTypeButton.layer.cornerRadius =
    _dotTypeButton.layer.cornerRadius= 5.0f;
    
    _leftAlignVC = [[KMDesignPropertySelector alloc] initWithType:PropertySelectorTypeIcon_Btn];
    _centerAlignVC = [[KMDesignPropertySelector alloc] initWithType:PropertySelectorTypeIcon_Btn];
    _rightAlignVC = [[KMDesignPropertySelector alloc] initWithType:PropertySelectorTypeIcon_Btn];
    _leftAlignBox.fillColor = [NSColor clearColor];
    _leftAlignBox.contentView = _leftAlignVC.view;
    _leftAlignVC.target = self;
    _leftAlignVC.action = @selector(leftAlignButtonAction:);
    _leftAlignVC.image = [NSImage imageNamed:@"icon_propertybar_fontAlign_left"];
    _centerAlignBox.fillColor = [NSColor clearColor];
    _centerAlignBox.contentView = _centerAlignVC.view;
    _centerAlignVC.target = self;
    _centerAlignVC.action = @selector(centerAlignButtonAction:);
    _centerAlignVC.image = [NSImage imageNamed:@"icon_propertybar_fontAlign_center"];
    _rightAlignBox.fillColor = [NSColor clearColor];
    _rightAlignBox.contentView = _rightAlignVC.view;
    _rightAlignVC.target = self;
    _rightAlignVC.action = @selector(rightAlignButtonAction:);
    _rightAlignVC.image = [NSImage imageNamed:@"icon_propertybar_fontAlign_right"];
    
    _leftRotateVC = [[KMDesignPropertySelector alloc] initWithType:PropertySelectorTypeIcon_Btn];
    _rightRotateVC = [[KMDesignPropertySelector alloc] initWithType:PropertySelectorTypeIcon_Btn];
    _leftRotateBox.fillColor = [NSColor clearColor];
    _leftRotateBox.contentView = _leftRotateVC.view;
    _rightAlignBox.fillColor = [NSColor clearColor];
    _rightAlignBox.contentView = _rightAlignVC.view;
    
    _fontNameVC = [[KMDesignSelect alloc] initWithType:SelectTypePopButton];
    _fontStyleVC = [[KMDesignSelect alloc] initWithType:SelectTypePopButton];
    _fontSizeVC = [[KMDesignSelect alloc] initWithType:SelectTypePopButton];
    NSMutableArray *fontNames = [NSMutableArray array];
    for (NSDictionary *font in [CPDFAnnotationModel supportFonts]) {
        NSString *fontName = font.allKeys.firstObject;
        [fontNames addObject:fontName];
    }
    _fontNameBox.fillColor = [NSColor clearColor];
    _fontNameBox.contentView = _fontNameVC.view;
    [_fontNameVC removeAllItems];
    [_fontNameVC addItemsWithObjectValues:fontNames];
    [_fontNameVC selectItemAt:0];
    _fontNameVC.delete = self;
    
    NSArray *fontnames = [_annotationModel.fontName componentsSeparatedByString:@"-"];
    NSFont *currentFont = [NSFont systemFontOfSize:12.0];
    if ([fontnames.lastObject isEqualToString:@"Regular"]) {
        currentFont = [NSFont fontWithName:fontnames.firstObject size:_annotationModel.fontSize];
    } else {
        currentFont = [NSFont fontWithName:_annotationModel.fontName size:_annotationModel.fontSize];
    }
    _fontStyleBox.fillColor = [NSColor clearColor];
    _fontStyleBox.contentView = _fontStyleVC.view;
    NSArray *styles = [currentFont styles];
    [_fontStyleVC addItemsWithObjectValues:styles];
    [_fontStyleVC selectItemAt:0];
    _fontStyleVC.delete = self;

    _fontSizeBox.fillColor = [NSColor clearColor];
    _fontSizeBox.contentView = _fontSizeVC.view;
    [_fontSizeVC removeAllItems];
    [_fontSizeVC addItemsWithObjectValues:@[@"8pt", @"9pt", @"10pt", @"11pt", @"12pt", @"14pt", @"16pt", @"18pt", @"20pt", @"22pt", @"24pt", @"26pt", @"28pt", @"36pt", @"48pt", @"72pt"]];
    [_fontSizeVC selectItemAt:0.0];
    _fontSizeVC.delete = self;
    
    _borderWidthVC = [[KMDesignSelect alloc] initWithType:SelectTypePopButton];
    _borderWidthBox.fillColor = [NSColor clearColor];
    _borderWidthBox.contentView = _borderWidthVC.view;
    [_borderWidthVC removeAllItems];
    if(_annotationModel.annotationType == CAnnotationTypeEraser) {
        self.borderWidthSlider.minValue = 5;
        [_borderWidthVC addItemsWithObjectValues:@[@"5pt", @"6pt", @"8pt", @"10pt", @"12pt"]];
    } else {
        [_borderWidthVC addItemsWithObjectValues:@[@"1pt", @"2pt", @"4pt", @"6pt", @"8pt"]];
    }
    [_borderWidthVC selectItemAt:0.0];
    _borderWidthVC.delete = self;
    
    _squareVC = [[KMDesignPropertySelector alloc] initWithType:PropertySelectorTypeIcon_Btn];
    _circleVC = [[KMDesignPropertySelector alloc] initWithType:PropertySelectorTypeIcon_Btn];
    _arrowVC = [[KMDesignPropertySelector alloc] initWithType:PropertySelectorTypeIcon_Btn];
    _lineVC = [[KMDesignPropertySelector alloc] initWithType:PropertySelectorTypeIcon_Btn];
    _squareBox.fillColor = [NSColor clearColor];
    _squareBox.contentView = _squareVC.view;
    _squareVC.target = self;
    _squareVC.action = @selector(updateFigurateModelAction:);
    _squareVC.image = [NSImage imageNamed:@"KMImageNameMainToolSquare"];
    _squareVC.image_sel = [NSImage imageNamed:@"KMImageNameMainToolSquareSelect"];
    _circleBox.fillColor = [NSColor clearColor];
    _circleBox.contentView = _circleVC.view;
    _circleVC.target = self;
    _circleVC.action = @selector(updateFigurateModelAction:);
    _circleVC.image = [NSImage imageNamed:@"KMImageNameMainToolRectangle"];
    _circleVC.image_sel = [NSImage imageNamed:@"KMImageNameMainToolRectangle"];
    _arrowBox.fillColor = [NSColor clearColor];
    _arrowBox.contentView = _arrowVC.view;
    _arrowVC.target = self;
    _arrowVC.action = @selector(updateFigurateModelAction:);
    _arrowVC.image = [NSImage imageNamed:@"KMImageNameMainToolShapes"];
    _arrowVC.image_sel = [NSImage imageNamed:@"KMImageNameMainToolShapesSelect"];
    _lineBox.fillColor = [NSColor clearColor];
    _lineBox.contentView = _lineVC.view;
    _lineVC.target = self;
    _lineVC.action = @selector(updateFigurateModelAction:);
    _lineVC.image = [NSImage imageNamed:@"KMImageNameMainToolStraightLine"];
    _lineVC.image_sel = [NSImage imageNamed:@"KMImageNameMainToolStraightLineSelect"];

    [self configureUIView];
        
    if (CAnnotationTypeFreeText == _annotationType ||
        CAnnotationTypeSignText == _annotationType ||
        CAnnotationTypeSignDate == _annotationType ) {
        self.fillColorPickerVC.isFreeText = YES;
    } else {
        self.fillColorPickerVC.isFreeText = NO;
    }
    
    _defaultPopUpButton.type = KMPopUpButtonType_ArrowDown;
    _datePopupButton.type = KMPopUpButtonType_ArrowUpDown;
    
    self.generalImageView.image = [self.annotationModel annotationImage];
    if (CAnnotationTypeSignFalse != _annotationType &&
        CAnnotationTypeSignTure != _annotationType &&
        CAnnotationTypeSignDot != _annotationType &&
        CAnnotationTypeSignLine != _annotationType &&
        CAnnotationTypeSignCircle != _annotationType &&
        CAnnotationTypeSignDate != _annotationType) {
        if (CAnnotationTypeStamp == _annotationType ||
            CAnnotationTypeInk == _annotationType ||
            CAnnotationTypeSquare == _annotationType ||
            CAnnotationTypeCircle == _annotationType ||
            CAnnotationTypeArrow == _annotationType ||
            CAnnotationTypeLine == _annotationType ||
            CAnnotationTypeHighlight == _annotationType ||
            CAnnotationTypeUnderline == _annotationType ||
            CAnnotationTypeFreeText == _annotationType ||
            CAnnotationTypeStrikeOut == _annotationType) {
            self.noteView.hidden = YES;
        } else {
            self.noteView.hidden = self.annotationModel.annotation==nil;
        }
    }
    //全部便签都隐藏
    self.noteView.hidden = YES;
  
    if (self.annotationModel.annotations.count > 1) {
        self.noteView.hidden = YES;
    }
    self.noteTextView.wantsLayer = YES;

    if (self.annotationModel.annotation) {
        CPDFAnnotation *annotation = self.annotationModel.annotation;
        
        NSString *contextString = annotation.contents ? : @"";
        if ([annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
            contextString = [(CPDFMarkupAnnotation *)annotation markupContent];
        }
        self.noteTextView.string = contextString?:@"";
        self.noteTextView.textColor = [NSColor labelColor];
        self.noteTextView.delegate = self;
    }
    self.opacityView.wantsLayer = YES;
    self.opacityView.layer.backgroundColor = [NSColor clearColor].CGColor;
    self.opacityView.layer.shadowColor = [NSColor colorWithRed:0 green:0 blue:0 alpha:0.16].CGColor;
    self.opacityView.layer.shadowOffset = NSMakeSize(0, 2);
    self.opacityView.layer.cornerRadius = 5;
    
    self.borderOpacityView.wantsLayer = YES;
    self.borderOpacityView.layer.backgroundColor = [NSColor clearColor].CGColor;
    self.borderOpacityView.layer.shadowColor = [NSColor colorWithRed:0 green:0 blue:0 alpha:0.16].CGColor;
    self.borderOpacityView.layer.shadowOffset = NSMakeSize(0, 2);
    self.borderOpacityView.layer.cornerRadius = 5;
}

#pragma mark - private

- (void)hiddenSubviews {
    if(self.generalImageBoxView.hidden) {
        self.generaImageTopLayoutConstraint.constant = -(self.generalImageBoxView.bounds.size.height) - (self.inkView.bounds.size.height);
    } else {
        self.generaImageTopLayoutConstraint.constant = self.inkView.hidden ?  -(self.inkView.bounds.size.height) : 16.0f;
    }
    
    self.fontDefaultTopConstraint.constant = self.defaultFontView.hidden ? -(self.defaultFontView.bounds.size.height) : 16.0f;
    self.figureTopConstraint.constant = self.figureView.hidden ? -(self.figureView.bounds.size.height) : 8.0f;
    self.fontViewTopConstraint.constant = self.fontView.hidden ? -(self.fontView.bounds.size.height) : 8.0f;
    self.fillColorViewTopConstraint.constant = self.fillColorView.hidden ? -(self.fillColorView.bounds.size.height) : 8.0f;
    self.borderColorViewTopConstraint.constant = self.borderColorView.hidden ? -(self.borderColorView.bounds.size.height) : 8.0f;
    self.borderWidthViewTopConstraint.constant = self.borderWidthView.hidden ? -(self.borderWidthView.bounds.size.height) : 0.0f;
    self.boderWidthStyleViewTopConstraint.constant = self.boderWidthStyleView.hidden ? -(self.boderWidthStyleView.bounds.size.height) : 16.0f;
    self.rotateViewTopConstraint.constant = self.rotateView.hidden ? -(self.rotateView.bounds.size.height) : 16.0f;
    self.noteViewTopConstraint.constant = self.noteStyleView.hidden ? -(self.rotateView.bounds.size.height) : 16.0f;
    self.noteViewTopConstraint.constant = self.noteView.hidden ? -(self.noteView.bounds.size.height) : 16.0f;
    self.backgroundTopConstraint.constant = self.backgroundView.hidden ? -(self.backgroundView.bounds.size.height) : 16.0f;
    self.typeTopConstraint.constant = self.typeView.hidden ? -(self.typeView.bounds.size.height) : 16.0f;
    self.dateTopConstraint.constant = self.dateView.hidden ? -(self.dateView.bounds.size.height) : 16.0f;
    self.noteStyleTopConstraint.constant = self.noteStyleView.hidden ? -(self.noteStyleView.bounds.size.height) : 8.0f;
}

- (void)configureUIView
{
    [self updateOpacityView];
    if (!self.annotationModel.annotation) {
        self.noteView.hidden = YES;
    }
    
    if (_annotationType == CAnnotationTypeInk || _annotationType == CAnnotationTypeEraser) {
        if (self.annotationModel.annotations.count > 0) {
            self.inkView.hidden = YES;
            [self updateInkAction:self.addInkButton];
        } else {
            self.inkView.hidden = NO;
            if (_annotationType == CAnnotationTypeInk) {
                [self updateInkAction:self.addInkButton];
            } else {
                [self updateInkAction:self.clearInkButton];
            }
        }
    } else {
        self.inkView.hidden = YES;
    }
    
    if (self.annotationModel.annotations.count > 1) {
        self.generalImageBoxView.hidden = YES;
    } else {
        self.generalImageBoxView.hidden = NO;
    }
    
    self.noteStyleView.hidden = YES;
    self.borderOpacityView.hidden = YES;

    switch (_annotationType) {
        case CAnnotationTypeHighlight:
        case CAnnotationTypeUnderline:
        case CAnnotationTypeStrikeOut:
        {
            self.fontView.hidden = YES;
            self.borderColorView.hidden = YES;
            self.borderWidthView.hidden = YES;
            self.boderWidthStyleView.hidden = YES;
            self.rotateView.hidden = YES;
            self.backgroundView.hidden = YES;
            self.typeView.hidden = YES;
            self.dateView.hidden = YES;
            self.defaultFontView.hidden = YES;
            self.figureView.hidden = YES;
            self.noteView.hidden = YES;
            
            [self createMarkupProperties];
            [self updateFillColorPickerVC];
        }
            break;
        case CAnnotationTypeInk:
        case CAnnotationTypeLine:
        case CAnnotationTypeArrow:
        {
            self.fontView.hidden = YES;
            self.borderColorView.hidden = YES;
            self.rotateView.hidden = YES;
            self.backgroundView.hidden = YES;
            self.typeView.hidden = YES;
            self.dateView.hidden = YES;
            self.fillColorView.hidden = NO;
            self.boderWidthStyleView.hidden = NO;
            self.defaultFontView.hidden = YES;
            self.noteView.hidden = YES;
            if(_annotationType != CAnnotationTypeInk && self.pdfView.annotationType != CAnnotationTypeUnkown) {
                self.figureView.hidden = NO;
                [self updateFigureModel];
            } else {
                self.figureView.hidden = YES;
            }
            if (_annotationType == CAnnotationTypeLine || _annotationType == CAnnotationTypeArrow) {
                _fillColorLabel.stringValue = NSLocalizedString(@"Border Style", nil);
            }
            [self createInkAndLineProperties];
            [self updateBorderWidthView];
            [self updateFillColorPickerVC];
        }
            break;
        case CAnnotationTypeEraser:
        {
            self.fontView.hidden = YES;
            self.borderColorView.hidden = YES;
            self.rotateView.hidden = YES;
            self.backgroundView.hidden = YES;
            self.typeView.hidden = YES;
            self.dateView.hidden = YES;
            self.fillColorView.hidden = YES;
            self.boderWidthStyleView.hidden = YES;
            self.defaultFontView.hidden = YES;
            self.figureView.hidden = YES;
            [self hiddenSubviews];
            [self updateBorderWidthView];
        }
            break;
        case CAnnotationTypeAnchored:
        {
            self.fontView.hidden = YES;
            self.borderColorView.hidden = YES;
            self.borderWidthView.hidden = YES;
            self.boderWidthStyleView.hidden = YES;
            self.rotateView.hidden = YES;
            self.backgroundView.hidden = YES;
            self.typeView.hidden = YES;
            self.dateView.hidden = YES;
            self.defaultFontView.hidden = YES;
            self.noteStyleView.hidden = NO;
            self.figureView.hidden = YES;
            
            _fillColorLabel.stringValue = NSLocalizedString(@"Color", nil);
            [self createTextProperties];
            [self updateFillColorPickerVC];
        }
            break;
        case CAnnotationTypeCircle:
        case CAnnotationTypeSquare:
        {
            self.fontView.hidden = YES;
            self.rotateView.hidden = YES;
            self.backgroundView.hidden = YES;
            self.typeView.hidden = YES;
            self.dateView.hidden = YES;
            self.defaultFontView.hidden = YES;
            if(self.pdfView.annotationType != CAnnotationTypeUnkown) {
                self.figureView.hidden = NO;
                [self updateFigureModel];
            } else {
                self.figureView.hidden = YES;
            }
            if (_annotationType == CAnnotationTypeCircle) {
                _borderColorLabel.stringValue = NSLocalizedString(@"Border Style", nil);
            }
            self.borderColorView.hidden = NO;
            self.borderOpacityView.hidden = NO;
            
            if ((_annotationType == CAnnotationTypeCircle || _annotationType == CAnnotationTypeSquare) && _annotationModel.annotations.count > 1) {
                BOOL otherType = NO;
                for (CPDFAnnotation *annotation in _annotationModel.annotations) {
                    if ([annotation.type isEqualToString:@"Line"] || [annotation.type isEqualToString:@"Arrow"]) {
                        otherType = YES;
                    }
                }
                if (otherType) {
                    self.fillColorView.hidden = YES;
                }
            }
            
            [self createSquareAndCircleProperties];
            [self updateBorderOpacityView];
            [self updateBorderWidthView];
            [self updateFillColorPickerVC];
            [self updateBorderColorPickerVC];
            [self updateFigureModel];
        }
            break;
        case CAnnotationTypeFreeText:
        case CAnnotationTypeSignText:
        {
            self.borderColorView.hidden = YES;
            self.borderWidthView.hidden = YES;
            self.boderWidthStyleView.hidden = YES;
            self.rotateView.hidden = YES;
            self.backgroundView.hidden = YES;
            self.typeView.hidden = YES;
            self.dateView.hidden = YES;
            self.figureView.hidden = YES;
            
            _fillColorLabel.stringValue = NSLocalizedString(@"Fill", nil);
            [self createFreeTextProperties];
            [self createDefultFontPropert];
            [self updateFillColorPickerVC];
            [self updateFontView];
        }
            break;
        case CAnnotationTypeStamp:
        {
            self.fontView.hidden = YES;
            self.fillColorView.hidden = YES;
            self.borderColorView.hidden = YES;
            self.borderWidthView.hidden = YES;
            self.boderWidthStyleView.hidden = YES;
            self.backgroundView.hidden = YES;
            self.rotateView.hidden = YES;
            self.typeView.hidden = YES;
            self.dateView.hidden = YES;
            self.opacityView.hidden = YES;
            self.noteView.hidden = YES;
            self.defaultFontView.hidden = YES;
            self.figureView.hidden = YES;
            
            [self createStampProperties];
        }
            break;
        case CAnnotationTypeSignDate:
            self.fillColorView.hidden = YES;
            self.borderColorView.hidden = YES;
            self.borderWidthView.hidden = YES;
            self.boderWidthStyleView.hidden = YES;
            self.typeView.hidden = YES;
            self.rotateView.hidden = YES;
            self.noteView.hidden = YES;
            self.defaultFontView.hidden = YES;
            self.figureView.hidden = YES;
            
            [self createSelfSignFreeTextProperties];
            [self updateBackgroundColorPickerVC];
            [self updateFontView];
            [self updateDateView];
            break;
        case CAnnotationTypeSignLine:
        case CAnnotationTypeSignDot:
        case CAnnotationTypeSignCircle:
        case CAnnotationTypeSignTure:
        case CAnnotationTypeSignFalse:
            self.fontView.hidden = YES;
            self.borderColorView.hidden = YES;
            self.backgroundView.hidden = YES;
            self.rotateView.hidden = YES;
            self.dateView.hidden = YES;
            self.noteView.hidden = YES;
            self.defaultFontView.hidden = YES;
            self.figureView.hidden = YES;
            if (_pdfView.annotationType == CAnnotationTypeSignDot) {
                self.borderWidthView.hidden = YES;
                self.boderWidthStyleView.hidden = YES;
            }
            
            self.typeView.hidden = self.annotationModel.annotation == nil;
            [self createSelfSignProperties];
            break;
        default:
            break;
    }
}

- (void)alignmentTypeSelected:(NSTextAlignment)type {
    self.leftAlignVC.state = KMDesignTokenStateNorm;
    self.centerAlignVC.state = KMDesignTokenStateNorm;
    self.rightAlignVC.state = KMDesignTokenStateNorm;
    if (type == NSTextAlignmentLeft) {
        self.leftAlignVC.state = KMDesignTokenStateSel;
    } else if (type == NSTextAlignmentCenter) {
        self.centerAlignVC.state = KMDesignTokenStateSel;
    } else if (type == NSTextAlignmentRight) {
        self.rightAlignVC.state = KMDesignTokenStateSel;
    }
}

- (void)updateOpacityView
{
    CGFloat opacity = self.annotationModel.opacity;
    if(self.annotationModel.annotations.count > 1) {
        opacity = self.annotationModel.annotation.opacity;
    }
    if (CAnnotationTypeCircle == _annotationType ||
        CAnnotationTypeSquare == _annotationType) {
        opacity = self.annotationModel.interiorOpacity;
    } else if (CAnnotationTypeFreeText == _annotationType ||
               CAnnotationTypeSignDate == _annotationType ||
               CAnnotationTypeSignText == _annotationType ) {
        opacity = self.annotationModel.interiorOpacity;
    }
    self.opacitySlider.floatValue = opacity;
    self.opacitySlider.toolTip = [NSString stringWithFormat:@"%@%%",@((int)(opacity*100))];
    self.opacityComboBox.stringValue = [NSString stringWithFormat:@"%@%%",@((int)(opacity*100))];
    
    self.generalImageView.image = [self.annotationModel annotationImage];
}

//边框颜色
- (void)updateBorderOpacityView {
    CGFloat opacity = self.annotationModel.opacity;
    if(self.annotationModel.annotations.count > 1) {
        opacity = self.annotationModel.annotation.opacity;
    }
    self.borderOpacitySlider.floatValue = opacity;
    self.borderOpacitySlider.toolTip = [NSString stringWithFormat:@"%@%%",@((int)(opacity*100))];
    self.borderOpacityComboBox.stringValue = [NSString stringWithFormat:@"%@%%",@((int)(opacity*100))];
    
    self.generalImageView.image = [self.annotationModel annotationImage];
}

- (void)updateBorderWidthView
{
    CGFloat lineWidth = self.annotationModel.lineWidth;
    CPDFBorderStyle stype = self.annotationModel.style;
    if(self.annotationModel.annotations.count > 1) {
        lineWidth = self.annotationModel.annotation.borderWidth;
        stype = self.annotationModel.annotation.border.style;
    }
    self.borderWidthSlider.floatValue = lineWidth;
    self.borderWidthSlider.toolTip = [NSString stringWithFormat:@"%0.1f pt",lineWidth];
    self.borderWidthVC.stringValue = [NSString stringWithFormat:@"%0.1f pt",lineWidth];
    
    self.generalImageView.image = [self.annotationModel annotationImage];
    
    if (self.annotationModel.annotationType == CAnnotationTypeEraser)  return;
    if (stype == CPDFBorderStyleDashed) {
        self.borderWidthStyleDotted.wantsLayer = YES;
        self.borderWidthStyleDotted.layer.backgroundColor = [NSColor colorWithRed:206.0/255.0 green:208.0/255.0 blue:212.0/255.0 alpha:0.6].CGColor;
        self.borderWidthStyleDotted.layer.borderWidth = 1.0;
        self.borderWidthStyleDotted.layer.cornerRadius = 4.0;
        self.borderWidthStyleDotted.layer.borderColor = [NSColor colorWithRed:206.0/255.0 green:208.0/255.0 blue:212.0/255.0 alpha:1].CGColor;
        self.borderWidthStyleReal.wantsLayer = YES;
        self.borderWidthStyleReal.layer.backgroundColor = [NSColor clearColor].CGColor;
        self.borderWidthStyleReal.layer.borderWidth = 0;
        self.borderWidthStyleReal.layer.cornerRadius = 0;
        self.borderWidthStyleReal.layer.borderColor = [NSColor clearColor].CGColor;
    } else if (stype == CPDFBorderStyleSolid) {
        self.borderWidthStyleReal.wantsLayer = YES;
        self.borderWidthStyleReal.layer.backgroundColor = [NSColor colorWithRed:206.0/255.0 green:208.0/255.0 blue:212.0/255.0 alpha:0.6].CGColor;
        self.borderWidthStyleReal.layer.borderWidth = 1.0;
        self.borderWidthStyleReal.layer.cornerRadius = 4.0;
        self.borderWidthStyleReal.layer.borderColor = [NSColor colorWithRed:206.0/255.0 green:208.0/255.0 blue:212.0/255.0 alpha:1].CGColor;
        self.borderWidthStyleDotted.wantsLayer = YES;
        self.borderWidthStyleDotted.layer.backgroundColor = [NSColor clearColor].CGColor;
        self.borderWidthStyleDotted.layer.borderWidth = 0;
        self.borderWidthStyleDotted.layer.cornerRadius = 0;
        self.borderWidthStyleDotted.layer.borderColor = [NSColor clearColor].CGColor;
    }
}

- (void)updateInkAction:(NSButton *)currentButton {
    self.addInkButton.wantsLayer =
    self.inkView.wantsLayer =
    self.clearInkButton.wantsLayer = YES;
    self.addInkButton.layer.cornerRadius =
    self.inkView.layer.cornerRadius =
    self.clearInkButton.layer.cornerRadius = 4.0;
    self.addInkButton.layer.backgroundColor =
    self.inkView.layer.backgroundColor =
    self.clearInkButton.layer.backgroundColor = [NSColor colorWithSRGBRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1].CGColor;
    
    if (currentButton == self.addInkButton) {
        self.addInkButton.layer.backgroundColor = [NSColor whiteColor].CGColor;
        [self.addInkButton setImage:[NSImage imageNamed:@"KMImagePropertPanelADDSelect"]];
        [self.clearInkButton setImage:[NSImage imageNamed:@"KMImagePropertPanelClearNone"]];
    } else if (currentButton == self.clearInkButton) {
        self.clearInkButton.layer.backgroundColor = [NSColor whiteColor].CGColor;
        [self.addInkButton setImage:[NSImage imageNamed:@"KMImagePropertPanelADDNone"]];
        [self.clearInkButton setImage:[NSImage imageNamed:@"KMImagePropertPanelClearSelect"]];
    }
}

- (void)updateFillColorPickerVC
{
    if (self.annotationModel.annotations.count > 0) {
        NSColor *color = nil;
        CPDFAnnotation *firstAnnotation = self.annotationModel.annotations.firstObject;
        if ([firstAnnotation isKindOfClass:[CPDFCircleAnnotation class]] ||
            [firstAnnotation isKindOfClass:[CPDFSquareAnnotation class]]) {
            if ([firstAnnotation isKindOfClass:[CPDFCircleAnnotation class]]) {
                color = ((CPDFCircleAnnotation *)firstAnnotation).interiorColor;
            } else {
                color = ((CPDFSquareAnnotation *)firstAnnotation).interiorColor;
            }
            self.fillColorPickerVC.isFillColor = YES;
        } else {
            color = firstAnnotation.color;
        }
        self.fillColorPickerVC.color = color;
    } else {
        NSColor *color = nil;
        if (CAnnotationTypeCircle == _annotationType ||
            CAnnotationTypeSquare == _annotationType) {
            color = self.annotationModel.interiorColor;
            self.fillColorPickerVC.isFillColor = YES;
        } else {
            color = self.annotationModel.color;
        }
        
        self.fillColorPickerVC.color = color;
    }
    self.generalImageView.image = [self.annotationModel annotationImage];
}

- (void)updateBackgroundColorPickerVC
{
    NSColor *color = nil;
    if (CAnnotationTypeSignDate == _annotationType) {
        color = self.annotationModel.color;
        if (color) {
            CGFloat red,green,blue,alpha;
            [[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
            color = [NSColor colorWithRed:red green:green blue:blue alpha:self.annotationModel.opacity];
        }
        self.backgroundColorPickerVC.color = color;
    }

    self.generalImageView.image = [self.annotationModel annotationImage];
}

- (void)updateFontView {
    if (_annotationModel.annotations.count > 0) {
        CPDFAnnotation *firstAnnotation = _annotationModel.annotations.firstObject;
        if ([firstAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
            CPDFFreeTextAnnotation *textNote = (CPDFFreeTextAnnotation *)firstAnnotation;
            if (_annotationModel.annotations.count > 1) {
                NSFont *currentFont = textNote.font;
                NSArray *fonts = [CPDFAnnotationModel supportFonts];
                NSDictionary * dic = nil;
                NSMutableArray *mutableArr = [NSMutableArray array];
                for (NSUInteger i=0; i<fonts.count; i++) {
                    NSDictionary *fontDic = fonts[i];
                    NSString * fontName = fontDic.allKeys.firstObject?:@"Helvetica";
                    NSFont *font = [NSFont fontWithName:fontName size:12.0];
                    NSDictionary *attrited = @{NSFontAttributeName:font};
                    NSAttributedString *string = [[NSAttributedString alloc] initWithString:fontName attributes:attrited];
                    [mutableArr addObject:string];
                    NSString * family = [currentFont.fontDescriptor objectForKey:NSFontFamilyAttribute];
                    
                    if ([family isEqualToString:fontName]) {
                        dic = fontDic;
                    }
                }
                NSString * style = [currentFont.fontDescriptor objectForKey:NSFontFaceAttribute];
                if (dic) {
                    NSInteger selectedIndex = [fonts indexOfObject:dic];
                    [self.fontNameVC selectItemAt:selectedIndex];
                    NSString * fontName = dic.allKeys.firstObject;
                    NSInteger selectIndex = [self setFontStyleWithFontName:fontName currentStyle:style];
                    if (selectIndex != -1) {
                        [self.fontStyleVC selectItemAt:selectIndex];
                    } else {
                        self.fontStyleVC.stringValue = @"";
                    }
                    NSNumber *sizeString = [textNote.font.fontDescriptor objectForKey:NSFontSizeAttribute];
                    self.fontSizeVC.stringValue = [NSString stringWithFormat:@"%@pt",sizeString.stringValue];

                    NSDictionary *model = [self freeTextPropertWithTitle:self.defaultPopUpButton.indexOfSelectedItem];
                    if (model) {
                        NSArray *modelFontNames = [model[@"fontName"] componentsSeparatedByString:@"-"];
                        if ([self.fontStyleVC.stringValue isEqualToString:@"Regular"] && (modelFontNames.count == 1)) {
                            if (![self.fontNameVC.stringValue isEqualToString:modelFontNames.firstObject] ||
                                _annotationModel.fontSize != [model[@"fontSize"] floatValue]) {
                                NSInteger defaultCount = self.defaultPopUpButton.indexOfSelectedItem;
                                
                                [self.defaultPopUpButton removeAllItems];
                                NSArray *defaultArr = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
                                NSString *defaultString = defaultArr[defaultCount];
                                for(NSString* title in defaultArr){
                                    if ([title isEqualToString:defaultString] && ![title isEqualToString:@"Customize"]) {
                                        [self.defaultPopUpButton addItemWithTitle:[NSString stringWithFormat:@"*%@", NSLocalizedString(title, nil)]];
                                    } else {
                                        [self.defaultPopUpButton addItemWithTitle:NSLocalizedString(title, nil)];
                                    }
                                }
                                [self.defaultPopUpButton selectItemAtIndex:defaultCount];
                            }
                        } else {
                            if (![self.fontNameVC.stringValue isEqualToString:modelFontNames.firstObject] ||
                                ![self.fontStyleVC.stringValue isEqualToString:modelFontNames.lastObject] ||
                                _annotationModel.fontSize != [model[@"fontSize"] floatValue]) {
                                NSInteger defaultCount = self.defaultPopUpButton.indexOfSelectedItem;
                                
                                [self.defaultPopUpButton removeAllItems];
                                NSArray *defaultArr = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
                                NSString *defaultString = defaultArr[defaultCount];
                                for(NSString* title in defaultArr){
                                    if ([title isEqualToString:defaultString] && ![title isEqualToString:@"Customize"]) {
                                        [self.defaultPopUpButton addItemWithTitle:[NSString stringWithFormat:@"*%@", NSLocalizedString(title, nil)]];
                                    } else {
                                        [self.defaultPopUpButton addItemWithTitle:NSLocalizedString(title, nil)];
                                    }
                                }
                                [self.defaultPopUpButton selectItemAtIndex:defaultCount];
                            } else {
                                NSInteger defaultCount = self.defaultPopUpButton.indexOfSelectedItem;
                                
                                [self.defaultPopUpButton removeAllItems];
                                NSArray *defaultArr = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
                                [self.defaultPopUpButton addItemsWithTitles:defaultArr];
                                [self.defaultPopUpButton selectItemAtIndex:defaultCount];
                            }
                        }
                    } else {
                        NSInteger defaultCount = self.defaultPopUpButton.indexOfSelectedItem;
                        
                        [self.defaultPopUpButton removeAllItems];
                        NSArray *defaultArr = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
                        [self.defaultPopUpButton addItemsWithTitles:defaultArr];
                        [self.defaultPopUpButton selectItemAtIndex:defaultCount];
                    }
                } else {
                    self.fontNameVC.stringValue = @"";
                    self.fontStyleVC.stringValue = @"";
                }
//                NSString *familyString = [textNote.font.fontDescriptor objectForKey:NSFontFamilyAttribute];
//                NSString *faceString = [textNote.font.fontDescriptor objectForKey:NSFontFaceAttribute];
//                NSNumber *sizeString = [textNote.font.fontDescriptor objectForKey:NSFontSizeAttribute];
//                _fontNameVC.stringValue = familyString;
//                _fontStyleVC.stringValue = faceString;
//                _fontSizeVC.stringValue = sizeString.stringValue;
                if (textNote.alignment == NSTextAlignmentLeft) {
                    self.leftAlignVC.state = KMDesignTokenStateSel;
                } else if (textNote.alignment == NSTextAlignmentCenter) {
                    self.centerAlignVC.state = KMDesignTokenStateSel;
                } else if (textNote.alignment == NSTextAlignmentRight) {
                    self.rightAlignVC.state = KMDesignTokenStateSel;
                }
                self.fontColorButton.color = textNote.fontColor ? : [NSColor blackColor];
                [self.fontColorButton setNeedsDisplay:YES];
            } else {
                NSFont *currentFont = textNote.font;
                NSArray *fonts = [CPDFAnnotationModel supportFonts];
                NSDictionary * dic = nil;
                NSMutableArray *mutableArr = [NSMutableArray array];
                for (NSUInteger i=0; i<fonts.count; i++) {
                    NSDictionary *fontDic = fonts[i];
                    NSString * fontName = fontDic.allKeys.firstObject?:@"Helvetica";
                    NSFont *font = [NSFont fontWithName:fontName size:12.0];
                    NSDictionary *attrited = @{NSFontAttributeName:font};
                    NSAttributedString *string = [[NSAttributedString alloc] initWithString:fontName attributes:attrited];
                    [mutableArr addObject:string];
                    NSString * family = [currentFont.fontDescriptor objectForKey:NSFontFamilyAttribute];

                    if ([family isEqualToString:fontName]) {
                        dic = fontDic;
                    }
                }
                NSString * style = [currentFont.fontDescriptor objectForKey:NSFontFaceAttribute];

                if (dic) {
                    NSInteger selectedIndex = [fonts indexOfObject:dic];
                    [self.fontNameVC selectItemAt:selectedIndex];
                    NSString * fontName = dic.allKeys.firstObject;
                    NSInteger selectIndex = [self setFontStyleWithFontName:fontName currentStyle:style];
                    if (selectIndex != -1) {
                        [self.fontStyleVC selectItemAt:selectIndex];
                    } else {
                        self.fontStyleVC.stringValue = @"";
                    }
                } else {
                    self.fontNameVC.stringValue = @"";
                    self.fontStyleVC.stringValue = @"";
                }
                NSNumber *sizeString = [textNote.font.fontDescriptor objectForKey:NSFontSizeAttribute];
                self.fontSizeVC.stringValue = [NSString stringWithFormat:@"%@pt",sizeString.stringValue];

                NSDictionary *model = [self freeTextPropertWithTitle:self.defaultPopUpButton.indexOfSelectedItem];
                if (model) {
                    NSArray *modelFontNames = [model[@"fontName"] componentsSeparatedByString:@"-"];
                    if ([self.fontStyleVC.stringValue isEqualToString:@"Regular"] && (modelFontNames.count == 1)) {
                        if (![self.fontNameVC.stringValue isEqualToString:modelFontNames.firstObject] ||
                            _annotationModel.fontSize != [model[@"fontSize"] floatValue]) {
                            NSInteger defaultCount = self.defaultPopUpButton.indexOfSelectedItem;
                            
                            [self.defaultPopUpButton removeAllItems];
                            NSArray *defaultArr = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
                            NSString *defaultString = defaultArr[defaultCount];
                            for(NSString* title in defaultArr){
                                if ([title isEqualToString:defaultString] && ![title isEqualToString:@"Customize"]) {
                                    [self.defaultPopUpButton addItemWithTitle:[NSString stringWithFormat:@"*%@", NSLocalizedString(title, nil)]];
                                } else {
                                    [self.defaultPopUpButton addItemWithTitle:NSLocalizedString(title, nil)];
                                }
                            }
                            [self.defaultPopUpButton selectItemAtIndex:defaultCount];
                        }
                    } else {
                        if (![self.fontNameVC.stringValue isEqualToString:modelFontNames.firstObject] ||
                            ![self.fontStyleVC.stringValue isEqualToString:modelFontNames.lastObject] ||
                            _annotationModel.fontSize != [model[@"fontSize"] floatValue]) {
                            NSInteger defaultCount = self.defaultPopUpButton.indexOfSelectedItem;
                            
                            [self.defaultPopUpButton removeAllItems];
                            NSArray *defaultArr = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
                            NSString *defaultString = defaultArr[defaultCount];
                            for(NSString* title in defaultArr){
                                if ([title isEqualToString:defaultString] && ![title isEqualToString:@"Customize"]) {
                                    [self.defaultPopUpButton addItemWithTitle:[NSString stringWithFormat:@"*%@", NSLocalizedString(title, nil)]];
                                } else {
                                    [self.defaultPopUpButton addItemWithTitle:NSLocalizedString(title, nil)];
                                }
                            }
                            [self.defaultPopUpButton selectItemAtIndex:defaultCount];
                        } else {
                            NSInteger defaultCount = self.defaultPopUpButton.indexOfSelectedItem;
                            
                            [self.defaultPopUpButton removeAllItems];
                            NSArray *defaultArr = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
                            [self.defaultPopUpButton addItemsWithTitles:defaultArr];
                            [self.defaultPopUpButton selectItemAtIndex:defaultCount];
                        }
                    }
                } else {
                    NSInteger defaultCount = self.defaultPopUpButton.indexOfSelectedItem;
                    
                    [self.defaultPopUpButton removeAllItems];
                    NSArray *defaultArr = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
                    [self.defaultPopUpButton addItemsWithTitles:defaultArr];
                    [self.defaultPopUpButton selectItemAtIndex:defaultCount];
                }
//                NSString *familyString = [textNote.font.fontDescriptor objectForKey:NSFontFamilyAttribute];
//                NSString *faceString = [textNote.font.fontDescriptor objectForKey:NSFontFaceAttribute];
//                NSNumber *sizeString = [textNote.font.fontDescriptor objectForKey:NSFontSizeAttribute];
//                _fontNameVC.stringValue = familyString;
//                _fontStyleVC.stringValue = faceString;
//                _fontSizeVC.stringValue = sizeString.stringValue;
                if (textNote.alignment == NSTextAlignmentLeft) {
                    self.leftAlignVC.state = KMDesignTokenStateSel;
                } else if (textNote.alignment == NSTextAlignmentCenter) {
                    self.centerAlignVC.state = KMDesignTokenStateSel;
                } else if (textNote.alignment == NSTextAlignmentRight) {
                    self.rightAlignVC.state = KMDesignTokenStateSel;
                }
                self.fontColorButton.color = textNote.fontColor ? : [NSColor blackColor];
                [self.fontColorButton setNeedsDisplay:YES];
            }
        }
    } else {
        NSArray *fontnames = [_annotationModel.fontName componentsSeparatedByString:@"-"];
        NSFont *currentFont = [NSFont systemFontOfSize:12.0];
        if ([fontnames.lastObject isEqualToString:@"Regular"] || [fontnames.lastObject isEqualToString:@"Roman"]) {
            currentFont = [NSFont fontWithName:fontnames.firstObject size:_annotationModel.fontSize];
        } else {
            currentFont = [NSFont fontWithName:_annotationModel.fontName size:_annotationModel.fontSize];
        }
        
        NSArray *fonts = [CPDFAnnotationModel supportFonts];
        NSDictionary * dic = nil;
        NSMutableArray *mutableArr = [NSMutableArray array];
        for (NSUInteger i=0; i<fonts.count; i++) {
            NSDictionary *fontDic = fonts[i];
            NSString * fontName = fontDic.allKeys.firstObject?:@"Helvetica";
            NSFont *font = [NSFont fontWithName:fontName size:12.0];
            NSDictionary *attrited = @{NSFontAttributeName:font};
            NSAttributedString *string = [[NSAttributedString alloc] initWithString:fontName attributes:attrited];
            [mutableArr addObject:string];
            NSString * family = [currentFont.fontDescriptor objectForKey:NSFontFamilyAttribute];

            if ([family isEqualToString:fontName]) {
                dic = fontDic;
            }
        }
        NSString * style = [currentFont.fontDescriptor objectForKey:NSFontFaceAttribute];

        if (dic) {
            NSInteger selectedIndex = [fonts indexOfObject:dic];
            [self.fontNameVC selectItemAt:selectedIndex];
            NSString * fontName = dic.allKeys.firstObject;
            NSInteger selectIndex = [self setFontStyleWithFontName:fontName currentStyle:style];
            if (selectIndex != -1) {
                [self.fontStyleVC selectItemAt:selectIndex];
            } else {
                self.fontStyleVC.stringValue = @"";
            }
        } else {
            self.fontNameVC.stringValue = @"";
            self.fontStyleVC.stringValue = @"";
        }
        NSNumber *sizeString = [currentFont.fontDescriptor objectForKey:NSFontSizeAttribute];
        self.fontSizeVC.stringValue = [NSString stringWithFormat:@"%@pt",sizeString.stringValue];
        
        NSDictionary *model = [self freeTextPropertWithTitle:self.defaultPopUpButton.indexOfSelectedItem];
        if (model) {
            NSArray *modelFontNames = [model[@"fontName"] componentsSeparatedByString:@"-"];
            if ([self.fontStyleVC.stringValue isEqualToString:@"Regular"] && (modelFontNames.count == 1)) {
                if (![self.fontNameVC.stringValue isEqualToString:modelFontNames.firstObject] ||
                    _annotationModel.fontSize != [model[@"fontSize"] floatValue]) {
                    NSInteger defaultCount = self.defaultPopUpButton.indexOfSelectedItem;
                    
                    [self.defaultPopUpButton removeAllItems];
                    NSArray *defaultArr = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
                    NSString *defaultString = defaultArr[defaultCount];
                    for(NSString* title in defaultArr){
                        if ([title isEqualToString:defaultString] && ![title isEqualToString:@"Customize"]) {
                            [self.defaultPopUpButton addItemWithTitle:[NSString stringWithFormat:@"*%@", NSLocalizedString(title, nil)]];
                        } else {
                            [self.defaultPopUpButton addItemWithTitle:NSLocalizedString(title, nil)];
                        }
                    }
                    [self.defaultPopUpButton selectItemAtIndex:defaultCount];
                }
            } else {
                if (![self.fontNameVC.stringValue isEqualToString:modelFontNames.firstObject] ||
                    ![self.fontStyleVC.stringValue isEqualToString:modelFontNames.lastObject] ||
                    _annotationModel.fontSize != [model[@"fontSize"] floatValue]) {
                    NSInteger defaultCount = self.defaultPopUpButton.indexOfSelectedItem;
                    
                    [self.defaultPopUpButton removeAllItems];
                    NSArray *defaultArr = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
                    NSString *defaultString = defaultArr[defaultCount];
                    for(NSString* title in defaultArr){
                        if ([title isEqualToString:defaultString] && ![title isEqualToString:@"Customize"]) {
                            [self.defaultPopUpButton addItemWithTitle:[NSString stringWithFormat:@"*%@", NSLocalizedString(title, nil)]];
                        } else {
                            [self.defaultPopUpButton addItemWithTitle:NSLocalizedString(title, nil)];
                        }
                    }
                    [self.defaultPopUpButton selectItemAtIndex:defaultCount];
                } else {
                    NSInteger defaultCount = self.defaultPopUpButton.indexOfSelectedItem;
                    
                    [self.defaultPopUpButton removeAllItems];
                    NSArray *defaultArr = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
                    [self.defaultPopUpButton addItemsWithTitles:defaultArr];
                    [self.defaultPopUpButton selectItemAtIndex:defaultCount];
                }
            }
        } else {
            NSInteger defaultCount = self.defaultPopUpButton.indexOfSelectedItem;
            
            [self.defaultPopUpButton removeAllItems];
            NSArray *defaultArr = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
            [self.defaultPopUpButton addItemsWithTitles:defaultArr];
            [self.defaultPopUpButton selectItemAtIndex:defaultCount];
        }
//        NSString *familyString = [currentFont.fontDescriptor objectForKey:NSFontFamilyAttribute];
//        NSString *faceString = [currentFont.fontDescriptor objectForKey:NSFontFaceAttribute];
//        NSNumber *sizeString = [currentFont.fontDescriptor objectForKey:NSFontSizeAttribute];
//        _fontNameVC.stringValue = familyString;
//        _fontStyleVC.stringValue = faceString;
//        _fontSizeVC.stringValue = sizeString.stringValue;
        if (_annotationModel.alignment == NSTextAlignmentLeft) {
            self.leftAlignVC.state = KMDesignTokenStateSel;
        } else if (_annotationModel.alignment == NSTextAlignmentCenter) {
            self.centerAlignVC.state = KMDesignTokenStateSel;
        } else if (_annotationModel.alignment == NSTextAlignmentRight) {
            self.rightAlignVC.state = KMDesignTokenStateSel;
        }
        self.fontColorButton.color = _annotationModel.fontColor ? : [NSColor blackColor];
        [self.fontColorButton setNeedsDisplay:YES];
    }
    
    self.generalImageView.image = [self.annotationModel annotationImage];
}

- (void)updateBorderColorPickerVC
{
    NSColor *color = self.annotationModel.color;
    if (self.annotationModel.annotations.count > 1) {
        CPDFAnnotation *firstAnnotation = _annotationModel.annotations.firstObject;
        color = firstAnnotation.color;
    } else {
        color = self.annotationModel.color;
    }
    CGFloat red,green,blue,alpha;
    [[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
    
    if (CAnnotationTypeCircle== _annotationType ||
        CAnnotationTypeSquare == _annotationType) {
        color = [NSColor colorWithRed:red green:green blue:blue alpha:self.annotationModel.opacity];
    }

    self.borderColorPickerVC.color = color;
    self.generalImageView.image = [self.annotationModel annotationImage];
}

- (void)updateDateView
{
    [_datePopupButton removeAllItems];
    BOOL includeTime = self.annotationModel.includeTime;
    [_datePopupButton addItemsWithTitles:[KMSelfSignAnnotationFreeText fetchAllDateString:includeTime]];
    NSInteger dateFormatIndex = self.annotationModel.dateFormatIndex;
    if (dateFormatIndex >= 0 && dateFormatIndex < _datePopupButton.numberOfItems) {
        [_datePopupButton selectItemAtIndex:dateFormatIndex];
    } else {
        [_datePopupButton selectItemAtIndex:0];
    }
    _dateButton.state = includeTime ? NSControlStateValueOn:NSControlStateValueOff;
}

- (void)typeSelected:(CAnnotationType)type
{
    _falseTypeButton.layer.backgroundColor =
    _tureTypeButton.layer.backgroundColor =
    _circletypeButton.layer.backgroundColor =
    _lineTypeButton.layer.backgroundColor =
    _dotTypeButton.layer.backgroundColor = [NSColor clearColor].CGColor;
    _falseTypeButton.image = [NSImage imageNamed:@"KMImageNamePropertybarFillsignForkNor"];
    _tureTypeButton.image = [NSImage imageNamed:@"KMImageNamePropertybarFillsignTickNor"];
    _circletypeButton.image = [NSImage imageNamed:@"KMImageNamePropertybarFillsignRoundedrecNor"];
    _lineTypeButton.image = [NSImage imageNamed:@"KMImageNamePropertybarFillsignLineNor"];
//    _dotTypeButton.image = [NSImage imageNamed:@"KMImageNamePropertybarFillsignPointNor"];
    if (type == CAnnotationTypeSignFalse) {
        self.falseTypeButton.layer.backgroundColor = [NSColor grayColor].CGColor;
        _falseTypeButton.image = [NSImage imageNamed:@"KMImageNamePropertybarFillsignForkSel"];
    } else if (type == CAnnotationTypeSignTure) {
        self.tureTypeButton.layer.backgroundColor = [NSColor grayColor].CGColor;
        _tureTypeButton.image = [NSImage imageNamed:@"KMImageNamePropertybarFillsignTickSel"];
    } else if (type == CAnnotationTypeSignCircle){
        self.circletypeButton.layer.backgroundColor = [NSColor grayColor].CGColor;
        _circletypeButton.image = [NSImage imageNamed:@"KMImageNamePropertybarFillsignRounderdrecSel"];
    } else if (type == CAnnotationTypeSignLine) {
        self.lineTypeButton.layer.backgroundColor = [NSColor grayColor].CGColor;
        _lineTypeButton.image = [NSImage imageNamed:@"KMImageNamePropertybarFillsignLineSel"];
    } else if (type == CAnnotationTypeSignDot) {
        self.dotTypeButton.layer.backgroundColor = [NSColor grayColor].CGColor;
        _dotTypeButton.image = [NSImage imageNamed:@"KMImageNamePropertybarFillsignPointSet"];
    }
}

- (void)updateAnnotation
{
    CPDFAnnotation *annotation = self.annotationModel.annotation;
    if (annotation) {
        if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
            CPDFFreeTextAnnotation *textNote = (CPDFFreeTextAnnotation *)annotation;
            if ([self.pdfView isEditWithCurrentFreeText:textNote]) {
                [self.pdfView commitEditAnnotationFreeText:textNote];
            }
        }
        for (CPDFAnnotation *tAnnotation in self.annotationModel.annotations) {
             if([tAnnotation isKindOfClass:[CPDFStampAnnotation class]]) {
                 [(KMSelfSignAnnotation *)tAnnotation updateAppearanceStream];
             }
            
            if ([tAnnotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
                [self.pdfView setNeedsDisplayForPage:tAnnotation.page];
            } else {
                [self.pdfView setNeedsDisplayAnnotationViewForPage:tAnnotation.page];
            }
        }
    }
    
    self.generalImageView.image = [self.annotationModel annotationImage];
}

- (NSDictionary *)freeTextPropertWithTitle:(NSInteger)integer {
    NSDictionary *model;
    NSArray *array = [CPDFAnnotationModel defaultFreeTextPropert];
//    if ([string isEqualToString:@"Customize"]) {
//        model = [array objectAtIndex:0];
//    } else if ([string isEqualToString:@"H1 Headline"]) {
//        model = [array objectAtIndex:1];
//    } else if ([string isEqualToString:@"H2 Title"]) {
//        model = [array objectAtIndex:2];
//    } else if ([string isEqualToString:@"H3 Subtitle"]) {
//        model = [array objectAtIndex:3];
//    } else if ([string isEqualToString:@"B1 Text"]) {
//        model = [array objectAtIndex:4];
//    } else if ([string isEqualToString:@"B2 Small Text"]) {
//        model = [array objectAtIndex:5];
//    } else if ([string isEqualToString:@"B3 Describtion"]) {
//        model = [array objectAtIndex:6];
//    }
    model = [array objectAtIndex:integer];
    return model;
}

#pragma mark  MarkUp

- (void)createMarkupProperties {
    [self hiddenSubviews];
    
    _fillColorBox.contentView = _fillColorPickerVC.view;
    __block typeof(self) blockSelf = self;
    _fillColorPickerVC.colorChangeCallback = ^(NSColor *color) {
        blockSelf.annotationModel.color = color;
        blockSelf.annotationModel.opacity = color.alphaComponent;
        
        [blockSelf updateOpacityView];
        [blockSelf updateAnnotation];
    };
    
    if (CAnnotationTypeHighlight == _annotationType) {
        self.fillColorPickerVC.annotationType = KMPropertiesColor_MarkupHighlightColors;
    } else {
        self.fillColorPickerVC.annotationType = KMPropertiesColor_MarkupOtherColors;
    }
}

#pragma mark  SKLineNote

- (void)createInkAndLineProperties {
    [self hiddenSubviews];
    
    _fillColorBox.contentView = _fillColorPickerVC.view;
    __block typeof(self) blockSelf = self;
    _fillColorPickerVC.colorChangeCallback = ^(NSColor *color) {
        if (color) {
            CGFloat r, g, b, a = 0.0;
            [[color colorUsingColorSpaceName:NSDeviceRGBColorSpace] getRed:&r green:&g blue:&b alpha:&a];
            
            blockSelf.annotationModel.color = color;
            blockSelf.annotationModel.opacity = a;
        } else {
            blockSelf.annotationModel.color = [NSColor blackColor];
            blockSelf.annotationModel.opacity = 0;

        }
        
        [blockSelf updateOpacityView];
        [blockSelf updateAnnotation];
    };
    
    self.borderWidthSlider.floatValue = self.annotationModel.lineWidth;
    self.borderWidthVC.stringValue = [NSString stringWithFormat:@"%0.1f pt",self.annotationModel.lineWidth];
    
    if (CAnnotationTypeLine == self.annotationModel.annotationType ||
        CAnnotationTypeArrow == self.annotationModel.annotationType) {
        self.fillColorPickerVC.annotationType = KMPropertiesColor_LineColors;
        
    } else if (CAnnotationTypeInk == self.annotationModel.annotationType) {
        self.fillColorPickerVC.annotationType = KMPropertiesColor_InkColors;
    }
}

- (void)createDefultFontPropert {
    NSArray *defaultSting = @[@"Customize",@"H1 Headline",@"H2 Title",@"H3 Subtitle",@"B1 Text",@"B2 Small Text",@"B3 Describtion"];
    for(NSString* title in defaultSting){
        [self.defaultPopUpButton addItemWithTitle:NSLocalizedString(title, nil)];
    }
    [self.defaultPopUpButton selectItemAtIndex:0];
    
}

- (void)updateFigureModel {
    if(self.pdfView.annotationType != CAnnotationTypeUnkown) {
        if (_annotationType == CAnnotationTypeSquare) {
            self.squareVC.state = KMDesignTokenStateSel;
        } else if (_annotationType == CAnnotationTypeCircle) {
            self.circleVC.state = KMDesignTokenStateSel;
        } else if (_annotationType == CAnnotationTypeArrow) {
            self.arrowVC.state = KMDesignTokenStateSel;
        } else if (_annotationType == CAnnotationTypeLine) {
            self.lineVC.state = KMDesignTokenStateSel;
        }
    }
}

- (void)updateCurrentDefaultFont:(NSMenuItem *)item {
    if (item.tag == 0) {
        NSDictionary *newdic = @{@"fontName":self.annotationModel.fontName,@"fontSize":[NSString stringWithFormat:@"%.f",self.annotationModel.fontSize]};
        switch (self.defaultPopUpButton.indexOfSelectedItem) {
            case 1:
                [[NSUserDefaults standardUserDefaults] setObject:newdic forKey:@"CPDFFreeTextDefaultFontObjects_H1"];
                break;
            case 2:
                [[NSUserDefaults standardUserDefaults] setObject:newdic forKey:@"CPDFFreeTextDefaultFontObjects_H2"];
                break;
            case 3:
                [[NSUserDefaults standardUserDefaults] setObject:newdic forKey:@"CPDFFreeTextDefaultFontObjects_H3"];
                break;
            case 4:
                [[NSUserDefaults standardUserDefaults] setObject:newdic forKey:@"CPDFFreeTextDefaultFontObjects_B1"];
                break;
            case 5:
                [[NSUserDefaults standardUserDefaults] setObject:newdic forKey:@"CPDFFreeTextDefaultFontObjects_B2"];
                break;
            case 6:
                [[NSUserDefaults standardUserDefaults] setObject:newdic forKey:@"CPDFFreeTextDefaultFontObjects_B3"];
                break;
            default:
                break;
        }
        [self updateFontView];
    } else if (item.tag == 1) {
        switch (self.defaultPopUpButton.indexOfSelectedItem) {
            case 1:
                [[NSUserDefaults standardUserDefaults] setObject:@{@"fontName":@"Helvetica-Bold",@"fontSize":@"36"} forKey:@"CPDFFreeTextDefaultFontObjects_H1"];
                break;
            case 2:
                [[NSUserDefaults standardUserDefaults] setObject:@{@"fontName":@"Helvetica-Bold",@"fontSize":@"24"} forKey:@"CPDFFreeTextDefaultFontObjects_H2"];
                break;
            case 3:
                [[NSUserDefaults standardUserDefaults] setObject:@{@"fontName":@"Helvetica-Bold",@"fontSize":@"18"} forKey:@"CPDFFreeTextDefaultFontObjects_H3"];
                break;
            case 4:
                [[NSUserDefaults standardUserDefaults] setObject:@{@"fontName":@"Helvetica",@"fontSize":@"14"} forKey:@"CPDFFreeTextDefaultFontObjects_B1"];
                break;
            case 5:
                [[NSUserDefaults standardUserDefaults] setObject:@{@"fontName":@"Helvetica",@"fontSize":@"12"} forKey:@"CPDFFreeTextDefaultFontObjects_B2"];
                break;
            case 6:
                [[NSUserDefaults standardUserDefaults] setObject:@{@"fontName":@"Helvetica",@"fontSize":@"11"} forKey:@"CPDFFreeTextDefaultFontObjects_B3"];
                break;
            default:
                break;
        }
        [self defaultFontPopUpButtonAction:nil];
    }
}

#pragma mark - SquareNote、CircleNote、ArrowNote Properties

- (void)createSquareAndCircleProperties {
    [self hiddenSubviews];
    
    __block typeof(self) blockSelf = self;
    _fillColorBox.contentView = _fillColorPickerVC.view;
    _fillColorPickerVC.colorChangeCallback = ^(NSColor *color) {
        CGFloat red,green,blue,alpha;
        [[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
        if (fabs(alpha - 0) < 0.001) {
            blockSelf.annotationModel.interiorColor = nil;
        } else {
            blockSelf.annotationModel.interiorColor = color;
        }
        [blockSelf updateAnnotation];
    };
    _borderColorPickerVC.colorChangeCallback = ^(NSColor *color) {
        if (color) {
            blockSelf.annotationModel.color = color;
            blockSelf.annotationModel.opacity = 1;
        } else {
            //设置透明色必须要设一个颜色值
            blockSelf.annotationModel.color = [NSColor blackColor];
            blockSelf.annotationModel.opacity = 0;
        }
        [blockSelf updateOpacityView];
        [blockSelf updateAnnotation];
    };
    
    self.borderWidthSlider.floatValue = self.annotationModel.lineWidth;
    self.borderWidthVC.stringValue = [NSString stringWithFormat:@"%0.1f pt",self.annotationModel.lineWidth];
    self.fillColorPickerVC.annotationType = KMPropertiesColor_LineFillColors;
    self.borderColorPickerVC.annotationType = KMPropertiesColor_CircleLineColors;
}

#pragma mark - FreeTextNote、KMPDFAnnotationFreeText Properties

- (void)createFreeTextProperties {
    [self hiddenSubviews];
    
    __block typeof(self) blockSelf = self;
    _fillColorBox.contentView = _fillColorPickerVC.view;
    _fillColorPickerVC.colorChangeCallback = ^(NSColor *color) {
        CGFloat red,green,blue,alpha;
        [[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
        if(alpha == 0) {
            blockSelf.annotationModel.color = nil;
        } else {
            blockSelf.annotationModel.color = color;
        }
        [blockSelf updateOpacityView];

        CPDFAnnotation *annotation = blockSelf.annotationModel.annotation;

        if (annotation) {
            [blockSelf.pdfView setNeedsDisplayAnnotationViewForPage:annotation.page];
            if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
                CPDFFreeTextAnnotation *textNote = (CPDFFreeTextAnnotation *)annotation;
                if ([blockSelf.pdfView isEditWithCurrentFreeText:textNote]) {
                    [blockSelf.pdfView setEditAnnotationFreeTextBackgroundColor:color freeText:textNote];
                } else {
                    [blockSelf updateAnnotation];
                }
            }
            blockSelf.generalImageView.image = [blockSelf.annotationModel annotationImage];
        } else {
            [blockSelf updateAnnotation];
        }
       
    };
    
    if (CAnnotationTypeFreeText == _annotationType ||
        CAnnotationTypeSignDate == _annotationType ||
        CAnnotationTypeSignText == _annotationType) {
        self.fontColorButton.color = self.annotationModel.fontColor;
        [self.fontColorButton setNeedsDisplay:YES];
    }
    
    self.leftAlignVC.button.toolTip = NSLocalizedString(@"Left Alignment", nil);
    self.centerAlignVC.button.toolTip = NSLocalizedString(@"Center", nil);
    self.rightAlignVC.button.toolTip = NSLocalizedString(@"Right Alignment", nil);
    
//    NSArray *array = @[blockSelf.leftAlignBox,blockSelf.centerAlignBox,blockSelf.rightAlignBox];
//    for (KMBox *box in array) {
//        box.fillColor = [NSColor clearColor];
//        box.mouseDownCallback = ^(BOOL downEntered, KMBox *mouseBox) {
//            if (downEntered) {
//                box.fillColor = [NSColor colorWithRed:206.0/255.0 green:208.0/255.0 blue:212.0/255.0 alpha:0.6];
//                if (box == blockSelf.leftAlignBox) {
//                    [blockSelf leftAlignButtonAction:box];
//                } else if (box == blockSelf.centerAlignBox) {
//                    [blockSelf centerAlignButtonAction:box];
//                } else if (box == blockSelf.rightAlignBox) {
//                    [blockSelf rightAlignButtonAction:box];
//                }
//            } else {
//                box.fillColor = [NSColor clearColor];
//            }
//        };
//        box.mouseMoveCallback = ^(BOOL mouseEntered, KMBox *mouseBox) {
//            if (mouseEntered) {
//                box.fillColor = [NSColor colorWithRed:237.0/255.0 green:238.0/255.0 blue:240.0/255.0 alpha:1];
//            } else {
//                box.fillColor = [NSColor clearColor];
//            }
//        };
//    }
    
    self.borderWidthSlider.floatValue = self.annotationModel.lineWidth;
    self.borderWidthVC.stringValue = [NSString stringWithFormat:@"%0.1f pt",self.annotationModel.lineWidth];
    
    self.fillColorPickerVC.annotationType = KMPropertiesColor_FreeTextFillColors;
}

#pragma mark - FreeTextNote、SelfSignAnnotationFreeText Properties

- (void)createSelfSignFreeTextProperties {
    [self hiddenSubviews];
    
    __block typeof(self) blockSelf = self;
    
//    NSArray *array = @[blockSelf.leftAlignBox,blockSelf.centerAlignBox,blockSelf.rightAlignBox];
//    for (KMBox *box in array) {
//        box.fillColor = [NSColor clearColor];
//        box.mouseDownCallback = ^(BOOL downEntered, KMBox *mouseBox) {
//            if (downEntered) {
//                box.fillColor = [NSColor colorWithRed:237.0/255.0 green:238.0/255.0 blue:240.0/255.0 alpha:1];
//                if (box == blockSelf.leftAlignBox) {
//                    [blockSelf leftAlignButtonAction:box];
//                } else if (box == blockSelf.centerAlignBox) {
//                    [blockSelf centerAlignButtonAction:box];
//                } else if (box == blockSelf.rightAlignBox) {
//                    [blockSelf rightAlignButtonAction:box];
//                }
//            } else {
//                box.fillColor = [NSColor clearColor];
//            }
//        };
//        box.mouseMoveCallback = ^(BOOL mouseEntered, KMBox *mouseBox) {
//            if (mouseEntered) {
//                box.fillColor = [NSColor lightGrayColor];
//            } else {
//                box.fillColor = [NSColor clearColor];
//            }
//        };
//    }
    _backgroundBox.contentView = _backgroundColorPickerVC.view;
    _backgroundColorPickerVC.colorChangeCallback = ^(NSColor *color) {
        CGFloat r, g, b, a = 0.0;
        [[color colorUsingColorSpaceName:NSDeviceRGBColorSpace] getRed:&r green:&g blue:&b alpha:&a];
        if(a == 0) {
            blockSelf.annotationModel.color = nil;
        } else {
            blockSelf.annotationModel.color = color;
        }
        [blockSelf updateOpacityView];
        [blockSelf updateAnnotation];
    };
    
    if ([self.annotationModel.annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
        self.fontColorButton.color = self.annotationModel.fontColor;
        [self.fontColorButton setNeedsDisplay:YES];
    }
    self.leftAlignVC.button.toolTip = NSLocalizedString(@"Left Alignment", nil);
    self.centerAlignVC.button.toolTip = NSLocalizedString(@"Center", nil);
    self.rightAlignVC.button.toolTip = NSLocalizedString(@"Right Alignment", nil);
    
    NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc]initWithAttributedString:[_dateButton attributedTitle]];
    NSUInteger len = [attrTitle length];
    NSRange range = NSMakeRange(0, len);
    [attrTitle addAttribute:NSForegroundColorAttributeName
                      value:[NSColor lightGrayColor]
                      range:range];
    [attrTitle fixAttributesInRange:range];
    [_dateButton setAttributedTitle:attrTitle];
    
    if ([self.annotationModel.annotation isKindOfClass:[KMSelfSignAnnotationFreeText class]]) {
        if (self.annotationModel.annotation.contents.length <= 0) {
            self.annotationModel.annotation.contents = self.datePopupButton.title;
        }
    }
    self.backgroundColorPickerVC.annotationType = KMPropertiesColor_FreeTextFillColors;
}

#pragma mark - KMSelfSignAnnotation

- (void)createSelfSignProperties {
    [self hiddenSubviews];
    
    __block typeof(self) blockSelf = self;
    _fillColorBox.contentView = _fillColorPickerVC.view;
    _fillColorPickerVC.colorChangeCallback = ^(NSColor *color) {
        CGFloat r, g, b, a = 0.0;
        [[color colorUsingColorSpaceName:NSDeviceRGBColorSpace] getRed:&r green:&g blue:&b alpha:&a];
        blockSelf.annotationModel.color = color;
        blockSelf.annotationModel.opacity = a;

        [blockSelf updateOpacityView];
        [blockSelf updateAnnotation];
    };
    if (self.annotationModel.annotation) {
        CAnnotationType annotationType = [(KMSelfSignAnnotation *)self.annotationModel.annotation annotationType];
        [self typeSelected:annotationType];
    }
    
    NSColor *color = self.annotationModel.color;
    CGFloat r, g, b, a = 0.0;
    [[color colorUsingColorSpaceName:NSDeviceRGBColorSpace] getRed:&r green:&g blue:&b alpha:&a];
    CGFloat opacity = self.annotationModel.opacity;

//    [self.borderWidthComboBox removeAllItems];
//    NSMutableArray *titles = @[@"1 pt",@"3 pt",@"6 pt",@"9 pt",@"12 pt"].mutableCopy;
//    for (NSString *string in titles) {
//        [self.borderWidthComboBox addItemWithObjectValue:string];
//    }
    
    self.borderWidthSlider.maxValue = 12;
    self.fillColorPickerVC.color = [NSColor colorWithRed:r green:g blue:b alpha:opacity];
    self.opacityComboBox.stringValue = [NSString stringWithFormat:@"%@%%",@((int)(opacity*100))];
    self.opacitySlider.floatValue = opacity;
    self.borderWidthSlider.floatValue = self.annotationModel.lineWidth;
    self.borderWidthVC.stringValue = [NSString stringWithFormat:@"%0.1f pt",self.annotationModel.lineWidth];
    
    self.fillColorPickerVC.annotationType = KMPropertiesColor_SelfSignColors;
}

-(NSUInteger)setFontStyleWithFontName:(NSString *)fontName currentStyle:(NSString *)style
{
    NSUInteger selectIndex = -1;
//    NSMenu *menu = [[NSMenu alloc] init];
    NSMutableArray *fontStyles = [NSMutableArray array];
    for (NSDictionary *dic in self.fonts) {
        NSString *familyString = dic.allKeys.firstObject;
        if ([familyString isEqual:fontName]) {
            NSArray *styles = [dic objectForKey:fontName];
            for (NSUInteger i = 0; i < styles.count; i++) {
                NSString * styleName = [styles objectAtIndex:i];
                if ([style isEqualToString:styleName]) {
                    selectIndex = i;
                }

                NSFontDescriptor *attributeFontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:@{NSFontFamilyAttribute:fontName,NSFontFaceAttribute:styleName}];
                if ([styleName isEqualToString:@"Roman"]) {
                    attributeFontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:@{NSFontFamilyAttribute:fontName}];
                    selectIndex = 0;
                }
//                NSFont *font = [NSFont fontWithDescriptor:attributeFontDescriptor size:12.0];
//                NSDictionary *attrited = @{NSFontAttributeName:font};
//                NSAttributedString *string = [[NSAttributedString alloc] initWithString:styleName attributes:attrited];
//                NSMenuItem *item = [[NSMenuItem alloc] init];
//                item.attributedTitle = string;

//                [menu addItem:item];
                [fontStyles addObject:styleName];
            }
            break;
        }
    }
    if (!style) {
        selectIndex = 0;
    }
//    [self.fontStylePopUpButton setMenu:menu];

    return selectIndex;
}

#pragma mark annotation text

- (void)createTextProperties {
    [self hiddenSubviews];
    
    __block typeof(self) blockSelf = self;
    _fillColorBox.contentView = _fillColorPickerVC.view;
    _fillColorPickerVC.colorChangeCallback = ^(NSColor *color) {
        blockSelf.annotationModel.color = color;

        [blockSelf updateOpacityView];
        [blockSelf updateAnnotation];
    };
    
    self.fillColorPickerVC.annotationType = KMPropertiesColor_AnchoredNoteColors;
  
    self.noteStyleNoneButton.image = [NSImage imageNamed:@"KMImagePropertTextStyleOne"];
    self.noteStyleButton1.image = [NSImage imageNamed:@"KMImagePropertTextStylenote"];
    self.noteStyleButton2.image = [NSImage imageNamed:@"KMImagePropertTextStyleKeyWord"];
    self.noteStyleButton3.image = [NSImage imageNamed:@"KMImagePropertTextStyleHelp"];
    self.noteStyleButton4.image = [NSImage imageNamed:@"KMImagePropertTextStyleP"];
    self.noteStyleButton5.image = [NSImage imageNamed:@"KMImagePropertTextStyleInsert"];
    self.noteStyleButton6.image = [NSImage imageNamed:@"KMImagePropertTextStyleNP"];
    
    CPDFTextAnnotationIconType type = CPDFTextAnnotationIconNote;
    if (self.annotationModel.annotations.count > 0) {
        CPDFAnnotation *firstAnnotation = _annotationModel.annotations.firstObject;
        if([firstAnnotation isKindOfClass:[CPDFTextAnnotation class]]) {
            type = [(CPDFTextAnnotation *)firstAnnotation iconType];
        }
    } else {
        type = self.annotationModel.anchoredIconType;
    }
    switch (type) {
        case CPDFTextAnnotationIconComment:
            self.noteStyleNoneButton.image = [NSImage imageNamed:@"KMImagePropertTextStyleOneSelect"];
            break;
        case CPDFTextAnnotationIconKey:
            self.noteStyleButton2.image = [NSImage imageNamed:@"KMImagePropertTextStyleKeyWordSelect"];
            break;
        case CPDFTextAnnotationIconNote:
            self.noteStyleButton1.image = [NSImage imageNamed:@"KMImagePropertTextStyleNoteSelect"];
            break;
        case CPDFTextAnnotationIconHelp:
            self.noteStyleButton3.image = [NSImage imageNamed:@"KMImagePropertTextStylehelpSelect"];
            break;
        case CPDFTextAnnotationIconNewParagraph:
            self.noteStyleButton6.image = [NSImage imageNamed:@"KMImagePropertTextStyleNPSelect"];
            break;
        case CPDFTextAnnotationIconParagraph:
            self.noteStyleButton4.image = [NSImage imageNamed:@"KMImagePropertTextStylePSelect"];
            break;
        case CPDFTextAnnotationIconInsert:
            self.noteStyleButton5.image = [NSImage imageNamed:@"KMImagePropertTextStyleInsertSelect"];
            break;
        default:
            break;
    }
}

#pragma mark annotation Ink

- (IBAction)updateInkStype:(NSButton *)sender {
    self.annotationModel.style = sender.tag;
    [self updateBorderWidthView];
    [self updateAnnotation];
}

- (IBAction)updateInkButton_click:(NSButton *)sender {
    [self updateInkAction:sender];
    if ((sender == self.addInkButton && _annotationType == CAnnotationTypeInk) || (sender == self.clearInkButton && _annotationType == CAnnotationTypeEraser)) {
        return;
    }

    if (sender != self.addInkButton) {
        self.pdfView.annotationType = CAnnotationTypeEraser;
        [self.borderWidthVC removeAllItems];
        [self.borderWidthVC addItemsWithObjectValues:@[@"5pt",@"10pt",@"15pt",@"20pt"]];
    } else {
        [self.borderWidthVC removeAllItems];
        [self.borderWidthVC addItemsWithObjectValues:@[@"1pt",@"2pt",@"4pt",@"6pt",@"8pt"]];
        self.pdfView.annotationType = CAnnotationTypeInk;
    }
    if (self.callBack) {
        self.callBack (self.pdfView.annotationType);
    }
    self.annotationModel = [[CPDFAnnotationModel alloc] initWithAnnotationType:self.pdfView.annotationType];
    [self loadingUIAndLocalization];
}

#pragma mark annotation stamp

- (IBAction)showOpacityView:(id)sender {
    KMOpacityPanel *vc = [[KMOpacityPanel alloc] initWithNibName:@"KMOpacityPanel" bundle:nil];
    vc.annotationModel = self.annotationModel;
    if (!_fillColorPopover) {
        _fillColorPopover = [[NSPopover alloc] init];
        _fillColorPopover.delegate = self;
    }
    _fillColorPopover.contentViewController = vc;
    _fillColorPopover.animates = YES;
    _fillColorPopover.behavior = NSPopoverBehaviorSemitransient;
    [_fillColorPopover setValue:@(YES) forKey:@"shouldHideAnchor"];
    [_fillColorPopover showRelativeToRect:NSMakeRect(_fillColorView.bounds.origin.x, 57, _fillColorView.bounds.size.width, _fillColorView.bounds.size.height) ofView:_fillColorView preferredEdge:NSRectEdgeMinY];
    vc.type = AnnotationOpacityTypeOpacity;

    __block typeof(self) blockSelf = self;
    vc.callback = ^(CGFloat opacity) {
        [blockSelf updateFillColorPickerVC];
        [blockSelf updateAnnotation];
    };
}

- (IBAction)showBorderOpacityView:(id)sender {
    KMOpacityPanel *vc = [[KMOpacityPanel alloc] initWithNibName:@"KMOpacityPanel" bundle:nil];
    vc.annotationModel = self.annotationModel;
    if (!_borderColorPopover) {
        _borderColorPopover = [[NSPopover alloc] init];
        _borderColorPopover.delegate = self;
    }
    _borderColorPopover.contentViewController = vc;
    _borderColorPopover.animates = YES;
    _borderColorPopover.behavior = NSPopoverBehaviorSemitransient;
    [_borderColorPopover setValue:@(YES) forKey:@"shouldHideAnchor"];
    [_borderColorPopover showRelativeToRect:NSMakeRect(_borderColorView.bounds.origin.x, 47, _borderColorView.bounds.size.width, _borderColorView.bounds.size.height) ofView:_borderColorView preferredEdge:NSRectEdgeMinY];
    if (_annotationModel.annotations.count > 0) {
        CPDFAnnotation *firstAnnotation = _annotationModel.annotations.firstObject;
        if ([firstAnnotation isKindOfClass:[CPDFLineAnnotation class]]) {
            vc.type = AnnotationOpacityTypeInteriorOpacity;
        } else {
            vc.type = AnnotationOpacityTypeOpacity;
        }
    } else {
        if (_annotationModel.annotationType == CAnnotationTypeLine ||
            _annotationModel.annotationType == CAnnotationTypeArrow) {
            vc.type = AnnotationOpacityTypeInteriorOpacity;
        } else {
            vc.type = AnnotationOpacityTypeOpacity;
        }
    }
    __block typeof(self) blockSelf = self;
    vc.callback = ^(CGFloat opacity) {
        [blockSelf updateBorderColorPickerVC];
        [blockSelf updateAnnotation];
    };

}

- (void)createStampProperties {
    [self hiddenSubviews];
}

#pragma mark - UI Action

- (IBAction)opacitySliderAction:(id)sender
{
    CGFloat opcity = self.opacitySlider.floatValue;
    if (self.opacitySlider.floatValue == 0) {
        opcity = 0.0001;
    }
    
    CAnnotationType annotationType = self.annotationModel.annotationType;
    if (CAnnotationTypeSignFalse == annotationType ||
        CAnnotationTypeSignTure == annotationType ||
        CAnnotationTypeSignDot == annotationType ||
        CAnnotationTypeSignCircle == annotationType ||
        CAnnotationTypeSignLine == annotationType) {
        self.annotationModel.opacity = opcity;
        [self updateFillColorPickerVC];
    } else if(CAnnotationTypeCircle == annotationType ||
              CAnnotationTypeSquare == annotationType){
        if(opcity !=0) {
            self.annotationModel.opacity = opcity;
            self.annotationModel.interiorOpacity= opcity;
        } else {
            self.annotationModel.color = nil;
            self.annotationModel.interiorColor= nil;
        }
        [self updateFillColorPickerVC];
        [self updateBorderColorPickerVC];
    } else if (CAnnotationTypeFreeText == annotationType ||
               CAnnotationTypeSignDate == annotationType ||
               CAnnotationTypeSignText == annotationType ) {
        self.annotationModel.opacity = opcity;
        [self updateBackgroundColorPickerVC];
        NSColor *color = self.annotationModel.fontColor;
        if (self.annotationModel.opacity != 0) {
            CGFloat red,green,blue,alpha;
            [[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
            color = [NSColor colorWithRed:red green:green blue:blue alpha:self.annotationModel.interiorOpacity];
        } else {
            color = [NSColor clearColor];
        }
        self.fontColorButton.color = color;
        [self.fontColorButton setNeedsDisplay:YES];
    } else {
        self.annotationModel.opacity = opcity;
        [self updateFillColorPickerVC];
    }
    
    [self updateOpacityView];
    [self updateAnnotation];
}

- (IBAction)borderOpacitySliderAction:(id)sender
{
    CGFloat opcity = self.borderOpacitySlider.floatValue;
    if (self.borderOpacitySlider.floatValue == 0) {
        opcity = 0.0001;
    }
    
    CAnnotationType annotationType = self.annotationModel.annotationType;
    if(CAnnotationTypeCircle == annotationType ||
       CAnnotationTypeSquare == annotationType){
        if(opcity !=0) {
            self.annotationModel.opacity = opcity;
        } else {
            self.annotationModel.color = nil;
            self.annotationModel.interiorColor= nil;
        }
        [self updateFillColorPickerVC];
        [self updateBorderColorPickerVC];
    }
    
    [self updateBorderOpacityView];
    [self updateAnnotation];
}


- (IBAction)opacityComboBoxAction:(id)sender
{
    [self.opacitySlider setFloatValue:(CGFloat)[self.opacityComboBox intValue]/100];
    CGFloat opcity = self.opacitySlider.floatValue;
    if (self.opacitySlider.floatValue == 0) {
        opcity = 0.0001;
    }
    CAnnotationType annotationType = self.annotationModel.annotationType;
    if (CAnnotationTypeSignFalse == annotationType ||
        CAnnotationTypeSignTure == annotationType ||
        CAnnotationTypeSignDot == annotationType ||
        CAnnotationTypeSignCircle == annotationType ||
        CAnnotationTypeSignLine == annotationType) {
        self.annotationModel.opacity = opcity;
        [self updateFillColorPickerVC];
    } else if(CAnnotationTypeCircle == annotationType ||
              CAnnotationTypeSquare == annotationType){
        if(opcity !=0) {
            self.annotationModel.opacity = opcity;
            self.annotationModel.interiorOpacity= opcity;
        } else {
            self.annotationModel.color = nil;
            self.annotationModel.interiorColor= nil;
    
        }
        [self updateFillColorPickerVC];
        [self updateBorderColorPickerVC];
    } else if (CAnnotationTypeFreeText == annotationType ||
               CAnnotationTypeSignDate == annotationType ||
               CAnnotationTypeSignText == annotationType ) {
        self.annotationModel.interiorOpacity = opcity;
        [self updateBackgroundColorPickerVC];
    } else {
        self.annotationModel.opacity = opcity;
        [self updateFillColorPickerVC];
    }
    
    [self updateOpacityView];
    [self updateAnnotation];
}

- (IBAction)borderWidthSliderAction:(id)sender
{
    self.annotationModel.lineWidth = self.borderWidthSlider.floatValue;
    [self updateBorderWidthView];
    [self updateAnnotation];
}

- (IBAction)leftAlignButtonAction:(NSBox *)sender {
    [self updateAnnotation];

    NSTextAlignment alignment = NSTextAlignmentLeft;
    self.annotationModel.alignment = alignment;
    [self alignmentTypeSelected:alignment];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"KMAnnotationAlignmentTypeNotification" object:[NSString stringWithFormat:@"%ld",alignment]];
    
    [self updateAnnotation];
}

- (IBAction)centerAlignButtonAction:(NSBox *)sender {
    [self updateAnnotation];
    
    NSTextAlignment alignment = NSTextAlignmentCenter;
    self.annotationModel.alignment = alignment;
    [self alignmentTypeSelected:alignment];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"KMAnnotationAlignmentTypeNotification" object:[NSString stringWithFormat:@"%ld",alignment]];
    
    [self updateAnnotation];
}

- (IBAction)rightAlignButtonAction:(NSBox *)sender {
    [self updateAnnotation];
    
    NSTextAlignment alignment = NSTextAlignmentRight;
    self.annotationModel.alignment = alignment;
    [self alignmentTypeSelected:alignment];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"KMAnnotationAlignmentTypeNotification" object:[NSString stringWithFormat:@"%ld",alignment]];
    
    [self updateAnnotation];
}

- (IBAction)justifiedAlignButtonAction:(NSBox *)sender {
    [self updateAnnotation];

    NSTextAlignment alignment = NSTextAlignmentJustified;
    self.annotationModel.alignment = alignment;
    [self alignmentTypeSelected:alignment];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"KMAnnotationAlignmentTypeNotification" object:[NSString stringWithFormat:@"%ld",alignment]];
    
    [self updateAnnotation];
}

- (IBAction)currentFontColorButtonAction:(NSButton *)sender {
    KMAnnotationFontWindowController *fontWindowController = [KMAnnotationFontWindowController sharedAnnotationFont];
    fontWindowController.PDFListView = self.pdfView;
    NSWindow *window = [fontWindowController window];
    fontWindowController.annotationModel = self.annotationModel;
    __block typeof(self) blockSelf = self;

    fontWindowController.annotationAlignCallback = ^(NSInteger selectedCount) {
        NSTextAlignment textAlignment = NSTextAlignmentLeft;
        if (selectedCount == 0) {
            textAlignment = NSTextAlignmentLeft;
        } else if (selectedCount == 2) {
            textAlignment = NSTextAlignmentCenter;
        } else if (selectedCount == 1) {
            textAlignment = NSTextAlignmentRight;
        } else if (selectedCount == 3) {
            textAlignment = NSTextAlignmentJustified;
        }
        [blockSelf alignmentTypeSelected:selectedCount];
        if(blockSelf.annotationModel) {
            blockSelf.annotationModel.alignment = textAlignment;
            [blockSelf updateAnnotation];
        }
    };
    fontWindowController.annotationCallback = ^(CPDFAnnotation *annotation) {
        [blockSelf updateFontView];
        [blockSelf updateAnnotation];
    };
    [window orderFront:sender];
}

- (IBAction)fontCustomColorButtonAction:(NSButton *)sender {
    [[NSColorPanel sharedColorPanel] setTarget:self];
    [NSColorPanel sharedColorPanel].showsAlpha = YES;
    [[NSColorPanel sharedColorPanel] setAction:@selector(colorPanelAction:)];
    [[NSColorPanel sharedColorPanel] orderFront:nil];
}

- (IBAction)fontColorButtonAction:(NSButton *)sender {
    self.annotationModel.fontColor = _fontColorButton.color;
    [self updateAnnotation];
}

- (NSColor *)colorWithCGColor:(CGColorRef)CGColor
{
    if (CGColor == NULL) return nil;
    return [NSColor colorWithCIColor:[CIColor colorWithCGColor:CGColor]];
}

- (void)colorPanelAction:(id)sender {
    if([NSColorPanel sharedColorPanel].visible) {
        NSColor *currentColor = [NSColorPanel sharedColorPanel].color;
        _fontColorButton.color = currentColor;
        [self.fontColorButton setNeedsDisplay:YES];
        
        CGFloat r, g, b, a = 0.0;
        [[currentColor colorUsingColorSpaceName:NSDeviceRGBColorSpace] getRed:&r green:&g blue:&b alpha:&a];
        if (fabs(a - 0) < 0.001) {
            //设置透明色必须要设一个颜色值
            self.annotationModel.opacity = 0;
            self.annotationModel.fontColor = [NSColor blackColor];
        } else {
            self.annotationModel.opacity = a;
            self.annotationModel.fontColor = currentColor;
        }
        
        [self updateOpacityView];
        if (CAnnotationTypeFreeText == _annotationType ||
            CAnnotationTypeSignDate == _annotationType ||
            CAnnotationTypeSignText == _annotationType) {
            [[self.annotationModel.fontColor colorUsingColorSpaceName:NSDeviceRGBColorSpace] getRed:&r green:&g blue:&b alpha:&a];

            self.fontColorButton.color = [NSColor colorWithRed:r green:g blue:b alpha:self.annotationModel.opacity];
            [_fontButton setNeedsDisplay:YES];
        }
        CPDFAnnotation *annotation = self.annotationModel.annotation;
        if (annotation) {
            if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
                CPDFFreeTextAnnotation *textNote = (CPDFFreeTextAnnotation *)annotation;
                if ([self.pdfView isEditWithCurrentFreeText:textNote]) {
                    [self.pdfView setEditAnnotationFreeTextColor:currentColor freeText:textNote];
                } else {
                    [self updateAnnotation];
                }
            } else {
                [self updateAnnotation];
            }
            self.generalImageView.image = [self.annotationModel annotationImage];
        } else {
            [self updateAnnotation];
        }

    }
}

- (IBAction)buttonClicked_TypeAwitch:(NSButton *)sender {
    NSInteger tag = sender.tag;
    CAnnotationType annotationType = CAnnotationTypeSignLine;
    if (tag == 201) {
        annotationType = CAnnotationTypeSignTure;
    } else if (tag == 202) {
        annotationType = CAnnotationTypeSignFalse;
    } else {
        annotationType = tag;
    }
    
    self.selfSignType = annotationType;
    [self typeSelected:annotationType];
    if (self.callBack) {
        self.callBack (annotationType);
    }
        
    self.annotationModel.stampAnnotationType = annotationType;
    
    if (self.selfSignType == CAnnotationTypeSignDot) {
        self.borderWidthView.hidden = YES;
        self.borderWidthViewTopConstraint.constant = -(self.borderWidthView.bounds.size.height);
    } else {
        self.borderWidthView.hidden = NO;
        self.borderWidthViewTopConstraint.constant = 16.0f;
    }
    
    [self updateAnnotation];
}

- (IBAction)buttonClicked_SwitchIncludeTime:(NSButton *)sender
{
    self.annotationModel.includeTime = sender.state == NSControlStateValueOn?YES:NO;
    [self updateDateView];
    [self updateAnnotation];
}

- (IBAction)dateCheckButtonAction:(NSPopUpButton *)sender
{
    self.annotationModel.dateFormatIndex = sender.indexOfSelectedItem;
    [self updateAnnotation];
}

#pragma mark SqureAnnotation Style

- (IBAction)updateFigurateModelAction:(NSButton *)sender {
    if ((sender == self.squareVC.button && _annotationType == CAnnotationTypeSquare) ||
        (sender == self.circleVC.button && _annotationType == CAnnotationTypeCircle) ||
        (sender == self.arrowVC.button && _annotationType == CAnnotationTypeArrow) ||
        (sender == self.lineVC.button && _annotationType == CAnnotationTypeLine)) {
        return;
    }
    self.squareVC.state = KMDesignTokenStateNorm;
    self.circleVC.state = KMDesignTokenStateNorm;
    self.arrowVC.state = KMDesignTokenStateNorm;
    self.lineVC.state = KMDesignTokenStateNorm;
    if (sender == self.squareVC.button) {
        self.pdfView.annotationType = CAnnotationTypeSquare;
        self.squareVC.state = KMDesignTokenStateSel;
    } else if (sender == self.circleVC.button) {
        self.pdfView.annotationType = CAnnotationTypeCircle;
        self.circleVC.state = KMDesignTokenStateSel;
    } else if (sender == self.arrowVC.button) {
        self.pdfView.annotationType = CAnnotationTypeArrow;
        self.arrowVC.state = KMDesignTokenStateSel;
    } else if (sender == self.lineVC.button) {
        self.pdfView.annotationType = CAnnotationTypeLine;
        self.lineVC.state = KMDesignTokenStateSel;
    }
    
    self.annotationModel = [[CPDFAnnotationModel alloc] initWithAnnotationType:self.pdfView.annotationType];
    [self loadingUIAndLocalization];
}


#pragma mark CPDFTextAnnotation Action

- (IBAction)textAnnotationStyleAction:(NSButton *)sender {
    [self.annotationModel setAnchoredIconType:sender.tag];
    [self loadingUIAndLocalization];
    
    [self updateAnnotation];
}

#pragma mark Font、FontSize Action

- (IBAction)defaultFontPopUpButtonAction:(id)sender {
    NSDictionary *model = [[CPDFAnnotationModel defaultFreeTextPropert] objectAtIndex:self.defaultPopUpButton.indexOfSelectedItem];
    self.annotationModel.fontName = model[@"fontName"];
    self.annotationModel.fontSize = [model[@"fontSize"] floatValue];
    [self updateFillColorPickerVC];
    [self updateFontView];
    [self updateAnnotation];
    [self createFreeTextProperties];
}

- (IBAction)resetDefaultAction:(id)sender {
    NSMenu *menu = [[NSMenu alloc] init];
    if (self.defaultPopUpButton.indexOfSelectedItem != 0) {
        [menu addItemWithTitle:NSLocalizedString(@"Redefine", nil)  action:@selector(updateCurrentDefaultFont:) target:self tag:0];
        [menu addItemWithTitle:NSLocalizedString(@"Reset", nil)  action:@selector(updateCurrentDefaultFont:) target:self tag:1];
    } else {
        [menu addItemWithTitle:NSLocalizedString(@"Redefine", nil)  action:nil target:self tag:0];
        [menu addItemWithTitle:NSLocalizedString(@"Reset", nil)  action:nil target:self tag:1];
    }
    [menu popUpMenuPositioningItem:nil atLocation:NSMakePoint(self.view.frame.size.width-100, CGRectGetMinY(self.defaultFontView.frame)-7) inView:self.mianView];
}

#pragma mark - NSNotification

- (void)textDidChange:(NSNotification *)notification {
    NSObject *obj = notification.object;
    if ([obj isEqualTo:self.noteTextView]) {
        [self.annotationModel setContents:self.noteTextView.string?:@""];
        if (CAnnotationTypeHighlight == self.annotationModel.annotationType || CAnnotationTypeStrikeOut == self.annotationModel.annotationType || CAnnotationTypeUnderline == self.annotationModel.annotationType) {
        } else {
            [self updateAnnotation];
        }
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (![object isEqual:self.annotationModel.annotation]) {
        return;
    }
    id newValue = [change objectForKey:NSKeyValueChangeNewKey] ? : [NSNull null];
    id oldValue = [change objectForKey:NSKeyValueChangeOldKey] ? : [NSNull null];
    if ([newValue isEqual:oldValue]) {
        return;
    }
    if ([keyPath isEqualToString:@"contents"] || [keyPath isEqualToString:@"markupText"]) {
        if ([newValue isKindOfClass:[NSNull class]]) {
            return;
        }
        self.noteTextView.string = newValue ? : @"";
    }
}

- (void)alignmentTypeNotification:(NSNotification *)notification {
    NSString *count = notification.object;
    
    NSTextAlignment alignment = NSTextAlignmentLeft;
    if ([count isEqualToString:@"0"]) {
        alignment = NSTextAlignmentLeft;
    } else if ([count isEqualToString:@"1"]) {
        alignment = NSTextAlignmentRight;
    } else if ([count isEqualToString:@"2"]) {
        alignment = NSTextAlignmentCenter;
    }
    [self alignmentTypeSelected:alignment];
}

- (void)annotationChangeNotification:(NSNotification *)notification {
    CPDFAnnotation *annotation = notification.object;
    if ([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
        [self configureUIView];
    }
}

#pragma mark - NSPopoverDelegate

- (void)popoverWillShow:(NSNotification *)notification {
    NSPopover *popover = notification.object;
    if ([popover isEqual:_fillColorPopover]) {
        self.fillButton.wantsLayer = YES;
        self.fillButton.layer.cornerRadius = 4.0;
        self.fillButton.layer.backgroundColor = [NSColor colorWithRed:206/255.0 green:208/255.0 blue:212/255.0 alpha:1.0].CGColor;
    } else if ([popover isEqual:_borderColorPopover]) {
        self.showborderOpacityButton.wantsLayer = YES;
        self.showborderOpacityButton.layer.cornerRadius = 4.0;
        self.showborderOpacityButton.layer.backgroundColor = [NSColor colorWithRed:206/255.0 green:208/255.0 blue:212/255.0 alpha:1.0].CGColor;
    }
}

- (void)popoverWillClose:(NSNotification *)notification {
    NSPopover *popover = notification.object;
    if ([popover isEqual:_fillColorPopover]) {
        self.fillButton.wantsLayer = YES;
        self.fillButton.layer.cornerRadius = 0.0;
        self.fillButton.layer.backgroundColor = [NSColor clearColor].CGColor;
    } else if ([popover isEqual:_borderColorPopover]) {
        self.showborderOpacityButton.wantsLayer = YES;
        self.showborderOpacityButton.layer.cornerRadius = 0.0;
        self.showborderOpacityButton.layer.backgroundColor = [NSColor clearColor].CGColor;
    }
}

#pragma mark - KMSelectPopButtonDelegate

- (void)km_comboBoxSelectionDidChange:(KMDesignSelect *)obj {
    if ([self.fontNameVC isEqual:obj]) {
        NSArray *fonts = [CPDFAnnotationModel supportFonts];
        NSString *familyString = self.fontNameVC.stringValue;
        for (NSDictionary *dict in fonts) {
            NSString * fontName = dict.allKeys.firstObject;
            if ([fontName isEqualToString:familyString]) {
                [self.fontStyleVC removeAllItems];
                [self.fontStyleVC addItemsWithObjectValues:dict.allValues.firstObject];
                [self.fontStyleVC selectItemAt:0];
            }
        }
                    
        NSString *styleString = self.fontStyleVC.stringValue;
        if ([styleString isEqualToString:@"Regular"] || [styleString isEqualToString:@"Roman"]) {
            [_annotationModel setFontName:[NSString stringWithFormat:@"%@", familyString]];
        } else {
            [_annotationModel setFontName:[NSString stringWithFormat:@"%@-%@", familyString, [styleString stringByReplacingOccurrencesOfString:@" " withString:@""]]];
        }

        [self updateFontView];
        [self adjustFreetText];
        [self updateAnnotation];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"KMAnnotationFontTypeNotification" object:self.pdfView];

    } else if ([self.fontStyleVC isEqual:obj]) {
        NSInteger index = self.fontStyleVC.indexOfSelectedItem;
        if (index < 0) {
            return;
        }
        
        NSString *familyString = self.fontNameVC.stringValue;
        NSString *styleString = self.fontStyleVC.stringValue;
        if ([styleString isEqualToString:@"Regular"] || [styleString isEqualToString:@"Roman"]) {
            [_annotationModel setFontName:[NSString stringWithFormat:@"%@", familyString]];
        } else {
            [_annotationModel setFontName:[NSString stringWithFormat:@"%@-%@", familyString, [styleString stringByReplacingOccurrencesOfString:@" " withString:@""]]];
        }
        [self updateFontView];
        [self adjustFreetText];
        [self updateAnnotation];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"KMAnnotationFontTypeNotification" object:self.pdfView];
    } else if (self.fontSizeVC == obj) {
        NSInteger index = self.fontSizeVC.indexOfSelectedItem;
        if (index < 0) {
            return;
        }
        NSString *size = [self.fontSizeVC.stringValue stringByReplacingOccurrencesOfString:@"pt" withString:@""];
        [self.annotationModel setFontSize:[size floatValue]];
        [self adjustFreetText];
        [self updateAnnotation];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"KMAnnotationFontTypeNotification" object:self.pdfView];
    } else if (self.borderWidthVC == obj) {
        CGFloat lineWidth = [[self.borderWidthVC.stringValue stringByReplacingOccurrencesOfString:@"pt" withString:@""] floatValue];
        if (self.fillColorPickerVC.annotationType == KMPropertiesColor_SelfSignColors) {
            if (lineWidth > 8) {
                lineWidth = 8;
            }
        } else {
            if (_annotationType != CAnnotationTypeEraser) {
                if (lineWidth > 20) {
                    lineWidth = 20;
                }
            }
        }
        
        self.annotationModel.lineWidth = lineWidth;
        [self updateBorderWidthView];
        [self updateAnnotation];

    }
}

-(void)adjustFreetText {
    if(self.annotationModel.annotation &&
       (CAnnotationTypeFreeText == self.annotationModel.annotationType ||
        CAnnotationTypeSignText == self.annotationModel.annotationType ||
        CAnnotationTypeSignDate == self.annotationModel.annotationType )) {
        for(CPDFAnnotation *an in self.annotationModel.annotations) {
            if([an isKindOfClass:[CPDFFreeTextAnnotation class]]) {
                CPDFFreeTextAnnotation *freeTextAn = (CPDFFreeTextAnnotation *)an;
                NSFont* font = freeTextAn.font;
                NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
                [dictionary setObject:font forKey:NSFontAttributeName];
                NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
                [style setAlignment:freeTextAn.alignment];
                [dictionary setObject:style forKey:NSParagraphStyleAttributeName];
                
                CGSize textViewSize = [freeTextAn.contents?:@"" boundingRectWithSize:CGSizeMake(freeTextAn.bounds.size.width, MAXFLOAT)
                                                                         options:NSStringDrawingUsesLineFragmentOrigin
                                                                      attributes:dictionary].size;
                
                CGRect rect = freeTextAn.bounds;
                if(textViewSize.height != freeTextAn.bounds.size.height) {
                    rect.origin.y -= (textViewSize.height - rect.size.height);
                    rect.size.height = textViewSize.height;
                }
                
                if(textViewSize.width < freeTextAn.bounds.size.width) {
                    rect.size.width = textViewSize.width;
                }
                
                freeTextAn.bounds = rect;
            }
        }
    }
}

- (void)km_controlTextDidEndEditing:(KMDesignSelect *)obj {
    
}

@end