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

#import "KMAnnotationPropertiesViewController.h"
#import "KMGeneralAnnotationViewController.h"
#import "KMAnnotationLinkViewController.h"
#import "KMSelfSignAnnotation.h"
#import "KMSelfSignAnnotationFreeText.h"
//#import "NSString_SKExtensions.h"
#import "KMAnnotationStampViewController.h"
#import <PDF_Master-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;

@end

@implementation KMAnnotationPropertiesViewController


#pragma mark - View Methods

- (void)loadView {
    [super loadView];
    
    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.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 = [NSColor colorWithRed:0.145 green:0.149 blue:0.161 alpha:1];
}

#pragma mark - Setter Methods
-(void)setAnnotations:(NSArray *)annotations
{
    if (_annotations != annotations) {
        _annotations = annotations;
    }
    
    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) {
        [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] == KMSelfSignAnnotationFreeTextSubType_Profile) {
                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 {
        self.titleLabel.stringValue = [annotation type].typeName;
    }
    
    CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc] initWithPDFAnnotations:_annotations];
    if ([annotation isKindOfClass:[CPDFTextWidgetAnnotation class]] ||
        [annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]] ||
        [annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]]){
        KMFormPropertPanelViewController *vc = [[KMFormPropertPanelViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        self.propertiesBox.contentView = 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)];
        }
        
        KMAnnotationLinkViewController *vc = [[KMAnnotationLinkViewController alloc] init];
        vc.annotationModel = annotationModel;
        vc.pdfview = self.pdfView;
        vc.pageCount = self.pdfView.document.pageCount;
        [vc setContent:contents];
        vc.startDestination = [[CPDFDestination alloc] initWithDocument:self.pdfView.document pageIndex:linkAnnotation.page.pageIndex atPoint:linkAnnotation.bounds.origin zoom:self.pdfView.scaleFactor];
        if (linkAnnotation.destination) {
            CPDFDestination *des = linkAnnotation.destination;
            vc.targetDestination = [[CPDFDestination alloc] initWithDocument:des.document pageIndex:des.pageIndex atPoint:des.point zoom:des.zoom];
        }
        
        self.propertiesBox.contentView = 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) {
            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 (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:[CPDFStampAnnotation class]]) {
//        KMAnnotationStampViewController *vc = [[KMAnnotationStampViewController alloc] init];
//        vc.annotationModel = annotationModel;
//        vc.pdfView = self.pdfView;
//        self.propertiesBox.contentView = vc.view;
//        self.contentViewController = vc;
    } else if ([annotation isKindOfClass:[CPDFListSignatureAnnotation class]]) {
        
    } else if ([annotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
        
    } else {
        KMGeneralAnnotationViewController *vc = [[KMGeneralAnnotationViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        self.propertiesBox.contentView = vc.view;
        self.contentViewController = vc;
        if ([annotation isKindOfClass:[KMSelfSignAnnotation class]]) {
            __block typeof(self) blockSelf = self;
            vc.callBack = ^(CAnnotationType newType) {
                NSString *returnString = @"";
                if (newType == CAnnotationTypeSignFalse) {
                    returnString = NSLocalizedString(@"X", nil);
                } else if (newType == CAnnotationTypeSignTure) {
                    returnString = NSLocalizedString(@"Check mark", nil);
                } else if (newType == CAnnotationTypeSignCircle) {
                    returnString = NSLocalizedString(@"Circle", nil);
                } else if (newType == CAnnotationTypeSignLine) {
                    returnString = NSLocalizedString(@"Line", nil);
                } else if (newType == CAnnotationTypeSignDot) {
                    returnString = NSLocalizedString(@"Dot", nil);
                } else if (newType == CAnnotationTypeSignText) {
                    returnString = NSLocalizedString(@"Text", nil);
                } else if (newType == CAnnotationTypeInk) {
                    returnString = NSLocalizedString(@"Pen", nil);
                } else if (newType == CAnnotationTypeEraser) {
                    returnString = NSLocalizedString(@"Eraser", nil);
                }
                blockSelf.titleLabel.stringValue = returnString;
            };
        } else {
            vc.callBack = nil;
        }
    }
}

- (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
        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;
        if (annotationMode == CAnnotationTypeInk || annotationMode == CAnnotationTypeEraser) {
            __block typeof(self) blockSelf = self;
            vc.callBack = ^(CAnnotationType newType) {
                NSString *returnString = @"";
                if (newType == CAnnotationTypeInk) {
                    returnString = NSLocalizedString(@"Pen", nil);
                } else if (newType == CAnnotationTypeEraser) {
                    returnString = NSLocalizedString(@"Eraser", nil);
                }
                blockSelf.titleLabel.stringValue = returnString;
            };
        } else {
            vc.callBack = nil;
        }
    } else if (annotationMode == CAnnotationTypeLink) {
//        KMAnnotationEmptyLinkViewController *vc = [[KMAnnotationEmptyLinkViewController alloc] init];
//        vc.annotationModel = annotationModel;
//        self.propertiesBox.contentView = vc.view;
//        self.contentViewController = vc;
    
    } else if (annotationMode == CAnnotationTypeRadioButton ||
               annotationMode == CAnnotationTypeCheckBox ||
               annotationMode == CAnnotationTypeTextField ||
               annotationMode == CAnnotationTypeComboBox ||
               annotationMode == CAnnotationTypeListMenu ||
               annotationMode == CAnnotationTypeActionButton) {
        KMFormPropertPanelViewController *vc = [[KMFormPropertPanelViewController alloc] init];
        vc.pdfView = self.pdfView;
        vc.annotationModel = annotationModel;
        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];
        }
        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 (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) {
        self.titleViewHeightConstraint.constant = 0;
        KMAnnotationStampViewController *vc = [[KMAnnotationStampViewController alloc] init];
        vc.annotationModel = annotationModel;
        vc.pdfView = self.pdfView;
        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;
    CPDFAnnotationModel * annotationModel = [[CPDFAnnotationModel alloc] initWithPDFAnnotations:_pdfView.activeAnnotations];
    if (openPropertiesType == KMOpenPropertiesType_PageDisplay) {
//        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) {
//            [blockSelf.mainController readMode:blockSelf];
//        };
//        self.propertiesBox.contentView = vc.view;
//        self.contentViewController = vc;
      
    } else if (openPropertiesType == KMOpenPropertiesType_Stamp) {
//        self.titleLabel.stringValue = NSLocalizedString(@"Stamp", nil);
//        KMAnnotationStampViewController *vc = [[KMAnnotationStampViewController alloc] init];
//        vc.annotationModel = annotationModel;
//        vc.pdfView = self.pdfView;
//        self.propertiesBox.contentView = vc.view;
//        self.contentViewController = vc;
   
    } else if (openPropertiesType == KMOpenPropertiesType_Link) {
        self.titleLabel.stringValue = NSLocalizedString(@"Link", nil);
        KMAnnotationLinkViewController *vc = [[KMAnnotationLinkViewController alloc] init];
        vc.annotationModel = annotationModel;
        vc.pdfview = self.pdfView;
        vc.pageCount = self.pdfView.document.pageCount;
        [vc setIsCreateLink:YES];
        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;
            KMAnnotationStampViewController *vc = [[KMAnnotationStampViewController alloc] init];
            vc.annotationModel = annotationModel;
            vc.pdfView = self.pdfView;
            vc.isContinuousAddStamp = YES;
            self.propertiesBox.contentView = vc.view;
            self.contentViewController = vc;
        }
    } 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:[KMSelfSignAnnotation class]]) {
        return 279;
    } else if ([annotation isKindOfClass:[CPDFStampAnnotation class]]) {
        return 279;
    } else if ([annotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
        return 279;
    } else if ([annotation isKindOfClass:[CPDFSignatureWidgetAnnotation class]]) {
        return 279;
    } else {
        return 279;
    }
}

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

@end