//
//  LNOrderRoomManageSeatListPanel.swift
//  Gami
//
//  Created by OneeChan on 2026/3/13.
//

import Foundation
import UIKit
import SnapKit


class LNOrderRoomManageSeatListPanel: LNPopupView {
    private let tabView = LNOrderRoomManageSeatTabView()
    private let scrollView = UIScrollView()
    private let guestListView = LNOrderRoomManageSeatListView(tabType: .guest)
    private let playmateListView = LNOrderRoomManageSeatListView(tabType: .playmate)
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        containerHeight = .percent(0.62)
        
        setupViews()
    }
    
    func update(_ room: LNOrderRoomViewModel?) {
        guestListView.update(room: room)
        playmateListView.update(room: room)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

private extension LNOrderRoomManageSeatListPanel {
    func switchToTab(_ tab: LNRoomApplySeatType, animated: Bool) {
        let width = scrollView.bounds.width
        guard width > 0 else {
            return
        }
        
        let offsetX = tab == .guest ? 0 : width
        scrollView.setContentOffset(.init(x: offsetX, y: 0), animated: animated)
    }

    func syncCurrentTabWithScrollPosition() {
        let width = max(scrollView.bounds.width, 1)
        let targetTab: LNRoomApplySeatType = scrollView.contentOffset.x >= width * 0.5 ? .playmate : .guest
        tabView.update(tab: targetTab)
    }
}

extension LNOrderRoomManageSeatListPanel: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        syncCurrentTabWithScrollPosition()
    }
    
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        syncCurrentTabWithScrollPosition()
    }
}

extension LNOrderRoomManageSeatListPanel {
    private func setupViews() {
        container.backgroundColor = .fill_7
        
        tabView.onTabChanged = { [weak self] tab in
            guard let self else { return }
            switchToTab(tab, animated: true)
        }
        
        container.addSubview(tabView)
        tabView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(16)
            make.horizontalEdges.equalToSuperview().inset(33)
            make.height.equalTo(32)
        }
        
        scrollView.isPagingEnabled = true
        scrollView.delegate = self
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.bounces = false
        scrollView.alwaysBounceVertical = false
        container.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.top.equalTo(tabView.snp.bottom).offset(16)
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        let stackView = UIStackView()
        stackView.axis = .horizontal
        scrollView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalToSuperview()
        }
        
        stackView.addArrangedSubview(guestListView)
        guestListView.snp.makeConstraints { make in
            make.width.equalTo(scrollView)
        }
        
        stackView.addArrangedSubview(playmateListView)
        playmateListView.snp.makeConstraints { make in
            make.width.equalTo(scrollView.snp.width)
        }
    }
}
