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

import Foundation
import UIKit
import SnapKit


protocol LNGameCategoryTabViewDelegate: AnyObject {
    func onGameCategoryTabView(view: LNGameCategoryTabView, didSelect category: LNGameTypeItemVO)
}


class LNGameCategoryTabView: UIView {
    private let stackView = UIStackView()
    private let indicator = UIImageView()
    private var itemViews: [LNGameCategoryTabItemView] = []
    
    weak var delegate: LNGameCategoryTabViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ categories: [LNGameTypeItemVO]) {
        itemViews.forEach {
            stackView.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        itemViews.removeAll()
        
        for category in categories {
            let itemView = LNGameCategoryTabItemView()
            itemView.update(category)
            itemView.onTap { [weak self] in
                guard let self else { return }
                self.selectCategory(itemView)
            }
            stackView.addArrangedSubview(itemView)
            itemViews.append(itemView)
        }
        layoutIfNeeded()
        
        if let first = itemViews.first {
            selectCategory(first)
        }
    }
    
    func select(_ category: LNGameTypeItemVO) {
        let view = itemViews.first { $0.item?.code == category.code }
        guard let view else { return }
        selectCategory(view, notify: false)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNGameCategoryTabView {
    private func selectCategory(_ itemView: LNGameCategoryTabItemView, notify: Bool = true) {
        guard !itemView.isSelected,
              let item = itemView.item else { return }
        
        itemViews.forEach {
            $0.setSelected(itemView == $0)
        }
        
        indicator.snp.remakeConstraints { make in
            make.centerY.equalTo(itemView)
            make.height.equalTo(24)
            make.horizontalEdges.equalToSuperview()
        }
        
        UIView.animate(withDuration: 0.3) { [weak self] in
            guard let self else { return }
            self.layoutIfNeeded()
        }
        
        if notify {
            delegate?.onGameCategoryTabView(view: self, didSelect: item)
        }
    }
}

extension LNGameCategoryTabView {
    private func setupViews() {
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let fakeView = UIView()
        scrollView.addSubview(fakeView)
        fakeView.snp.makeConstraints { make in
            make.leading.trailing.top.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(0)
        }
        
        stackView.axis = .vertical
        stackView.spacing = 20
        stackView.alignment = .center
        scrollView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(11)
            make.trailing.equalToSuperview().offset(-11)
            make.top.equalToSuperview().offset(16)
            make.bottom.equalToSuperview()
        }
        
        indicator.image = UIImage.primary_7
        indicator.layer.cornerRadius = 12
        indicator.clipsToBounds = true
        stackView.addSubview(indicator)
    }
}

private class LNGameCategoryTabItemView: UIView {
    private let stackView = UIStackView()
    private let nameLabel = UILabel()
    
    private(set) var item: LNGameTypeItemVO?
    private(set) var isSelected: Bool = false
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ item: LNGameTypeItemVO) {
        nameLabel.text = item.name
        self.item = item
    }
    
    func setSelected(_ selected: Bool) {
        nameLabel.textColor = selected ? .text_1 : .text_4
        isSelected = selected
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNGameCategoryTabItemView {
    private func setupViews() {
        stackView.axis = .horizontal
        stackView.spacing = 0
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(8)
            make.trailing.equalToSuperview().offset(-8)
            make.top.equalToSuperview().offset(4)
            make.bottom.equalToSuperview().offset(-4)
        }
        
        nameLabel.textAlignment = .center
        nameLabel.font = .heading_h5
        nameLabel.textColor = .text_4
        addSubview(nameLabel)
        stackView.addArrangedSubview(nameLabel)
    }
}


#if DEBUG

import SwiftUI

struct LNGameCategoryTabItemViewPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNGameCategoryTabItemView()
        view.backgroundColor = .red
        container.addSubview(view)
        view.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        view.setSelected(true)
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

#Preview(body: {
    LNGameCategoryTabItemViewPreview()
})

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

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