//
//  LNHomeTopMenuView.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/14.
//

import Foundation
import UIKit
import SnapKit


class LNHomeTopMenuView: UIView {
    private let createRoom = UIButton()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
            
        setupViews()
        LNEventDeliver.addObserver(self)
        onUserRoomAbilityChanged()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNHomeTopMenuView: LNRoomManagerNotify {
    func onUserRoomAbilityChanged() {
        guard let ability = LNRoomManager.shared.roomAbility else {
            createRoom.isHidden = true
            return
        }
        createRoom.isHidden = ability.homeowner != true || ability.roomTypes.isEmpty
    }
}

extension LNHomeTopMenuView {
    private func setupViews() {
        let logo = UIImageView()
        logo.image = .icHomeLogo
#if DEBUG
        logo.onTap {
            LNBottomSheetMenu.showEnvMenu()
        }
#endif
        addSubview(logo)
        logo.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
        }
        
#if DEBUG
        let envLabel = UILabel()
        envLabel.font = .heading_h4
        envLabel.textColor = .text_4
        envLabel.text = "(\(LNAppConfig.shared.curEnv.text))"
        addSubview(envLabel)
        envLabel.snp.makeConstraints { make in
            make.centerY.equalTo(logo)
            make.leading.equalTo(logo.snp.trailing).offset(5)
        }
#endif
        
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.spacing = 12
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
        }
        
        let search = UIButton()
        search.setImage(.icSearch, for: .normal)
        search.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            pushToUserSearch()
        }), for: .touchUpInside)
        stackView.addArrangedSubview(search)
        
        createRoom.isHidden = true
        createRoom.layer.cornerRadius = 15
        createRoom.layer.borderWidth = 1
        createRoom.layer.borderColor = .fill
        createRoom.backgroundColor = .fill.withAlphaComponent(0.5)
        createRoom.setImage(.icRoomCreate.withTintColor(.text_6, renderingMode: .alwaysOriginal), for: .normal)
        createRoom.setTitle(.init(key: "A00318"), for: .normal)
        createRoom.setTitleColor(.text_6, for: .normal)
        createRoom.titleLabel?.font = .heading_h5
        createRoom.contentEdgeInsets = .init(top: 0, left: 6, bottom: 0, right: 6)
        createRoom.addAction(UIAction(handler: { _ in
            let panel = LNCreateRoomPanel()
            panel.popup()
        }), for: .touchUpInside)
        stackView.addArrangedSubview(createRoom)
    }
}


#if DEBUG

import SwiftUI

struct LNHomeTopMenuViewPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNHomeTopMenuView()
        container.addSubview(view)
        view.snp.makeConstraints { make in
            make.leading.trailing.top.equalToSuperview()
        }
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

#Preview(body: {
    LNHomeTopMenuViewPreview()
})
#endif
