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

#import "KMAnnotationPropertiesViewController.h"
//#import "NSString_SKExtensions.h"
#import <PDF_Reader_Pro-Swift.h>
#import "KMSignatureAnnotationViewController.h"
#import "CSelfSignAnnotation.h"
#import "CSelfSignAnnotationFreeText.h"

#define KMFromContentButtonHeightFloat 32

@interface KMClipView : NSClipView

@end

@implementation KMClipView

- (BOOL)isFlipped {
    return YES;
}

@end

@interface KMAnnotationPropertiesViewController ()

@property (assign) IBOutlet NSBox *propertiesBox;

@property (nonatomic,assign) IBOutlet NSView *gradientView;

@property (weak) IBOutlet NSImageView *emptyImageView;
@property (nonatomic,assign) IBOutlet NSTextField *titleLabel;

@property (nonatomic,assign) IBOutlet NSTextField *subTitleLabel;

@property (nonatomic,assign) IBOutlet NSView *emptyView;

@property (nonatomic,retain) NSViewController *contentViewController;
@property (weak) IBOutlet NSLayoutConstraint *titleViewHeightConstraint;
@property (weak) IBOutlet NSLayoutConstraint *buttonbuttonLayoutConstraint;

@property (assign) IBOutlet NSView *buttonView;
@property (assign) IBOutlet NSButton *formGeneralButton;
@property (assign) IBOutlet NSButton *formAppearanceButton;
@property (assign) IBOutlet NSButton *formOptionsButton;

@property (assign) IBOutlet NSView *tipsView;
@property (assign) IBOutlet NSTextField *tipsLabel;
@property (assign) IBOutlet NSImageView *oneLineImageView;
@property (assign) IBOutlet NSImageView *lastLineImageView;

@property (nonatomic, retain) NSButton *clickBtn;

@end

@implementation KMAnnotationPropertiesViewController


#pragma mark - View Methods

//func addNotification() {
//    NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil)
//}
//
//func removeNotification() {
//    NotificationCenter.default.removeObserver(self)
//    DistributedNotificationCenter.default().removeObserver(self)
//}
//
//@objc func changeEffectiveAppearance() {
//    let isDarkModel = KMAdvertisementConfig.isDarkModel()
//    if isDarkModel {
//        self.appearance = NSAppearance(named: .darkAqua)
//    } else {
//        self.appearance = NSAppearance(named: .aqua)
//    }
//    self.updateUI()
//}

- (void)dealloc {
    [self removeNotification];
}

- (void)addNotification {
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(changeEffectiveAppearance) name:@"kEffectiveAppearance" object:nil];
}

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

- (void)changeEffectiveAppearance {
    BOOL isDarkModel = [KMAdvertisementConfig isDarkModel];
    if (isDarkModel) {
        self.view.appearance = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
        self.view.layer.backgroundColor = [NSColor km_initWithHex:@"#252526" alpha:1].CGColor;
    } else {
        self.view.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
        self.view.layer.backgroundColor = [NSColor km_initWithHex:@"#FAFAFA" alpha:1].CGColor;
    }
}

- (void)loadView {
    [super loadView];
    
    [self addNotification];
    
    self.view.wantsLayer = YES;
    self.view.layer.backgroundColor = [KMAppearance viewBackgroundColor].CGColor;
//    self.view.layer.backgroundColor = [NSColor colorWithRed:247.0/255.0 green:248.0/255.0 blue:250.0/255.0 alpha:1].CGColor;
    self.view.layer.shadowColor = [NSColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.15].CGColor;
    self.view.layer.shadowOffset = NSMakeSize(0, 0);
    self.view.layer.shadowRadius = 4;
    
    self.emptyImageView.image = [NSImage imageNamed:@"KMImageNameMarkupEmpty"];
    self.subTitleLabel.stringValue = NSLocalizedString(@"Show/Hide Annotation Properties Panel", nil);
    NSMutableParagraphStyle *ps = [[NSMutableParagraphStyle alloc] init];
    ps.lineSpacing = 10;
    ps.alignment = NSTextAlignmentCenter;
    self.subTitleLabel.attributedStringValue = [[NSAttributedString alloc] initWithString:self.subTitleLabel.stringValue attributes:@{
        NSForegroundColorAttributeName : [NSColor colorWithRed:148/255.f green:152/255.f blue:156/255.f alpha:1.f],
        NSFontAttributeName : [NSFont fontWithName:@"SFProText-Regular" size:12],
        NSParagraphStyleAttributeName : ps}];
    self.subTitleLabel.textColor = [NSColor grayColor];
    
    self.titleLabel.font = [NSFont SFProTextSemiboldFont:14.0];
    self.titleLabel.textColor = [KMAppearance KMColor_Layout_H0];
//    [NSColor colorWithRed:0.145 green:0.149 blue:0.161 alpha:1];
    
    self.buttonView.hidden = YES;
    self.formGeneralButton.wantsLayer = YES;
    self.formAppearanceButton.wantsLayer = YES;
    self.formOptionsButton.wantsLayer = YES;
    [self updateFormContentButtonUI:self.formAppearanceButton];
    self.formAppearanceButton.toolTip = NSLocalizedString(@"Appearance", nil);
    [self.formAppearanceButton setTitle:NSLocalizedString(@"Appearance", nil)];
    self.formGeneralButton.toolTip = NSLocalizedString(@"General", nil);
    [self.formGeneralButton setTitle:NSLocalizedString(@"General", nil)];
    self.formOptionsButton.toolTip = NSLocalizedString(@"Preferences", nil);
    [self.formOptionsButton setTitle:NSLocalizedString(@"Preferences", nil)];
    
    self.tipsView.hidden = YES;
    self.tipsView.wantsLayer = YES;
    self.tipsView.layer.cornerRadius = 1.0;
    
    self.oneLineImageView.wantsLayer = YES;
    self.lastLineImageView.wantsLayer = YES;
    
    self.oneLineImageView.hidden = YES;
}

