//
//  LNRoomTopMenuView.swift
//  Gami
//
//  Created by OneeChan on 2026/3/9.
//

import Foundation
import UIKit
import SnapKit


class LNRoomTopMenuView: UIView {
    private let hostAvatar = UIImageView()
    private let roomNameLabel = UILabel()
    private let roomIdLabel = UILabel()
    
    private weak var roomSession: LNRoomSession?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        LNEventDeliver.addObserver(self)
    }
    
    func update(_ room: LNRoomSession?) {
        roomSession = room
        
        if let room {
            update(room.roomInfo)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNRoomTopMenuView: LNRoomViewModelNotify {
    func onRoomInfoChanged(session: LNRoomSession, roomInfo: LNRoomInfo) {
        guard roomSession == session else { return }
        guard let roomInfo = roomSession?.roomInfo else { return }
        update(roomInfo)
    }
}

extension LNRoomTopMenuView {
    private func update(_ roomInfo: LNRoomInfo) {
        hostAvatar.sd_setImage(with: URL(string: roomInfo.coverURL))
        roomNameLabel.text = roomInfo.liveName
        roomIdLabel.text = "ID \(roomInfo.liveID)"
    }
    
    private func setupViews() {
        let closeButton = UIButton()
        closeButton.setImage(.icShutdown, for: .normal)
        closeButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            roomSession?.leaveRoom()
        }), for: .touchUpInside)
        addSubview(closeButton)
        closeButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-10)
        }
        
        let roomInfo = buildRoomInfo()
        addSubview(roomInfo)
        roomInfo.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(10)
            make.verticalEdges.equalToSuperview()
        }
    }
    
    private func buildRoomInfo() -> UIView {
        let container = UIView()
        
        hostAvatar.contentMode = .scaleAspectFill
        hostAvatar.layer.cornerRadius = 15
        hostAvatar.clipsToBounds = true
        container.addSubview(hostAvatar)
        hostAvatar.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.leading.equalToSuperview()
            make.width.height.equalTo(30)
        }
        
        let textView = UIView()
        container.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(hostAvatar.snp.trailing).offset(8)
            make.trailing.equalToSuperview()
        }
        
        roomNameLabel.font = .heading_h5
        roomNameLabel.textColor = .text_1
        textView.addSubview(roomNameLabel)
        roomNameLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        roomIdLabel.font = .systemFont(ofSize: 8)
        roomIdLabel.textColor = .text_1
        textView.addSubview(roomIdLabel)
        roomIdLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.trailing.lessThanOrEqualToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalTo(roomNameLabel.snp.bottom).offset(2)
        }
        
        return container
    }
}
