//
//  CLPopoverView.swift

//
//  Created by Leon on 2025/7/28.
//
import UIKit
extension FSPopoverView {
    func clStyle() {
        showsArrow = false
        // titleFont = .t.tll
    }
}

class CLPopoverListView: FSPopoverListView {
    var customBackGroundView: UIView = UIView()

    override init(scrollDirection: FSPopoverListView.ScrollDirection = .vertical) {
        super.init(scrollDirection: scrollDirection)

        showsArrow = false
        cornerRadius = 16
        shadowColor = nil
        shadowRadius = 0
        shadowOpacity = 0
        borderWidth = 0
        FSPopoverView.fs_appearance().backgroundColor = .c.csfn
        // ❌backgroundColor = .c.csfn
    }

    override func backgroundView(for popoverView: FSPopoverView) -> UIView? {
        customBackGroundView.backgroundColor = .c.csfn
        return customBackGroundView
    }

    override func containerSafeAreaInsets(for popoverView: FSPopoverView) -> UIEdgeInsets {
        return .init(top: 10.0, left: 16.0, bottom: 10.0, right: 16.0)
    }
}

extension CLPopoverListView {
    /// 通用popover Crushlevel样式
    static func setupCLStyle() {
        FSPopoverViewAppearance.shared.spacing = 16
        FSPopoverViewAppearance.shared.textFont = .t.tll
        FSPopoverViewAppearance.shared.textColor = .white
        FSPopoverViewAppearance.shared.borderWidth = 0
        FSPopoverViewAppearance.shared.cornerRadius = 16
        FSPopoverViewAppearance.shared.backgroundColor = .c.csfn
    }

    func setupDeletePopover(_ rect: CGRect, inView: UIView, block: (() -> Void)?) {
        var itemsNew: [CLPopoverListTextItem] = [CLPopoverListTextItem]()

        let item = CLPopoverListTextItem()
        item.image = MWIconFont.image(fromIcon: .iconDelete, size: .init(width: 20, height: 20), color: .white) // .white
        item.title = "Delete"
        item.setupCLStyle()
        item.updateLayout()// 必需
        item.selectedHandler = { item in
            guard let item = item as? CLPopoverListTextItem else {
                return
            }
            dlog(item.title ?? "")
            block?()
        }
        itemsNew.append(item)

        items = itemsNew
        dismissWhenSelected = true

        present(fromRect: rect, in: inView)
    }

    /// 🔥通用调用Popover Menu方法
    public func setupCommonPopover(_ rect: CGRect, inView: UIView, items: [CLPopoverListTextItem], block: (() -> Void)?) {
        self.items = items
        dismissWhenSelected = true
        present(fromRect: rect, in: inView)
    }
}
