//
//  KMEditPDFPopGuideView.swift
//  EaseUS PDF Editor
//
//  Created by tangchao on 2024/6/19.
//

import Cocoa

class KMEditPDFPopGuideView: NSView {
    private lazy var targetView_: KMGuideTargetView = {
        let view = KMGuideTargetView(frame: NSRect(x: 0, y: 0, width: 56, height: 56))
        view.lineWidth = 2
        view.strokeColor = NSColor(red: 34/255, green: 122/255, blue: 255/255, alpha: 0.7)
        return view
    }()
    
    private lazy var contentView_: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.borderWidth = 2
        view.layer?.cornerRadius = 8
        view.layer?.masksToBounds = true
        view.layer?.borderColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 1).cgColor
        view.layer?.backgroundColor = NSColor.white.cgColor
        return view
    }()
    
    private lazy var shadowView_: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.borderWidth = 0
//        view.layer?.shadowColor = NSColor.black.withAlphaComponent(0.55).cgColor
        view.layer?.shadowColor = NSColor.red.cgColor
        view.layer?.shadowOpacity = 0.1
        view.layer?.shadowRadius = 3.0
        return view
    }()
    private lazy var titleLabel_: NSTextField = {
        let view = NSTextField(labelWithString: NSLocalizedString("Add a new hover quick entrance.", comment: ""))
        return view
    }()
    private lazy var despLabel_: NSTextField = {
        let view = NSTextField(wrappingLabelWithString: NSLocalizedString("You can hide the hover bar in Settings.", comment: ""))
        return view
    }()
    private lazy var settingBtn_: NSButton = {
        let view = NSButton()
        view.isBordered = false
        let title = NSLocalizedString("Settings", comment: "")
        var attri = NSAttributedString(string: title)
        view.attributedTitle = NSAttributedString(string: title, attributes: [.underlineStyle : true, .foregroundColor : NSColor(hex: "#4982E6")])
        view.target = self
        view.action = #selector(buttonClick)
        return view
    }()
    
    private lazy var okBtnBox_: NSBox = {
        let view = NSBox()
        view.boxType = .custom
        view.titlePosition = .noTitle
        view.contentViewMargins = .zero
        view.borderColor = NSColor.clear
        view.cornerRadius = 2
        view.fillColor = NSColor(hex: "#4982E6")
        return view
    }()
    
    private lazy var okBtn_: NSButton = {
        let view = NSButton()
        view.isBordered = false
        view.title = NSLocalizedString("Got it", comment: "")
        view.setTitleColor(.white)
        view.target = self
        view.action = #selector(buttonClick)
        return view
    }()
    
    var itemClick: KMCommonClickBlock?
    
    static let kWidth: CGFloat = 300
    static let kHeight: CGFloat = 152+56+5
    
    var contentTop: Bool = false
    
    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        // Drawing code here.
    }
    
    convenience init(contentTop: Bool = true) {
        self.init(frame: .init(x: 0, y: 0, width: Self.kWidth, height: Self.kHeight))
        self.contentTop = contentTop
        
        self.initSubView()
    }
    
    func initSubView() {
        if self.contentTop { // 内容显示在上面
            self.addSubview(self.targetView_)
            self.targetView_.km_add_bottom_constraint()
            self.targetView_.km_add_centerX_constraint()
            self.targetView_.km_add_size_constraint(size: .init(width: 56, height: 56))
            
            self.addSubview(self.contentView_)
            self.contentView_.km_add_centerX_constraint()
            self.contentView_.km_add_bottom_constraint(equalTo: self.targetView_, attribute: .top, constant: -5)
            self.contentView_.km_add_size_constraint(size: .init(width: 300, height: 152))
        } else {
            self.addSubview(self.targetView_)
            self.targetView_.km_add_top_constraint()
            self.targetView_.km_add_centerX_constraint()
            self.targetView_.km_add_size_constraint(size: .init(width: 56, height: 56))
            
            self.addSubview(self.contentView_)
            self.contentView_.km_add_centerX_constraint()
            self.contentView_.km_add_top_constraint(equalTo: self.targetView_, attribute: .bottom, constant: 5)
            self.contentView_.km_add_size_constraint(size: .init(width: 300, height: 152))
        }
        
//        self.addSubview(self.shadowView_)
        //        self.wantsLayer = true
        //        self.shadow = NSShadow()
//        self.shadowView_.frame = self.bounds.insetBy(dx: 5, dy: 0)
//        let shadowPath = NSBezierPath(rect: self.shadowView_.bounds)
//        if #available(macOS 14.0, *) {
//            self.shadowView_.layer?.shadowPath = shadowPath.kmCGPath()
//        }
        
        self.contentView_.addSubview(self.titleLabel_)
        self.contentView_.addSubview(self.despLabel_)
        self.contentView_.addSubview(self.settingBtn_)
        self.contentView_.addSubview(self.okBtnBox_)
        self.okBtnBox_.contentView?.addSubview(self.okBtn_)
        
        self.titleLabel_.km_add_top_constraint(constant: 20)
        self.titleLabel_.km_add_left_constraint(constant: 20)
        self.despLabel_.km_add_left_constraint(constant: 20)
        self.despLabel_.km_add_top_constraint(equalTo: self.titleLabel_, attribute: .bottom,  constant: 14)
        self.despLabel_.km_add_width_constraint(constant: 260)
        self.okBtn_.km_add_inset_constraint(inset: .init(top: 4, left: 8, bottom: 4, right: 8))
        self.okBtnBox_.km_add_right_constraint(constant: -20)
        self.okBtnBox_.km_add_bottom_constraint(constant: -16)
        
        self.settingBtn_.km_add_bottom_constraint(constant: -20)
        self.settingBtn_.km_add_right_constraint(equalTo: self.okBtn_, attribute: .left, constant: -20)
    }
    
    @objc func buttonClick(_ sender: NSButton) {
        if sender.isEqual(to: self.okBtn_) {
            self.itemClick?(1)
        } else {
            self.itemClick?(0)
        }
    }
    
    override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
        if appearance == .darkAqua {
            self.contentView_.layer?.borderColor = NSColor(red: 34/255, green: 122/255, blue: 255/255, alpha: 1).cgColor
            self.contentView_.layer?.backgroundColor = NSColor.black.cgColor
            self.titleLabel_.textColor = KMAppearance.Layout.w0Color()
            self.despLabel_.textColor = KMAppearance.Layout.h1Color()
            self.targetView_.strokeColor = NSColor(red: 34/255, green: 122/255, blue: 255/255, alpha: 0.7)
            
        } else {
            self.contentView_.layer?.borderColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 1).cgColor
            self.contentView_.layer?.backgroundColor = NSColor.white.cgColor
            self.titleLabel_.textColor = KMAppearance.Layout.mColor()
            self.despLabel_.textColor = KMAppearance.Layout.h0Color()
            self.targetView_.strokeColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.4)
        }
    }
}