//
//  KMSecureEncryptSuccessTipView.swift
//  PDF Master
//
//  Created by tangchao on 2022/11/28.
//

import Cocoa

typealias KMSecureEncryptSuccessTipViewItemClick = () ->()
class KMSecureEncryptSuccessTipView: NSView {

    private var iconImageView = NSImageView()
    private var iconSelectImageView = NSImageView()
    private var despLabel = NSTextField(wrappingLabelWithString: "")
    private var titleLabel = NSTextField(labelWithString: "")
    private var closeButton = NSButton()
    
    private var knowButtonVC: KMDesignButton?
    
    var itemClick: KMSecureEncryptSuccessTipViewItemClick!
    
    private var knowButtonSize: NSSize = NSMakeSize(140, 32)
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)

        self.initSubViews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        
        self.initSubViews()
    }
    
    override var isFlipped: Bool {
        return true
    }
    
    func initSubViews() {
        wantsLayer = true
        layer?.backgroundColor = NSColor(hex: "#E8F5FF").cgColor
        layer?.cornerRadius = 8
        self.shadow = NSShadow()
        self.layer?.shadowColor = NSColor(hex: "#00000029").cgColor
        self.layer?.shadowOpacity = 1
        self.layer?.shadowRadius = 8
        self.layer?.shadowOffset = CGSize(width: 0, height: -3)
        
        self.addSubview(self.titleLabel)
        self.addSubview(self.closeButton)
        addSubview(iconImageView)
        addSubview(despLabel)
        
        self.knowButtonVC = KMDesignButton(withType: .Text)
        self.addSubview(self.knowButtonVC!.view)
        
        self.iconImageView.addSubview(self.iconSelectImageView)
        
        self.titleLabel.stringValue = NSLocalizedString("Set password successfully", comment: "")
        self.titleLabel.font = NSFont.SFProTextRegular(14)
        self.titleLabel.textColor = NSColor.titleColor()
        self.closeButton.isBordered = false
        self.closeButton.image = NSImage(named: "KMImageNameWhiteClose")
        self.closeButton.target = self
        self.closeButton.action = #selector(knowButtonAction)
        iconImageView.wantsLayer = true
        iconImageView.image = NSImage(named: "KMImageNameTipSelectedBackgroud")
        self.iconSelectImageView.image = NSImage(named: "KMImageNameTipSelected")
        
        self.despLabel.isSelectable = false
        despLabel.stringValue = NSLocalizedString("The security settings won't take effect until the document is saved. You can change the security settings before closing the file.", comment: "")
        let ps = NSMutableParagraphStyle()
        ps.lineSpacing = 7
        despLabel.attributedStringValue = NSAttributedString(string: despLabel.stringValue, attributes: [.foregroundColor : NSColor.despColor(),
            .font : NSFont.SFProTextRegular(14),
            .paragraphStyle : ps
        ])
        
        self.knowButtonVC?.stringValue = NSLocalizedString("Got it", comment: "")
        self.knowButtonVC?.button(type: .Ghost, size: .m)
        self.knowButtonVC?.target = self
        self.knowButtonVC?.action = #selector(knowButtonAction)
        let size = self.knowButtonVC?.stringValue.boundingRect(with: NSMakeSize(1000, self.knowButtonSize.height), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [.foregroundColor : NSColor.buttonTitleColor(), .font : NSFont.SFProTextRegular(14)]).size
        if (size == nil || size!.width < 40) {
            self.knowButtonSize.width = 70
        } else {
            self.knowButtonSize.width = size!.width + 34
        }
    }
    
    override func layout() {
        super.layout()
        
        let width: CGFloat = NSWidth(self.bounds)
        let height: CGFloat = NSHeight(self.bounds)
        
        let iconSize: CGFloat = 20
        iconImageView.frame = NSMakeRect(18, 18, iconSize, iconSize)
        self.iconSelectImageView.frame = self.iconImageView.bounds
        
        self.titleLabel.frame = NSMakeRect(self.iconImageView.frame.maxX+10, 16, 295, 22)
        self.closeButton.frame = NSMakeRect(width-34, 10, 24, 24)
        
        let despX: CGFloat = iconImageView.frame.maxX+12
        let despY: CGFloat = self.titleLabel.frame.maxY+8
        despLabel.frame = NSMakeRect(despX, despY, width-despX-8, height-despY-45)
        
        let knowSize = self.knowButtonSize
        self.knowButtonVC?.view.frame = NSMakeRect(width-knowSize.width-16, height-knowSize.height-16, knowSize.width, knowSize.height)
    }
    
    @objc func knowButtonAction() {
        guard let callback = itemClick else {
            return
        }
        
        callback()
    }
    
}