//
//  MainScanTabItem.swift
//  suixinkan
//

import UIKit

/// 主 TabBar 中间扫码核销入口。
enum MainScanTabItem {

    /// 构建扫码 TabBarItem，点击行为由 `MainTabBarController` 拦截。
    static func makeTabBarItem() -> UITabBarItem {
        let image = makeScanImage()
        let item = UITabBarItem(title: nil, image: image, selectedImage: image)
        item.accessibilityLabel = "扫码核销"
        item.accessibilityIdentifier = "main.scan"
        return item
    }

    private static func makeScanImage() -> UIImage {
        let diameter: CGFloat = 48
        let renderer = UIGraphicsImageRenderer(size: CGSize(width: diameter, height: diameter))
        return renderer.image { _ in
            let bounds = CGRect(origin: .zero, size: CGSize(width: diameter, height: diameter))
            AppColor.primary.setFill()
            UIBezierPath(ovalIn: bounds).fill()

            let configuration = UIImage.SymbolConfiguration(pointSize: 25, weight: .semibold)
            guard let symbol = UIImage(systemName: "qrcode.viewfinder", withConfiguration: configuration) else {
                return
            }
            let image = symbol.withTintColor(.white, renderingMode: .alwaysOriginal)
            let symbolSize = CGSize(width: 28, height: 28)
            let symbolRect = CGRect(
                x: (diameter - symbolSize.width) / 2,
                y: (diameter - symbolSize.height) / 2,
                width: symbolSize.width,
                height: symbolSize.height
            )
            image.draw(in: symbolRect)
        }.withRenderingMode(.alwaysOriginal)
    }
}
