//
//  MPPositive_SearchSuggestionItemListModel.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/13.
//

import UIKit

class MPPositive_SearchSuggestionItemListModel: NSObject {
    ///用户搜索的文本
    var searchText:String?
    ///展示的富文本内容
    var attributedTexts:[NSAttributedString]?
    init(_ text:String, suggestions:[[MPPositive_SearchSuggestionItemModel]]) {
        super.init()
        searchText = text
        //将suggestions拆开拼成一组字符串
        let titles:[String] = suggestions.flatMap({$0.compactMap({($0.title)})})
        self.attributedTexts = titles.map({createAttributedString(for: $0)})
    }
    
    //将与搜索文本相同的字符串转变颜色
    func createAttributedString(for string: String) -> NSAttributedString {
        let attributedString = NSMutableAttributedString(string: string, attributes: [.foregroundColor: UIColor.white, .font:UIFont.systemFont(ofSize: 14*width, weight: .medium)])
        if let range = string.range(of: searchText ?? "") {
            let narange = range.toNSRange(in: string)
            attributedString.addAttributes([.foregroundColor:UIColor(hex: "#80F988")], range: narange)
        }
        return attributedString
    }
}
