//
//  UILabel+Extension.swift
//  SpeakEasyLearnEnglish
//
//  Created by edy on 2025/7/3.
//

import UIKit
import CoreText

fileprivate var textTapState = SpeakEasyAssociatedKeys.labelTextTap.rawValue

@IBDesignable
extension UILabel {
    

    /// 设置文字间距
    func lineSpacing(_ lineSpacing:CGFloat = 5) {
        let style = NSMutableParagraphStyle()
        style.lineSpacing = lineSpacing
        style.alignment = self.textAlignment
        self.attributedText = NSAttributedString(string: self.text ?? "", attributes: [NSAttributedString.Key.paragraphStyle: style])
        self.sizeToFit()
        
    }
    
    
    override func setCutomFonts() {
        if cutomFont >= 0 {
            switch cutomFont {
            case 0:
                self.font = UIFont.montserrat(.regular ,size: fontSize)
                break
            case 1:
                self.font = UIFont.montserrat(.medium ,size: fontSize)
                break
            case 2:
                self.font = UIFont.montserrat(.semiBold ,size: fontSize)
                break
            default:
                self.font = UIFont.montserrat(.bold ,size: fontSize)
                break
            }
        }
    }
    
    var textTaps:[String] {
        set {
            objc_setAssociatedObject(self, &textTapState, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            return objc_getAssociatedObject(self, &textTapState) as? [String] ?? []
        }
    }
    
    func AddTextTap(_ text:[String]) -> Void {
        if text.count > 0 {
            textTaps = text
            self.gesture(target: self, class: UITapGestureRecognizer.self, selector: #selector(handleTap(_:)))
        }
    }
    
    @objc private func handleTap(_ gesture: UITapGestureRecognizer) {
        // 确保有文本和布局
        let attributedText = self.attributedText ?? NSAttributedString(string: text ?? "")
        if attributedText.string.count <= 0 {
            return
        }

        let layoutManager = NSLayoutManager()
        let textContainer = NSTextContainer(size: bounds.size)
        let textStorage = NSTextStorage(attributedString: attributedText)
        
        textContainer.lineFragmentPadding = 0
        textContainer.maximumNumberOfLines = numberOfLines
        textContainer.lineBreakMode = lineBreakMode
        
        layoutManager.addTextContainer(textContainer)
        textStorage.addLayoutManager(layoutManager)
        
        let textRect = self.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
        
        var adjustedPoint = gesture.location(in: self)
        switch textAlignment {
        case .center:
            adjustedPoint.x -= (bounds.width - textRect.width) / 2
        case .right, .justified, .natural:
            adjustedPoint.x -= (bounds.width - textRect.width)
        default:
            break
        }
        let characterIndex = layoutManager.characterIndex(for: adjustedPoint,
                                                       in: textContainer,
                                                       fractionOfDistanceBetweenInsertionPoints: nil)
        for (_,v) in textTaps.enumerated() {
            if let range = attributedText.string.range(of: v) {
                let rangeN = NSRange(range, in: attributedText.string)
                
                if characterIndex < (rangeN.location + rangeN.length) && characterIndex >= rangeN.location {
                    guard let callblack = callblack else { return }
                    Print(characterIndex,attributedText.string.count)
                    callblack(v)
                    return
                }
            }
        }
        
    }
}

