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

import Foundation
import UIKit
import SnapKit


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


class LNUserSearchHistoryView: UIView {
    private var history: [String] = LNUserDefaults[.userSearchHistory, []]
    private let historyStackView = LNAutoFillStackView()
    
    private let roomViews = [LNUserSearchRoomCardView(), LNUserSearchRoomCardView()]
    
    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)
        }
        historyStackView.update(itemViews)
    }
    
    private func setupViews() {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 22
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        stackView.addArrangedSubview(buildHistory())
//        stackView.addArrangedSubview(buildHotRoom())
    }
    
    private func buildHistory() -> UIView {
        let container = UIView()
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00242")
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(12)
        }
        
        historyStackView.itemSpacing = 9
        historyStackView.spacing = 10
        container.addSubview(historyStackView)
        historyStackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.bottom.equalToSuperview()
            make.height.equalTo(0).priority(.low)
        }
        
        return container
    }
    
    private func buildHotRoom() -> UIView {
        let container = UIView()
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00368")
        titleLabel.font = .heading_h5
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalToSuperview()
        }
        
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.spacing = 13
        stackView.distribution = .fillEqually
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.bottom.equalToSuperview()
        }
        
        for roomView in roomViews {
            stackView.addArrangedSubview(roomView)
        }
        
        return container
    }
}
