//
//  LNGameCategoryListViewController.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/16.
//

import Foundation
import UIKit
import SnapKit


extension UIView {
    func pushToGameCategoryList(exclude: [String] = [], handler: @escaping ((LNGameTypeItemVO, LNGameCategoryItemVO) -> Void)) {
        let vc = LNGameCategoryListViewController(exclude: exclude)
        vc.handler = handler
        navigationController?.pushViewController(vc, animated: true)
    }
}


class LNGameCategoryListViewController: LNViewController {
    private let exclude: [String]
    private let tabView = LNGameCategoryTabView()
    private let listView = LNGameCategoryListView()
    
    var handler: ((LNGameTypeItemVO, LNGameCategoryItemVO) -> Void)?
    
    init(exclude: [String]) {
        self.exclude = exclude
        
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
        
        let curGameTypes = LNGameMateManager.shared.curGameTypes
        if !exclude.isEmpty {
            curGameTypes.forEach { type in
                type.children.removeAll { item in
                    exclude.contains(item.code)
                }
            }
        }
        listView.update(categories: curGameTypes)
        tabView.update(curGameTypes)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
}

extension LNGameCategoryListViewController: LNGameCategoryTabViewDelegate {
    func onGameCategoryTabView(view: LNGameCategoryTabView, didSelect select: LNGameTypeItemVO) {
        listView.scrollTo(category: select)
    }
}

extension LNGameCategoryListViewController: LNGameCategoryListViewDelegate {
    func onCategoryListView(view: LNGameCategoryListView, didScrollTo category: LNGameTypeItemVO) {
        tabView.select(category)
    }
    
    func onCategoryListView(view: LNGameCategoryListView, didSelect topCategory: LNGameTypeItemVO, category: LNGameCategoryItemVO) {
        handler?(topCategory, category)
    }
}

extension LNGameCategoryListViewController {
    private func setupViews() {
        title = .init(key: "A00031")
        
        let tabView = buildTabView()
        view.addSubview(tabView)
        tabView.snp.makeConstraints { make in
            make.leading.top.bottom.equalToSuperview()
            make.width.equalTo(95)
        }
        
        listView.delegate = self
        view.addSubview(listView)
        listView.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(tabView.snp.trailing)
        }
    }
    
    private func buildTabView() -> UIView {
        tabView.delegate = self
        tabView.backgroundColor = .primary_1
        tabView.layer.maskedCorners = .layerMaxXMinYCorner
        tabView.layer.cornerRadius = 12
        
        return tabView
    }
}
