//
//  LNOrderRoomGameCategoryFilterPanel.swift
//  Gami
//
//  Created by OneeChan on 2026/3/18.
//

import Foundation
import UIKit
import SnapKit
import Combine


class LNOrderRoomGameCategoryFilterPanel: LNPopupView {
    private var scrollViewHeight: Constraint?
    private let stackView = UIStackView()
    private var options: [LNGameCategoryItemVO] = []
    private var curSelectedCode: String = ""
    
    var handler: ((LNGameCategoryItemVO) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(options: [LNGameCategoryItemVO], curSelectedCode: String) {
        self.options = options
        self.curSelectedCode = curSelectedCode
        
        reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

private extension LNOrderRoomGameCategoryFilterPanel {
    func setupViews() {
        container.backgroundColor = .fill_7
        container.snp.makeConstraints { make in
            make.height.lessThanOrEqualToSuperview().multipliedBy(0.62)
        }
        
        let header = UIView()
        container.addSubview(header)
        header.snp.makeConstraints { make in
            make.top.horizontalEdges.equalToSuperview()
            make.height.equalTo(52)
        }
        
        let titleLabel = UILabel()
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_1
        titleLabel.text = .init(key: "A00359")
        header.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        let scrollView = UIScrollView()
        scrollView.publisher(for: \.contentSize).removeDuplicates().sink { [weak self] newValue in
            guard let self else { return }
            scrollViewHeight?.update(offset: newValue.height)
        }.store(in: &cancellables)
        container.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.top.equalTo(header.snp.bottom)
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview().offset(commonBottomInset)
            scrollViewHeight = make.height.equalTo(0).priority(.high).constraint
        }
        
        stackView.axis = .vertical
        stackView.spacing = 0
        scrollView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }
    }
    
    func reloadData() {
        stackView.arrangedSubviews.forEach {
            stackView.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        
        options.forEach { option in
            stackView.addArrangedSubview(buildItemView(option))
        }
    }
    
    func buildItemView(_ option: LNGameCategoryItemVO) -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(54)
        }
        
        let titleLabel = UILabel()
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_1
        titleLabel.text = option.name
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.trailing.lessThanOrEqualToSuperview().offset(-48)
        }
        
        let checkIc = UIImageView()
        checkIc.image = option.code == curSelectedCode ? .icCheck : .icUncheck
        container.addSubview(checkIc)
        checkIc.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(16)
        }
        
        container.onTap { [weak self] in
            guard let self else { return }
            
            curSelectedCode = option.code
            reloadData()
            dismiss()
            handler?(option)
        }
        
        return container
    }
}
