//
//  LNOrderRoomManageSeatTabView.swift
//  Gami
//
//  Created by OneeChan on 2026/3/19.
//

import Foundation
import UIKit
import SnapKit


class LNOrderRoomManageSeatTabView: UIView {
    private let guestTabButton = UIButton()
    private let playmateTabButton = UIButton()
    private let tabSelectionView = UIView()
    private var tabSelectionLeadingConstraint: Constraint?
    
    private(set) var curTab: LNRoomApplySeatType? = .guest
    
    var onTabChanged: ((LNRoomApplySeatType) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        updateSelectionIndicator(animated: false)
    }
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        if window != nil, curTab == nil {
            curTab = .guest
        }
    }
    
    func update(tab: LNRoomApplySeatType) {
        curTab = tab
        updateButtonStates()
        updateSelectionIndicator()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

private extension LNOrderRoomManageSeatTabView {
    func setupViews() {
        backgroundColor = .fill.withAlphaComponent(0.2)
        layer.cornerRadius = 16
        clipsToBounds = true
        
        tabSelectionView.backgroundColor = .primary_1
        tabSelectionView.layer.cornerRadius = 13
        tabSelectionView.isUserInteractionEnabled = false
        addSubview(tabSelectionView)
        tabSelectionView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            tabSelectionLeadingConstraint = make.leading.equalToSuperview().offset(3).constraint
            make.width.equalTo(snp.width).multipliedBy(0.5).offset(-6)
            make.height.equalTo(26)
        }
        
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        guestTabButton.setTitle(.init(key: "A00331"), for: .normal)
        guestTabButton.titleLabel?.font = .heading_h4
        guestTabButton.addAction(UIAction(handler: { [weak self] _ in
            self?.handleTabTap(.guest)
        }), for: .touchUpInside)
        stackView.addArrangedSubview(guestTabButton)
        
        playmateTabButton.setTitle(.init(key: "A00332"), for: .normal)
        playmateTabButton.titleLabel?.font = .heading_h4
        playmateTabButton.addAction(UIAction(handler: { [weak self] _ in
            self?.handleTabTap(.playmate)
        }), for: .touchUpInside)
        stackView.addArrangedSubview(playmateTabButton)
        
        updateButtonStates()
    }
    
    func updateButtonStates() {
        let normalColor = UIColor.text_2
        let selectedColor = UIColor.text_5
        
        guestTabButton.setTitleColor(curTab == .guest ? selectedColor : normalColor, for: .normal)
        playmateTabButton.setTitleColor(curTab == .playmate ? selectedColor : normalColor, for: .normal)
    }
    
    func updateSelectionIndicator(animated: Bool = true) {
        let offset = curTab == .guest ? 3 : bounds.width / 2 + 3
        tabSelectionLeadingConstraint?.update(offset: offset)
        guard animated else {
            layoutIfNeeded()
            return
        }
        
        UIView.animate(withDuration: 0.25) { [weak self] in
            self?.layoutIfNeeded()
        }
    }
    
    func handleTabTap(_ tab: LNRoomApplySeatType) {
        guard curTab != tab else { return }
        curTab = tab
        updateButtonStates()
        updateSelectionIndicator()
        onTabChanged?(tab)
    }
}
