import SwiftUI

class NotificationBadgeViewModel: ObservableObject {
    @Published private(set) var viewCnt: Int = 0
    @Published private(set) var hasUnread: Bool = false

    init(viewCnt: Int = 0) {
        self.viewCnt = viewCnt
    }

    //MARK: - 未読カウントを更新
    func updateViewCnt(to newValue: Int) {
        viewCnt = newValue
        hasUnread = newValue > 0
    }

    //MARK: - バッジをリセット（既読状態にする）
    func resetBadge() {
        viewCnt = 0
        hasUnread = false
    }

    //MARK: - 現在の未読数を取得
    func getUnreadCount() -> Int {
        return viewCnt
    }
}

struct NotificationBadge: View {
    @ObservedObject var viewModel: NotificationBadgeViewModel
    let font: Font

    var body: some View {
        Group {
            if viewModel.getUnreadCount() > 0 {
                Ellipse()
                    .fill(Color.red)
                    .frame(width: 12, height: 12)
                    .overlay(
                        Text(viewModel.getUnreadCount() < 10 ? "\(viewModel.getUnreadCount())" : "-")
                            .font(font)
                            .foregroundColor(.white)
                    )
            }
        }
    }
}
