//
//  SimpleBtnView.swift
//  AcaMate
//
//  Created by TAnine on 2/4/25.
//

import SwiftUI
//import combine


struct SimpleBtnView: View {
    @ObservedObject var vm: ButtonViewModel
    let id: UUID
    
    var body: some View {
        if let state = vm.btnStates[id] {
            if let title = state.text, let font = state.font {
                Text("\(title)")
                    .font(font)
                    .lineLimit(1)
                    .minimumScaleFactor(0.5)
                    .multilineTextAlignment(.center)
                    .truncationMode(.tail)
                    .foregroundStyle(state.textColor)
                    .frame(width: state.width, height: state.height)
                    .onTapGesture {
                        if state.isUsable {
                            guard let action = state.action else { return }
                            action()
                        }
                    }
            }
            else {
                Button{
                    guard let action = state.action else { return }
                    action()
                } label: {
                    if let image = state.image {
                        image
                            .resizable()
                            .renderingMode(.template)
                            .frame(width: state.width, height: state.height)
                            .foregroundStyle(state.isUsable ? Color(.Second.normal) : Color(.Normal.normal))
                    }
                }
                .disabled(!state.isUsable)
            }
        } else {
            EmptyView()
        }
    }
}
