//
//  LNUserSearchHistoryView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/12.
//

import Foundation
import UIKit
import SnapKit


protocol LNUserSearchHistoryViewDelegate: NSObject {
    func onUserSearchHistoryView(view: LNUserSearchHistoryView, didClick history: String)
}


class LNUserSearchHistoryView: UIView {
    private var history: [String] = LNUserDefaults[.userSearchHistory, []]
    private let stackView = LNAutoFillStackView()
    
    weak var delegate: LNUserSearchHistoryViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        reload()
    }
    
    func addRecord(_ keyword: String) {
        let fixed = keyword.trimmingCharacters(in: .whitespacesAndNewlines)
        guard !fixed.isEmpty else { return }
        
        if history.first == keyword {
            return
        }
        
        history.removeAll { $0 == fixed }
        history.insert(fixed, at: 0)
        if history.count >= 10 {
            history.removeLast()
        }
        
        LNUserDefaults[.userSearchHistory] = history
        reload()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNUserSearchHistoryView {
    private func reload() {
        var itemViews: [UIView] = []
        history.forEach { item in
            let container = UIView()
            container.layer.cornerRadius = 11
            container.layer.borderColor = UIColor.fill_4.cgColor
            container.layer.borderWidth = 1
            container.onTap { [weak self] in
                guard let self else { return }
                delegate?.onUserSearchHistoryView(view: self, didClick: item)
            }
            container.snp.makeConstraints { make in
                make.height.equalTo(22)
            }
            
            let title = UILabel()
            title.text = item
            title.font = .body_xs
            title.textColor = .text_3
            container.addSubview(title)
            title.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.horizontalEdges.equalToSuperview().inset(8)
            }
            container.layoutIfNeeded()
            itemViews.append(container)
        }
        stackView.update(itemViews)
    }
    
    private func setupViews() {
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00242")
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_5
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.top.equalToSuperview()
        }
        
        stackView.itemSpacing = 9
        stackView.spacing = 10
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.bottom.equalToSuperview()
            make.height.equalTo(0).priority(.low)
        }
    }
}