#pragma mark - Setter Methods
-(void)setAnnotations:(NSArray *)annotations
{
    if (_annotations != annotations) {
        _annotations = annotations;
    }
    
    self.buttonView.hidden = YES;
    CPDFAnnotation *annotation = _annotations.firstObject;
    if (self.contentViewController) {
        [self.contentViewController.view removeFromSuperview];
        self.contentViewController = nil;
    }
    if (!annotation || [annotation isKindOfClass:[CPDFRedactAnnotation class]]) {
        return;
    }

    if (_annotations.count > 1) {
        if ([annotation isKindOfClass:[CPDFTextWidgetAnnotation class]] ||
            [annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]] ||
            [annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]]) {
            
        } else {
            [self selectAnnotations:_annotations];
            return;
        }
    }
    
    if ([annotation isKindOfClass:[CPDFTextWidgetAnnotation class]]){
        self.titleLabel.stringValue = NSLocalizedString(@"Text Field", nil);
    } else if ([annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]]) {
        CPDFButtonWidgetAnnotation *buttonWidget = (CPDFButtonWidgetAnnotation *)annotation;
        if (buttonWidget.controlType == 1) {
            self.titleLabel.stringValue = NSLocalizedString(@"Radio Button", nil);
        } else if (buttonWidget.controlType == 2) {
            self.titleLabel.stringValue = NSLocalizedString(@"Check Box", nil);
        } else if (buttonWidget.controlType == 0) {
            self.titleLabel.stringValue = NSLocalizedString(@"Button", nil);
        }
    } else if ([annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]) {
        CPDFChoiceWidgetAnnotation *choiceWidget = (CPDFChoiceWidgetAnnotation *)annotation;
        if (choiceWidget.isListChoice) {
            self.titleLabel.stringValue = NSLocalizedString(@"List Box", nil);
        } else {
            self.titleLabel.stringValue = NSLocalizedString(@"Combo Box", nil);
        }
    } else if ([annotation isKindOfClass:[KMSelfSignAnnotation class]]) {
        KMSelfSignAnnotation *newAnnotation = (KMSelfSignAnnotation *)annotation;
        CAnnotationType type = newAnnotation.annotationType;
        NSString *returnString = @"";
        if (type == CAnnotationTypeSignFalse) {
            returnString = NSLocalizedString(@"X", nil);
        } else if (type == CAnnotationTypeSignTure) {
            returnString = NSLocalizedString(@"Check mark", nil);
        } else if (type == CAnnotationTypeSignCircle) {
            returnString = NSLocalizedString(@"Circle", nil);
        } else if (type == CAnnotationTypeSignLine) {
            returnString = NSLocalizedString(@"Line", nil);
        } else if (type == CAnnotationTypeSignDot) {
            returnString = NSLocalizedString(@"Dot", nil);
        } else if (type == CAnnotationTypeSignText) {
            returnString = NSLocalizedString(@"Text", nil);
        }
        self.titleLabel.stringValue = returnString;
    } else if ([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
        self.titleLabel.stringValue = NSLocalizedString(@"Link", @"Description for export");
    } else if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
        if ([annotation isKindOfClass:[KMSelfSignAnnotationFreeText class]]) {
            KMSelfSignAnnotationFreeText *signFreeText = (KMSelfSignAnnotationFreeText *)annotation;
            if (signFreeText.subType == KMSelfSignAnnotationFreeTextSubTypeProfile) {
                self.titleLabel.stringValue = NSLocalizedString(@"Text", nil);
            } else {
                self.titleLabel.stringValue = NSLocalizedString(@"Date", nil);
            }
        } else {
            self.titleLabel.stringValue = [annotation type].typeName;
        }
    } else if ([annotation isKindOfClass:[CPDFSignatureWidgetAnnotation class]] ||
               [annotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
        self.titleLabel.stringValue = NSLocalizedString(@"Signature", nil);
    } else if ([annotation isKindOfClass:[CSelfSignAnnotation class]]) {
        CSelfSignAnnotation *newAnnotation = (CSelfSignAnnotation *)annotation;
        if (newAnnotation.annotationType == CAnnotationTypeSignFalse) {
            self.titleLabel.stringValue = NSLocalizedString(@"Fork", nil);
        } else if (newAnnotation.annotationType == CAnnotationTypeSignTure) {
            self.titleLabel.stringValue = NSLocalizedString(@"Hook", nil);
        } else if (newAnnotation.annotationType == CAnnotationTypeSignCircle) {
            self.titleLabel.stringValue = NSLocalizedString(@"Rectangle", nil);
        } else if (newAnnotation.annotationType == CAnnotationTypeSignLine) {
            self.titleLabel.stringValue = NSLocalizedString(@"Line", nil);
        } else if (newAnnotation.annotationType == CAnnotationTypeSignDot) {
            self.titleLabel.stringValue = NSLocalizedString(@"Dot", nil);
        } else if (newAnnotation.annotationType == CAnnotationTypeSignText) {
            self.titleLabel.stringValue = NSLocalizedString(@"Text", nil);
        } else if (newAnnotation.annotationType == CAnnotationTypeSignDate) {
            self.titleLabel.stringValue = NSLocalizedString(@"Date", nil);
        }
    } else if ([annotation isKindOfClass:[CPDFStampAnnotation class]]) {
        self.titleLabel.stringValue = NSLocalizedString(@"Stamp", nil);
    } else if ([annotation isKindOfClass:[KMTableAnnotation class]]) {
        self.titleLabel.stringValue = NSLocalizedString(@"Table", nil);
    } else {
        self.titleLabel.stringValue = [annotation type].typeName;
    }
    self.titleViewHeightConstraint.constant = 40;

    CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc] initWithPDFAnnotations:_annotations];
    if ([annotation isKindOfClass:[CPDFTextWidgetAnnotation class]] ||
        [annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]] ||
        [annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]]){
        self.buttonView.hidden = NO;
        if ([annotation isKindOfClass:[CPDFTextWidgetAnnotation class]]) {
            [self updateFormContentButtonUI:_formGeneralButton];
            KMAnnotationGeneralViewController *vc = [[KMAnnotationGeneralViewController alloc] init];
            vc.pdfview = self.pdfView;
            vc.annotationModel = annotationModel;
            vc.view.frame = [self viewControllerRect];
            [self.view addSubview:vc.view];
            self.contentViewController = vc;
        } else if ([annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]) {
            [self fromContentButtonAction:_formOptionsButton];
        } else if ([annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]]) {
            CPDFButtonWidgetAnnotation  *newAnnotation = (CPDFButtonWidgetAnnotation *)annotation;
            if (newAnnotation.controlType == CPDFWidgetRadioButtonControl || newAnnotation.controlType == CPDFWidgetCheckBoxControl) {
                [self updateFormContentButtonUI:_formGeneralButton];
                KMAnnotationGeneralViewController *vc = [[KMAnnotationGeneralViewController alloc] init];
                vc.pdfview = self.pdfView;
                vc.annotations = self.annotations;
                vc.view.frame = [self viewControllerRect];
                [self.view addSubview:vc.view];
                self.contentViewController = vc;
            } else if (newAnnotation.controlType == CPDFWidgetPushButtonControl) {
                [self fromContentButtonAction:_formOptionsButton];
            }
        }
    } else if ([annotation isKindOfClass:[KMAnnotationFromSignature class]]) {
        KMAnnotationGeneralViewController *vc = [[KMAnnotationGeneralViewController alloc] init];
        vc.pdfview = self.pdfView;
        vc.annotationModel = annotationModel;
        vc.view.frame = [self viewControllerRect];
        [self.view addSubview:vc.view];
        self.contentViewController = vc;
    } else if ([annotation isKindOfClass:[CPDFSignatureWidgetAnnotation class]]) {
        KMFillSignShapePanel *vc = [[KMFillSignShapePanel alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        self.propertiesBox.contentView = vc.view;
        self.contentViewController = vc;
        vc.callback = ^(CAnnotationType type) {
            if (type == CAnnotationTypeSignFalse) {
                self.titleLabel.stringValue = NSLocalizedString(@"Fork", nil);
            } else if (type == CAnnotationTypeSignTure) {
                self.titleLabel.stringValue = NSLocalizedString(@"Hook", nil);
            } else if (type == CAnnotationTypeSignCircle) {
                self.titleLabel.stringValue = NSLocalizedString(@"Rectangle", nil);
            } else if (type == CAnnotationTypeSignLine) {
                self.titleLabel.stringValue = NSLocalizedString(@"Line", nil);
            } else if (type == CAnnotationTypeSignDot) {
                self.titleLabel.stringValue = NSLocalizedString(@"Dot", nil);
            }
        };
    } else if ([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
        CPDFLinkAnnotation *linkAnnotation = annotations.firstObject;
        NSString *contents = nil;
        if ([linkAnnotation URL]) {
            contents = [linkAnnotation URL];
            if (contents) {
                contents = [NSString stringWithFormat:@"1%@", contents];
            } else {
                contents = @"";
            }
        } else {
            contents = [NSString stringWithFormat:@"0%@", @([[linkAnnotation destination] pageIndex]+1)];
        }
        
        KMAnnotationSelectLinkViewController *vc = [[KMAnnotationSelectLinkViewController alloc] init];
        vc.annotations = self.annotations;
        vc.pdfDocument = self.pdfView.document;
        vc.pdfview = self.pdfView;
        vc.view.frame = [self viewControllerRect];
        [self.view addSubview:vc.view];
        self.contentViewController = vc;
    } else if ([annotation isKindOfClass:[CSelfSignAnnotation class]]) {
        CSelfSignAnnotation *newAnnotation = (CSelfSignAnnotation *)annotation;
        if (newAnnotation.annotationType == CAnnotationTypeSignFalse ||
            newAnnotation.annotationType == CAnnotationTypeSignTure ||
            newAnnotation.annotationType == CAnnotationTypeSignCircle ||
            newAnnotation.annotationType == CAnnotationTypeSignLine ||
            newAnnotation.annotationType == CAnnotationTypeSignDot) {
            KMAnnotationSelfSignViewController *vc = [[KMAnnotationSelfSignViewController alloc] init];
            vc.pdfView = self.pdfView;
            vc.annotationModel = annotationModel;
            self.propertiesBox.contentView = vc.view;
            self.contentViewController = vc;
            vc.callBack = ^(CAnnotationType type) {
                if (type == CAnnotationTypeSignFalse) {
                    self.titleLabel.stringValue = NSLocalizedString(@"Fork", nil);
                } else if (type == CAnnotationTypeSignTure) {
                    self.titleLabel.stringValue = NSLocalizedString(@"Hook", nil);
                } else if (type == CAnnotationTypeSignCircle) {
                    self.titleLabel.stringValue = NSLocalizedString(@"Rectangle", nil);
                } else if (type == CAnnotationTypeSignLine) {
                    self.titleLabel.stringValue = NSLocalizedString(@"Line", nil);
                } else if (type == CAnnotationTypeSignDot) {
                    self.titleLabel.stringValue = NSLocalizedString(@"Dot", nil);
                }
            };
        } else if (newAnnotation.annotationType == CAnnotationTypeSignText ||
                   newAnnotation.annotationType == CAnnotationTypeSignDate) {
            KMFillSignTextPanel *vc = [[KMFillSignTextPanel alloc] init];
            vc.pdfView = self.pdfView;
            vc.annotationModel = annotationModel;
            self.propertiesBox.contentView = vc.view;
            self.contentViewController = vc;
        }
    } else if ([annotation isKindOfClass:[CSelfSignAnnotationFreeText class]]) {
        CSelfSignAnnotationFreeText *newAnnotation = (CSelfSignAnnotationFreeText *)annotation;
        if (newAnnotation.subType == CAnnotationTypeSignText ||
            newAnnotation.subType == CAnnotationTypeSignDate) {
            KMFillSignTextPanel *vc = [[KMFillSignTextPanel alloc] init];
            vc.pdfView = self.pdfView;
            vc.annotationModel = annotationModel;
            self.propertiesBox.contentView = vc.view;
            self.contentViewController = vc;
        }
        
    } else if ([annotation isKindOfClass:[CPDFSignatureAnnotation class]] ||
               [annotation isKindOfClass:[CPDFStampAnnotation class]]) {
        KMAnnotationStampViewController *vc = [[KMAnnotationStampViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        self.propertiesBox.contentView = vc.view;
        self.contentViewController = vc;
    } else if ([annotation isKindOfClass:[KMTableAnnotation class]]) {
        KMAnnotationTableViewController *vc = [[KMAnnotationTableViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        self.propertiesBox.contentView = vc.view;
        self.contentViewController = vc;
    } else {
        KMGeneralAnnotationViewController *vc = [[KMGeneralAnnotationViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        self.propertiesBox.contentView = vc.view;
        self.contentViewController = vc;
    }
    
//    self.buttonbuttonLayoutConstraint.constant = self.buttonView.hidden ? -36 : 0;
}

- (void)selectAnnotations:(NSArray *)annotations {
    CPDFAnnotation *fristAnnotation = annotations.firstObject;

    CAnnotationType annotationMode = CAnnotationTypeUnkown;
    if ([fristAnnotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
        annotationMode = CAnnotationTypeHighlight;
    } else if ([fristAnnotation isKindOfClass:[CPDFInkAnnotation class]]) {
        annotationMode = CAnnotationTypeInk;
    } else if ([fristAnnotation isKindOfClass:[CPDFTextAnnotation class]]) {
        annotationMode = CAnnotationTypeAnchored;
    } else if ([fristAnnotation isKindOfClass:[CPDFSquareAnnotation class]]) {
        annotationMode = CAnnotationTypeSquare;
    } else if ([fristAnnotation isKindOfClass:[CPDFCircleAnnotation class]]) {
        annotationMode = CAnnotationTypeCircle;
    } else if ([fristAnnotation isKindOfClass:[CPDFLineAnnotation class]]) {
        if(((CPDFLineAnnotation *)fristAnnotation).startLineStyle == CPDFLineStyleNone &&
           ((CPDFLineAnnotation *)fristAnnotation).endLineStyle == CPDFLineStyleOpenArrow ) {
            annotationMode = CAnnotationTypeArrow;
        } else {
            annotationMode = CAnnotationTypeLine;
        }
    } else if ([fristAnnotation isKindOfClass:[CPDFLinkAnnotation class]]) {
        annotationMode = CAnnotationTypeLink;
    } else if ([fristAnnotation isKindOfClass:[CPDFStampAnnotation class]]) {
        if ([fristAnnotation isKindOfClass:[CSelfSignAnnotation class]]) {
            CSelfSignAnnotation *newAnnotation = (CSelfSignAnnotation *)fristAnnotation;
            if (newAnnotation.annotationType == CAnnotationTypeSignFalse)
                annotationMode = CAnnotationTypeSignFalse;
            else if(newAnnotation.annotationType == CAnnotationTypeSignTure)
                annotationMode = CAnnotationTypeSignTure;
            else if(newAnnotation.annotationType == CAnnotationTypeSignCircle)
                annotationMode = CAnnotationTypeSignCircle;
            else if(newAnnotation.annotationType == CAnnotationTypeSignLine)
                annotationMode = CAnnotationTypeSignLine;
            else if(newAnnotation.annotationType == CAnnotationTypeSignDot)
                annotationMode = CAnnotationTypeSignDot;
            else if(newAnnotation.annotationType == CAnnotationTypeSignDot)
                annotationMode = CAnnotationTypeSignDot;
        } else if ([fristAnnotation isKindOfClass:[CSelfSignAnnotationFreeText class]]) {
            CSelfSignAnnotationFreeText *newAnnotation = (CSelfSignAnnotationFreeText *)fristAnnotation;
            if (newAnnotation.subType == CAnnotationTypeSignText)
                annotationMode = CAnnotationTypeSignText;
            else if(newAnnotation.subType == CAnnotationTypeSignDate)
                annotationMode = CAnnotationTypeSignDate;
        } else {
            annotationMode = CAnnotationTypeStamp;
        }
    } else if ([fristAnnotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
        annotationMode = CAnnotationTypeSignature;
    } else if ([fristAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
        if ([fristAnnotation isKindOfClass:[CSelfSignAnnotationFreeText class]]) {
            CSelfSignAnnotationFreeText *newAnnotation = (CSelfSignAnnotationFreeText *)fristAnnotation;
            if (newAnnotation.subType == CAnnotationTypeSignText)
                annotationMode = CAnnotationTypeSignText;
            else if(newAnnotation.subType == CAnnotationTypeSignDate)
                annotationMode = CAnnotationTypeSignDate;
        } else {
            annotationMode = CAnnotationTypeFreeText;
        }
    }
    self.annotationMode = annotationMode;
}

- (void)setAnnotationMode:(CAnnotationType)annotationMode {
    _annotationMode = annotationMode;
    if (self.contentViewController) {
       [self.contentViewController.view removeFromSuperview];
       self.contentViewController = nil;
    }
    NSString *title = @"";
    if (annotationMode == CAnnotationTypeFreeText)
        title = NSLocalizedString(@"Text", @"Description for export");
    else if (annotationMode == CAnnotationTypeCircle)
        title = NSLocalizedString(@"Circle", @"Description for export");
    else if (annotationMode == CAnnotationTypeSquare)
        title = NSLocalizedString(@"Shape", @"Description for export");
    else if (annotationMode == CAnnotationTypeHighlight) {
        title = NSLocalizedString(@"Highlight", @"Description for export");
        if (_annotations.count > 1)
            title = NSLocalizedString(@"General", @"Description for export");
    }
    else if (annotationMode == CAnnotationTypeUnderline) {
        title = NSLocalizedString(@"Underline", @"Description for export");
        if (_annotations.count > 1)
            title = NSLocalizedString(@"General", @"Description for export");
    }
    else if (annotationMode == CAnnotationTypeStrikeOut)
        title = NSLocalizedString(@"Strikethrough", @"Description for export");
    else if (annotationMode == CAnnotationTypeLine)
        title = NSLocalizedString(@"Line", @"Description for export");
    else if (annotationMode == CAnnotationTypeArrow)
        title = NSLocalizedString(@"Arrow", @"Description for export");
    else if (annotationMode == CAnnotationTypeInk || annotationMode == CAnnotationTypeEraser)
        title = NSLocalizedString(@"Pen", @"Description for export");
    else if (annotationMode == CAnnotationTypeAnchored)
        title = NSLocalizedString(@"Note", @"Description for export");
    else if (annotationMode == CAnnotationTypeLink)
        title = NSLocalizedString(@"Link", @"Description for export");
    else if (annotationMode == CAnnotationTypeTextField)
        title = NSLocalizedString(@"Text Field", @"Description for export");
    else if (annotationMode == CAnnotationTypeCheckBox)
        title = NSLocalizedString(@"Check Box", @"Description for export");
    else if (annotationMode == CAnnotationTypeListMenu)
        title = NSLocalizedString(@"List Box", @"Description for export");
    else if (annotationMode == CAnnotationTypeComboBox)
        title = NSLocalizedString(@"Combo Box", @"Description for export");
    else if (annotationMode == CAnnotationTypeRadioButton)
        title = NSLocalizedString(@"Radio Button", @"Description for export");
    else if (annotationMode == CAnnotationTypeActionButton)
        title = NSLocalizedString(@"Button", @"Description for export");
    else if (annotationMode == CAnnotationTypeSignature)
        title = NSLocalizedString(@"Sample", @"Description for export");
    else if (annotationMode == CAnnotationTypeSignDot)
        title = NSLocalizedString(@"Dot", @"Description for export");
    else if (annotationMode == CAnnotationTypeSignLine)
        title = NSLocalizedString(@"Line", @"Description for export");
    else if (annotationMode == CAnnotationTypeSignCircle)
        title = NSLocalizedString(@"Rectangle", @"Description for export");
    else if (annotationMode == CAnnotationTypeSignFalse)
        title = NSLocalizedString(@"Fork", nil);
    else if (annotationMode == CAnnotationTypeSignTure)
        title = NSLocalizedString(@"Hook", nil);
    else if (annotationMode == CAnnotationTypeSignText)
        title = NSLocalizedString(@"Text", nil);
    else if (annotationMode == CAnnotationTypeSignDate)
        title = NSLocalizedString(@"Date", nil);
    else if (annotationMode == CAnnotationTypeStamp)
        title = NSLocalizedString(@"Stamp", nil);
    else
        title = @"";
    if(_annotations.count > 1) {
        title = NSLocalizedString(@"Properties", nil);
    }
    self.titleLabel.stringValue = title;
    self.titleViewHeightConstraint.constant = 40;
    CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc] initWithAnnotationType:annotationMode];

    if (annotationMode == CAnnotationTypeFreeText ||
        annotationMode == CAnnotationTypeAnchored ||
        annotationMode == CAnnotationTypeCircle ||
        annotationMode == CAnnotationTypeSquare ||
        annotationMode == CAnnotationTypeHighlight ||
        annotationMode == CAnnotationTypeUnderline ||
        annotationMode == CAnnotationTypeStrikeOut ||
        annotationMode == CAnnotationTypeLine ||
        annotationMode == CAnnotationTypeInk ||
        annotationMode == CAnnotationTypeEraser ||
        annotationMode == CAnnotationTypeArrow) {
        //处理多选
        if(_annotations.count > 1) {
            annotationModel = [[CPDFAnnotationModel alloc] initWithPDFAnnotations:_annotations];
        }
        KMGeneralAnnotationViewController *vc = [[KMGeneralAnnotationViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        self.propertiesBox.contentView = vc.view;
        self.contentViewController = vc;
    } else if (annotationMode == CAnnotationTypeLink) {
    } else if (annotationMode == CAnnotationTypeRadioButton ||
               annotationMode == CAnnotationTypeCheckBox ||
               annotationMode == CAnnotationTypeTextField ||
               annotationMode == CAnnotationTypeComboBox ||
               annotationMode == CAnnotationTypeListMenu ||
               annotationMode == CAnnotationTypeActionButton) {
        if (annotationMode == CAnnotationTypeTextField) {
            KMAnnotationFromViewController *vc = [[KMAnnotationFromViewController alloc] init];
            vc.pdfView = self.pdfView;
            vc.annotationModel = annotationModel;
            self.propertiesBox.contentView = vc.view;
            self.contentViewController = vc;
        } else if (annotationMode == CAnnotationTypeListMenu ||
                   annotationMode == CAnnotationTypeComboBox ||
                   annotationMode == CAnnotationTypeActionButton) {
            KMAnnotationChoiceWidgetAppearanceViewController *vc = [[KMAnnotationChoiceWidgetAppearanceViewController alloc] init];
            vc.annotationModel = annotationModel;
            vc.pdfView = self.pdfView;
            self.propertiesBox.contentView = vc.view;
            self.contentViewController = vc;
        } else if (annotationMode == CAnnotationTypeRadioButton ||
                   annotationMode == CAnnotationTypeCheckBox) {
            KMAnnotationButtonWidgetAppearanceViewController *vc = [[KMAnnotationButtonWidgetAppearanceViewController alloc] init];
            vc.annotationModel = annotationModel;
            vc.pdfView = self.pdfView;
            self.propertiesBox.contentView = vc.view;
            self.contentViewController = vc;
        }
    } else if (annotationMode == CAnnotationTypeSignFalse ||
               annotationMode == CAnnotationTypeSignTure ||
               annotationMode == CAnnotationTypeSignCircle ||
               annotationMode == CAnnotationTypeSignLine ||
               annotationMode == CAnnotationTypeSignDot) {
        if(_annotations.count > 1) {
            annotationModel = [[CPDFAnnotationModel alloc] initWithPDFAnnotations:_annotations];
        }
        KMAnnotationSelfSignViewController *vc = [[KMAnnotationSelfSignViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        self.propertiesBox.contentView = vc.view;
        self.contentViewController = vc;
        vc.callBack = ^(CAnnotationType type) {
            if (type == CAnnotationTypeSignFalse) {
                self.titleLabel.stringValue = NSLocalizedString(@"Fork", nil);
            } else if (type == CAnnotationTypeSignTure) {
                self.titleLabel.stringValue = NSLocalizedString(@"Hook", nil);
            } else if (type == CAnnotationTypeSignCircle) {
                self.titleLabel.stringValue = NSLocalizedString(@"Rectangle", nil);
            } else if (type == CAnnotationTypeSignLine) {
                self.titleLabel.stringValue = NSLocalizedString(@"Line", nil);
            } else if (type == CAnnotationTypeSignDot) {
                self.titleLabel.stringValue = NSLocalizedString(@"Dot", nil);
            }
        };
    } else if (annotationMode == CAnnotationTypeSignText ||
               annotationMode == CAnnotationTypeSignDate) {
        if(_annotations.count > 1) {
            annotationModel = [[CPDFAnnotationModel alloc] initWithPDFAnnotations:_annotations];
        }
        KMFillSignTextPanel *vc = [[KMFillSignTextPanel alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        self.propertiesBox.contentView = vc.view;
        self.contentViewController = vc;
    } else if (annotationMode == CAnnotationTypeStamp) {
        KMAnnotationStampViewController *vc = [[KMAnnotationStampViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        self.propertiesBox.contentView = vc.view;
        self.contentViewController = vc;
    } else if (annotationMode == CAnnotationTypeSignSignature) {
        KMSignatureAnnotationViewController *vc = [[KMSignatureAnnotationViewController alloc] init];
        self.titleViewHeightConstraint.constant = 0;
        vc.annotationModel = annotationModel;
        vc.pdfView = self.pdfView;
        self.propertiesBox.contentView = vc.view;
        self.contentViewController = vc;
    } 
}

- (void)setIsEmptyAnnotation:(BOOL)isEmptyAnnotation
{
    _isEmptyAnnotation = isEmptyAnnotation;
    if (isEmptyAnnotation) {
        self.titleLabel.hidden = YES;
        self.emptyView.hidden  = NO;
    } else {
        self.titleLabel.hidden = NO;
        self.emptyView.hidden = YES;
    }
}

- (void)setOpenPropertiesType:(KMOpenPropertiesType)openPropertiesType {
    _openPropertiesType = openPropertiesType;
    if (openPropertiesType == KMOpenPropertiesType_PageDisplay) {
        if (self.contentViewController) {
           [self.contentViewController.view removeFromSuperview];
           self.contentViewController = nil;
        }
        self.titleLabel.stringValue = NSLocalizedString(@"View Settings", nil);
        KMPageDisplayPropertiesViewController *vc = [[KMPageDisplayPropertiesViewController alloc] init];
        vc.mainController = self.mainController;
        vc.pdfView = self.pdfView;
        __block typeof(self) blockSelf = self;
        vc.readerModeBlock = ^(BOOL isReaderMode) {
            if (blockSelf.pageDisplayReaderMode) {
                blockSelf.pageDisplayReaderMode(isReaderMode);
            }
        };
        self.propertiesBox.contentView = vc.view;
        self.contentViewController = vc;
    } else if (openPropertiesType == KMOpenPropertiesType_Stamp) {
        self.titleLabel.stringValue = NSLocalizedString(@"Stamp", nil);
    } else if (openPropertiesType == KMOpenPropertiesType_Link) {
        self.titleLabel.stringValue = NSLocalizedString(@"Link", nil);
        KMAnnotationLinkViewController *vc = [[KMAnnotationLinkViewController alloc] init];
        self.propertiesBox.contentView = vc.view;
    } else if (openPropertiesType == KMOpenPropertiesType_ListBox ||
               openPropertiesType == KMOpenPropertiesType_ComboBox ||
               openPropertiesType == KMOpenPropertiesType_Button) {
    
    }
}

- (void)setIsContinuousAddStamp:(BOOL)isContinuousAddStamp {
    if (_annotations.count <= 0) {
        return;
    }
    CPDFAnnotation *annotation = _annotations.firstObject;
    CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc] initWithPDFAnnotations:_annotations];
    if ([annotation isKindOfClass:[CPDFStampAnnotation class]]) {
        if (isContinuousAddStamp) {
            self.titleViewHeightConstraint.constant = 0;
        }
    } else if ([annotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
        if (isContinuousAddStamp) {
            KMSignatureAnnotationViewController *vc = [[KMSignatureAnnotationViewController alloc] init];
            self.titleViewHeightConstraint.constant = 0;
            vc.annotationModel = annotationModel;
            vc.pdfView = self.pdfView;
            self.propertiesBox.contentView = vc.view;
            self.contentViewController = vc;
        }
    }
}

+ (CGFloat)heightWithAnnotation:(CPDFAnnotation *)annotation {
    if (!annotation) {
        return 0;
    }
    if ([annotation isKindOfClass:[CPDFLinkAnnotation class]] ||
        [annotation isKindOfClass:[CPDFMovieAnnotation class]] ||
        [annotation isKindOfClass:[CPDFSoundAnnotation class]]) {
        return 0;
    } else if ([annotation isKindOfClass:[CSelfSignAnnotation class]]) {
        return 279;
    } else if ([annotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
        return 279;
    } else if ([annotation isKindOfClass:[CPDFListStampAnnotation class]]) {
        return 279;
    } else if ([annotation isKindOfClass:[CPDFStampAnnotation class]]) {
        return 0;
    } else if ([annotation isKindOfClass:[CPDFSignatureWidgetAnnotation class]]) {
        return 279;
    } else {
        return 279;
    }
}

+ (CGFloat)heightWithAnnotationMode:(CAnnotationType)annotationMode {
    if (CAnnotationTypeSignature == annotationMode ||
        CAnnotationTypeUnkown == annotationMode) {
        return 0;
    }
    return 279;
}

#pragma mark - Private Method

- (CGRect)viewControllerRect {
    CGFloat viewFloat = 0;
    if (_buttonView.hidden) {
        viewFloat = NSHeight(self.view.frame) - NSHeight(self.gradientView.frame);
    } else {
        viewFloat = NSHeight(self.view.frame) - NSHeight(self.gradientView.frame)-KMFromContentButtonHeightFloat;
    }
    return CGRectMake(0, 0, NSWidth(self.view.frame), viewFloat);
}

- (void)updateFormContentButtonUI:(NSButton *)button {
    self.clickBtn = button;
    
    switch (button.tag) {
        case 100:
            [_formGeneralButton setTitleColor:[KMAppearance KMColor_Layout_W0]];
            [_formAppearanceButton setTitleColor:[KMAppearance KMColor_Layout_H1]];
            [_formOptionsButton setTitleColor:[KMAppearance KMColor_Layout_H1]];
            
            if([KMAppearance isDarkMode]) {
                _formGeneralButton.layer.backgroundColor = [NSColor colorWithRed:34/255. green:122/255. blue:255/255. alpha:1.].CGColor;
                _formAppearanceButton.layer.backgroundColor = [NSColor colorWithRed:57/255. green:60/255. blue:62/255. alpha:1.].CGColor;
                _formOptionsButton.layer.backgroundColor = [NSColor colorWithRed:57/255. green:60/255. blue:62/255. alpha:1.].CGColor;
            } else {
                _formGeneralButton.layer.backgroundColor = [NSColor colorWithRed:73/255. green:130/255. blue:230/255. alpha:1.].CGColor;
                _formAppearanceButton.layer.backgroundColor = [NSColor colorWithRed:235/255. green:236/255. blue:240/255. alpha:1.].CGColor;
                _formOptionsButton.layer.backgroundColor = [NSColor colorWithRed:235/255. green:236/255. blue:240/255. alpha:1.].CGColor;
            }
            break;
        case 101:
            [_formGeneralButton setTitleColor:[KMAppearance KMColor_Layout_H1]];
            [_formAppearanceButton setTitleColor:[KMAppearance KMColor_Layout_W0]];
            [_formOptionsButton setTitleColor:[KMAppearance KMColor_Layout_H1]];
            
            if([KMAppearance isDarkMode]) {
                _formAppearanceButton.layer.backgroundColor = [NSColor colorWithRed:34/255. green:122/255. blue:255/255. alpha:1.].CGColor;
                _formGeneralButton.layer.backgroundColor = [NSColor colorWithRed:57/255. green:60/255. blue:62/255. alpha:1.].CGColor;
                _formOptionsButton.layer.backgroundColor = [NSColor colorWithRed:57/255. green:60/255. blue:62/255. alpha:1.].CGColor;
            } else {
                _formAppearanceButton.layer.backgroundColor = [NSColor colorWithRed:73/255. green:130/255. blue:230/255. alpha:1.].CGColor;
                _formGeneralButton.layer.backgroundColor = [NSColor colorWithRed:235/255. green:236/255. blue:240/255. alpha:1.].CGColor;
                _formOptionsButton.layer.backgroundColor = [NSColor colorWithRed:235/255. green:236/255. blue:240/255. alpha:1.].CGColor;
            }
            break;
        case 102:
            [_formGeneralButton setTitleColor:[KMAppearance KMColor_Layout_H1]];
            [_formAppearanceButton setTitleColor:[KMAppearance KMColor_Layout_H1]];
            [_formOptionsButton setTitleColor:[KMAppearance KMColor_Layout_W0]];
            
            if([KMAppearance isDarkMode]) {
                _formOptionsButton.layer.backgroundColor = [NSColor colorWithRed:34/255. green:122/255. blue:255/255. alpha:1.].CGColor;
                _formGeneralButton.layer.backgroundColor = [NSColor colorWithRed:57/255. green:60/255. blue:62/255. alpha:1.].CGColor;
                _formAppearanceButton.layer.backgroundColor = [NSColor colorWithRed:57/255. green:60/255. blue:62/255. alpha:1.].CGColor;
            } else {
                _formOptionsButton.layer.backgroundColor = [NSColor colorWithRed:73/255. green:130/255. blue:230/255. alpha:1.].CGColor;
                _formGeneralButton.layer.backgroundColor = [NSColor colorWithRed:235/255. green:236/255. blue:240/255. alpha:1.].CGColor;
                _formAppearanceButton.layer.backgroundColor = [NSColor colorWithRed:235/255. green:236/255. blue:240/255. alpha:1.].CGColor;
            }
            break;
        default:
            break;
    }
}

#pragma mark - NSButton Action

- (IBAction)fromContentButtonAction:(NSButton *)sender {
    [self updateFormContentButtonUI:sender];
    
    self.tipsView.hidden = YES;
    
    switch (sender.tag) {
        case 100:
        {
            self.lastLineImageView.hidden = NO;
            self.oneLineImageView.hidden = YES;
            [self formGeneralProperties];
        }
            break;
        case 101:
        {
            self.lastLineImageView.hidden = YES;
            self.oneLineImageView.hidden = YES;
            [self formAppearanceProperties];
        }
            break;
        case 102:
        {
            self.lastLineImageView.hidden = YES;
            self.oneLineImageView.hidden = NO;
            [self formOptionProperties];
        }
            break;
        default:
            break;
    }
}

- (void)formGeneralProperties {
    CPDFAnnotation  *annotation = _annotations.firstObject;
    if ([annotation isKindOfClass:[CPDFTextWidgetAnnotation class]] ||
        [annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]] ||
        [annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]) {
        if ([self.contentViewController isKindOfClass:[KMAnnotationGeneralViewController class]]) {
            return;
        }
        [self.contentViewController.view removeFromSuperview];
        
        CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc] initWithPDFAnnotations:_annotations];
        KMAnnotationGeneralViewController *vc = [[KMAnnotationGeneralViewController alloc] init];
        vc.pdfview = self.pdfView;
        vc.annotationModel = annotationModel;
        vc.view.frame = [self viewControllerRect];
        [self.view addSubview:vc.view];
        self.contentViewController = vc;
    }
}

- (void)formAppearanceProperties {
    CPDFAnnotation  *annotation = _annotations.firstObject;
    CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc] initWithPDFAnnotations:_annotations];
    if ([annotation isKindOfClass:[CPDFTextWidgetAnnotation class]]) {
        if ([self.contentViewController isKindOfClass:[KMAnnotationFromViewController class]]) {
            return;
        }
        [self.contentViewController.view removeFromSuperview];
        
        KMAnnotationFromViewController *vc = [[KMAnnotationFromViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        vc.view.frame = [self viewControllerRect];
        [self.view addSubview:vc.view];
        self.contentViewController = vc;
    } else if ([annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]]) {
        CPDFButtonWidgetAnnotation  *newAnnotation = (CPDFButtonWidgetAnnotation *)annotation;
        if (newAnnotation.controlType == 1 || newAnnotation.controlType == 2) {
            if ([self.contentViewController isKindOfClass:[KMAnnotationButtonWidgetAppearanceViewController class]]) {
                return;
            }
            [self.contentViewController.view removeFromSuperview];
            
            KMAnnotationButtonWidgetAppearanceViewController *vc = [[KMAnnotationButtonWidgetAppearanceViewController alloc] init];
            vc.pdfView = self.pdfView;
            vc.annotationModel = annotationModel;
            vc.view.frame = [self viewControllerRect];
            [self.view addSubview:vc.view];
            self.contentViewController = vc;
        } else if (newAnnotation.controlType == 0) {
            if ([self.contentViewController isKindOfClass:[KMAnnotationChoiceWidgetAppearanceViewController class]]) {
                return;
            }
            [self.contentViewController.view removeFromSuperview];
            
            KMAnnotationChoiceWidgetAppearanceViewController *vc = [[KMAnnotationChoiceWidgetAppearanceViewController alloc] init];
            vc.pdfView = self.pdfView;
            vc.annotationModel = annotationModel;
            vc.view.frame = [self viewControllerRect];
            [self.view addSubview:vc.view];
            self.contentViewController = vc;
        }
    } else if ([annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]) {
        if ([self.contentViewController isKindOfClass:[KMAnnotationChoiceWidgetAppearanceViewController class]]) {
            return;
        }
        [self.contentViewController.view removeFromSuperview];
        
        KMAnnotationChoiceWidgetAppearanceViewController *vc = [[KMAnnotationChoiceWidgetAppearanceViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        vc.view.frame = [self viewControllerRect];
        [self.view addSubview:vc.view];
        self.contentViewController = vc;
    }
}

- (void)formOptionProperties {
    CPDFAnnotation  *annotation = _annotations.firstObject;
    CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc] initWithPDFAnnotations:_annotations];
    if ([annotation isKindOfClass:[CPDFTextWidgetAnnotation class]]) {
        if ([self.contentViewController isKindOfClass:[KMAnnotationTextWidgetOptionsViewController class]]) {
            return;
        }
        [self.contentViewController.view removeFromSuperview];
        
        KMAnnotationTextWidgetOptionsViewController *vc = [[KMAnnotationTextWidgetOptionsViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        vc.view.frame = [self viewControllerRect];
        [self.view addSubview:vc.view];
        self.contentViewController = vc;
    } else if ([annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]]) {
        CPDFButtonWidgetAnnotation  *newAnnotation = (CPDFButtonWidgetAnnotation *)annotation;
        if (newAnnotation.controlType == CPDFWidgetRadioButtonControl || newAnnotation.controlType == CPDFWidgetCheckBoxControl) {
            if ([self.contentViewController isKindOfClass:[KMAnnotationButtonWidgetOptionsViewController class]]) {
                return;
            }
            [self.contentViewController.view removeFromSuperview];
            
            if (newAnnotation.controlType == CPDFWidgetRadioButtonControl) {
                self.tipsView.hidden = NO;
                self.tipsLabel.stringValue = [NSString stringWithFormat:@"     %@", NSLocalizedString(@"To create a set of mutually exclusive radio buttons (i.e., where only one can be selected at a time), give the fields the same name but different button choices.", nil)];;
            } else {
                self.tipsView.hidden = YES;
            }
            KMAnnotationButtonWidgetOptionsViewController *vc = [[KMAnnotationButtonWidgetOptionsViewController alloc] init];
            vc.pdfView = self.pdfView;
            vc.annotationModel = annotationModel;
            vc.view.frame = [self viewControllerRect];
            [self.view addSubview:vc.view];
            self.contentViewController = vc;
        } else if (newAnnotation.controlType == CPDFWidgetPushButtonControl) {
            if ([self.contentViewController isKindOfClass:[KMAnnotationButtonOptionsViewController class]]) {
                return;
            }
            [self.contentViewController.view removeFromSuperview];
            
            KMAnnotationButtonOptionsViewController *vc = [[KMAnnotationButtonOptionsViewController alloc] init];
            vc.pdfView = self.pdfView;
            vc.annotationModel = annotationModel;
            vc.view.frame = [self viewControllerRect];
            [self.view addSubview:vc.view];
            self.contentViewController = vc;
        }
    } else if ([annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]) {
        if ([self.contentViewController isKindOfClass:[KMAnnotationChoiceWidgetOptionsViewController class]]) {
            return;
        }
        [self.contentViewController.view removeFromSuperview];
        self.tipsView.hidden = YES;
        self.tipsLabel.stringValue = [NSString stringWithFormat:@"     %@", NSLocalizedString(@"Select an item in the list to make it the default choice.", nil)];;
        KMAnnotationChoiceWidgetOptionsViewController *vc = [[KMAnnotationChoiceWidgetOptionsViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        vc.view.frame = [self viewControllerRect];
        [self.view addSubview:vc.view];
        self.contentViewController = vc;
    }
}

@end