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

import Foundation
import UIKit
import SnapKit


class LNOrderRoomSeatsView: UIView {
    private let hostSeat = LNOrderRoomUserSeatView(seatNum: .host)
    private let guestSeat = LNOrderRoomUserSeatView(seatNum: .guest)
    private var playMateSeats: [LNOrderRoomUserSeatView] = []
    
    private weak var roomSession: LNOrderRoomViewModel?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ room: LNOrderRoomViewModel?) {
        roomSession = room
        
        hostSeat.update(room)
        guestSeat.update(room)
        playMateSeats.forEach { $0.update(room) }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNOrderRoomSeatsView {
    private func setupViews() {
        let topView = buildTopView()
        addSubview(topView)
        topView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(20)
            make.top.equalToSuperview()
        }
        
        let seats = buildSeatsView()
        addSubview(seats)
        seats.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(20)
            make.top.equalTo(topView.snp.bottom).offset(20)
            make.bottom.equalToSuperview()
        }
    }
    
    private func buildTopView() -> UIView {
        let stackView = UIStackView()
        stackView.distribution = .equalSpacing
        stackView.axis = .horizontal
        LNOrderRoomGuideView.mainSeatView = stackView
        
        // 主持人位置
        stackView.addArrangedSubview(hostSeat)
        
        // 中间介绍界面
        let orderView = UIView()
        orderView.onTap { [weak self] in
            guard let self else { return }
            guard let view = viewController?.view else { return }
            LNOrderRoomGuideView.show(view)
        }
        stackView.addArrangedSubview(orderView)
        
        let orderTag = UIImageView()
        orderTag.image = .icRoomOrderIntroduce.resizableImage(withCapInsets: .init(top: 0, left: 55, bottom: 0, right: 55))
        orderView.addSubview(orderTag)
        orderTag.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.verticalEdges.equalToSuperview()
            make.width.greaterThanOrEqualTo(130)
        }
        
        let textView = UIView()
        textView.isUserInteractionEnabled = false
        textView.alpha = 0.8
        orderTag.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(4)
            make.centerX.equalToSuperview()
            make.leading.greaterThanOrEqualToSuperview().offset(25)
        }
        
        let textLabel = UILabel()
        textLabel.font = .body_xs
        textLabel.textColor = .text_2
        textLabel.text = .init(key: "A00327")
        textView.addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.leading.equalToSuperview()
        }
        
        let arrow = UIImageView.arrowImageView(size: 8)
        arrow.tintColor = .text_2
        textView.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(textLabel.snp.trailing).offset(5)
            make.trailing.equalToSuperview()
        }
        
        // 土豪金座位
        stackView.addArrangedSubview(guestSeat)
        
        return stackView
    }
    
    private func buildSeatsView() -> UIView {
        let seatsView = LNMultiLineStackView()
        seatsView.columns = 4
        seatsView.spacing = 20
        seatsView.itemDistribution = .equalSpacing
        LNOrderRoomGuideView.playmateView = seatsView
        
        for seat in LNOrderRoomSeatNum.allCases {
            if case .mic1 ... .mic8 = seat {
                let seat = LNOrderRoomUserSeatView(seatNum: seat)
                seat.update(roomSession)
                playMateSeats.append(seat)
            }
        }
        seatsView.update(playMateSeats)
        
        return seatsView
    }
}
