//
//  LNRoomSheetMenu.swift
//  Gami
//
//  Created by OneeChan on 2026/3/16.
//

import Foundation
import UIKit
import SnapKit


class LNRoomSheetMenu: LNBottomSheetMenu {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        container.backgroundColor = .fill_7
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    static func buildMenuItem(title: String, handler: @escaping () -> Void) -> UIView {
        let container = UIView()
        container.onTap(handler)
        
        let titleLabel = UILabel()
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_1
        titleLabel.text = title
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.verticalEdges.equalToSuperview().inset(15)
        }
        
        return container
    }
    
    func update(title: String?, views: [UIView]) {
        let header = UIView()
        if let title {
            let titleLabel = UILabel()
            titleLabel.font = .heading_h5
            titleLabel.textColor = .text_2
            titleLabel.text = title
            header.addSubview(titleLabel)
            titleLabel.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview().offset(17)
                make.bottom.equalToSuperview().offset(-5)
            }
        } else {
            header.snp.makeConstraints { make in
                make.height.equalTo(15)
            }
        }
        
        stackView.addArrangedSubview(header)
        stackView.addArrangedSubview(buildMenus(views: views))
        stackView.addArrangedSubview(buildSeperator())
        stackView.addArrangedSubview(buildCancel())
    }
    
    private func buildMenus(views: [UIView]) -> UIView {
        let container = UIView()
        
        let stackView = UIStackView()
        stackView.axis = .vertical
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-15)
        }
        
        for view in views {
            stackView.addArrangedSubview(view)
        }
        
        return container
    }
    
    private func buildSeperator() -> UIView {
        let view = UIView()
        view.backgroundColor = .black.withAlphaComponent(0.8)
        view.snp.makeConstraints { make in
            make.height.equalTo(3)
        }
        
        return view
    }
    
    private func buildCancel() -> UIView {
        let container = UIView()
        container.onTap { [weak self] in
            guard let self else { return }
            dismiss()
        }
        
        let cancel = UILabel()
        cancel.text = .init(key: "A00003")
        cancel.textColor = .text_2
        cancel.font = .heading_h4
        container.addSubview(cancel)
        cancel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.verticalEdges.equalToSuperview().inset(20)
        }
        
        return container
    }
}
