import UIKit
import VMSDK_Core

protocol FloorSwitcherDelegate: AnyObject {
    func floorSwitcher(_ switcher: FloorSwitcherView, didSelectFloor floor: VMMSBaseFloor)
}

class FloorSwitcherView: UIView {

    weak var delegate: FloorSwitcherDelegate?

    private var floors: [VMMSBaseFloor] = []
    private var currentIndex: Int = 0

    private let prevButton = UIButton(type: .system)
    private let nextButton = UIButton(type: .system)
    private let floorLabel = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }

    override var intrinsicContentSize: CGSize {
        CGSize(width: UIView.noIntrinsicMetric, height: 50)
    }

    private func setup() {
        backgroundColor = UIColor.systemBackground.withAlphaComponent(0.9)
        layer.cornerRadius = 10
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.2
        layer.shadowRadius = 4
        layer.shadowOffset = CGSize(width: 0, height: -2)

        let config = UIImage.SymbolConfiguration(pointSize: 20, weight: .semibold)
        prevButton.setImage(UIImage(systemName: "chevron.left", withConfiguration: config), for: .normal)
        nextButton.setImage(UIImage(systemName: "chevron.right", withConfiguration: config), for: .normal)
        prevButton.addTarget(self, action: #selector(prevTapped), for: .touchUpInside)
        nextButton.addTarget(self, action: #selector(nextTapped), for: .touchUpInside)

        floorLabel.textAlignment = .center
        floorLabel.font = .systemFont(ofSize: 15, weight: .medium)
        floorLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)

        let stack = UIStackView(arrangedSubviews: [prevButton, floorLabel, nextButton])
        stack.axis = .horizontal
        stack.spacing = 12
        stack.alignment = .center
        stack.translatesAutoresizingMaskIntoConstraints = false
        addSubview(stack)

        NSLayoutConstraint.activate([
            stack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
            stack.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
            stack.topAnchor.constraint(equalTo: topAnchor, constant: 8),
            stack.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8),
            prevButton.widthAnchor.constraint(equalToConstant: 44),
            nextButton.widthAnchor.constraint(equalToConstant: 44),
        ])
    }

    func configure(floors: [VMMSBaseFloor], currentFloor: VMMSBaseFloor?) {
        self.floors = floors
        if let current = currentFloor,
           let idx = floors.firstIndex(where: { $0.uid == current.uid }) {
            currentIndex = idx
        } else {
            currentIndex = 0
        }
        updateLabel()
    }

    private func updateLabel() {
        guard !floors.isEmpty else {
            floorLabel.text = "No floors"
            prevButton.isEnabled = false
            nextButton.isEnabled = false
            return
        }
        let floor = floors[currentIndex]
        floorLabel.text = (floor.name.isEmpty == false) ? floor.name : floor.uid
        prevButton.isEnabled = floors.count > 1
        nextButton.isEnabled = floors.count > 1
    }

    @objc private func prevTapped() {
        guard !floors.isEmpty else { return }
        currentIndex = (currentIndex - 1 + floors.count) % floors.count
        updateLabel()
        delegate?.floorSwitcher(self, didSelectFloor: floors[currentIndex])
    }

    @objc private func nextTapped() {
        guard !floors.isEmpty else { return }
        currentIndex = (currentIndex + 1) % floors.count
        updateLabel()
        delegate?.floorSwitcher(self, didSelectFloor: floors[currentIndex])
    }
}
