//
//  LNHomeActivityTabView.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/21.
//

import Foundation
import UIKit
import SnapKit


protocol LNHomeActivityTabViewDelegate: AnyObject {
    func homeActivityTabView(view: LNHomeActivityTabView, didSelect category: LNGameCategoryItemVO?)
    func homeActivityTabViewClickMore(view: LNHomeActivityTabView)
}


class LNHomeActivityTabView: UIView {
    private(set) var curGame: LNGameCategoryItemVO? {
        didSet {
            guard oldValue?.code != curGame?.code else { return }
            tabItemViews.forEach {
                $0.setSelected(curGame?.code == $0.item?.code)
            }
            delegate?.homeActivityTabView(view: self, didSelect: curGame)
        }
    }
    
    private let stackView = UIStackView()
    private var tabItemViews: [LNHomeActivityTabItemView] = []
    
    weak var delegate: LNHomeActivityTabViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ games: [LNGameCategoryItemVO]) {
        let itemViews = stackView.arrangedSubviews
        itemViews.forEach {
            stackView.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        tabItemViews.removeAll()
        
        games.prefix(2).forEach { item in
            let itemView = LNHomeActivityTabItemView()
            itemView.onTap { [weak self] in
                guard let self else { return }
                if self.curGame?.code == item.code {
                    self.curGame = nil
                } else {
                    self.curGame = item
                }
            }
            itemView.update(item)
            itemView.setSelected(false)
            stackView.addArrangedSubview(itemView)
            tabItemViews.append(itemView)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNHomeActivityTabView {
    private func setupViews() {
        let moreView = LNHomeActivityMoreTabView()
        moreView.onTap { [weak self] in
            guard let self else { return }
            self.delegate?.homeActivityTabViewClickMore(view: self)
        }
        addSubview(moreView)
        moreView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.spacing = 2
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.verticalEdges.equalToSuperview()
            make.trailing.equalTo(moreView.snp.leading).offset(-2)
        }
    }
}


#if DEBUG

import SwiftUI

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

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



