//
//  LNHomeGameMatePanel.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/17.
//

import Foundation
import UIKit
import SnapKit


class LNHomeGameMatePanel: UIView {
    private var curItem: LNGameTypeItemVO?
    private let hallView = UIView()
    private let listView = LNGameMateListView()
    
    init(item: LNGameTypeItemVO) {
        super.init(frame: .zero)
        
        setupViews(item)
        curItem = item
        
        LNEventDeliver.addObserver(self)
        onHallEntranceChanged()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func onShow() {
        listView.reportExposure()
    }
}

extension LNHomeGameMatePanel: LNRoomManagerNotify {
    func onHallEntranceChanged() {
        hallView.isHidden = !LNRoomManager.hallEntrance
    }
}

extension LNHomeGameMatePanel: LNHomeGameTabViewDelegate {
    func homeGameTabView(view: LNHomeGameTabView, didSelect category: LNGameCategoryItemVO?) {
        guard let curItem else { return }
        listView.reloadList(newTopCategory: curItem.code, newCategory: category?.code, filter: LNGameMateFilter())
    }
    
    func homeGameTabViewClickMore(view: LNHomeGameTabView) {
        pushToGameCategoryList { [weak self] category, game in
            guard let self else { return }
            pushToGameMateList(topCategory: category, category: game, filter: LNGameMateFilter())
        }
    }
}

extension LNHomeGameMatePanel: LNHomeActivityTabViewDelegate {
    func homeActivityTabView(view: LNHomeActivityTabView, didSelect category: LNGameCategoryItemVO?) {
        guard let curItem else { return }
        listView.reloadList(newTopCategory: curItem.code, newCategory: category?.code, filter: LNGameMateFilter())
    }
    
    func homeActivityTabViewClickMore(view: LNHomeActivityTabView) {
        pushToGameCategoryList { [weak self] category, game in
            guard let self else { return }
            pushToGameMateList(topCategory: category, category: game, filter: LNGameMateFilter())
        }
    }
}

extension LNHomeGameMatePanel {
    private func setupViews(_ item: LNGameTypeItemVO) {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 8
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let tabView = UIView()
        if item.categoryType == .offline
            || item.categoryType == .user {
            let gameTab = LNHomeActivityTabView()
            gameTab.delegate = self
            tabView.addSubview(gameTab)
            gameTab.snp.makeConstraints { make in
                make.horizontalEdges.equalToSuperview().inset(16)
                make.verticalEdges.equalToSuperview()
            }
            gameTab.update(item.children)
        } else {
            let gameTab = LNHomeGameTabView()
            gameTab.delegate = self
            tabView.addSubview(gameTab)
            gameTab.snp.makeConstraints { make in
                make.horizontalEdges.equalToSuperview().inset(16)
                make.verticalEdges.equalToSuperview()
                make.height.equalTo(78)
            }
            gameTab.update(item.children)
        }
        stackView.addArrangedSubview(tabView)
        
        stackView.addArrangedSubview(buildHall())
        
        stackView.addArrangedSubview(listView)
        listView.reloadList(newTopCategory: item.code, newCategory: nil, filter: LNGameMateFilter())
    }
    
    private func buildHall() -> UIView {
        hallView.onTap { [weak self] in
            guard let self else { return }
            LNRoomManager.shared.getRandomRoom { [weak self] info in
                guard let self else { return }
                guard let info else { return }
                pushToRoom(info.roomId)
            }
        }
        
        let image: UIImage = .icHomeHall
        let bg = UIImageView()
        bg.image = image.resizableImage(withCapInsets: .init(top: 0, left: image.size.width * 0.5 - 1,
                                                             bottom: 0, right: image.size.width * 0.5 - 1))
        hallView.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.verticalEdges.equalToSuperview()
            make.height.equalTo(42)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00370")
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_1
        hallView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(bg).offset(61)
        }
        
        return hallView
    }
}
