//
//  ButtonViewModel.swift
//  AcaMate
//
//  Created by TAnine on 2/6/25.
//

import SwiftUI
import Combine

class ButtonViewModel: ObservableObject {
    @Published var btnStates: [UUID: ButtonState] = [:]
    
    func setImage(for id: UUID, newImage: Image){
        var state = btnStates[id] ?? ButtonState()
        state.image = newImage
        btnStates[id] = state
        objectWillChange.send()
    }
    
    func setText(for id: UUID, newText: String?, newFont: Font?) {
        var state = btnStates[id] ?? ButtonState()
        
        state.text = newText
        state.font = newFont

        btnStates[id] = state
        objectWillChange.send()
    }
    
    func setSize(for id: UUID, newWidth: CGFloat?, newHeight: CGFloat?){
        var state = btnStates[id] ?? ButtonState()
        
        state.width = newWidth ?? 0
        state.height = newHeight ?? 0
        
        btnStates[id] = state
        objectWillChange.send()
    }
    
    func setAction(for id: UUID, newAction: @escaping VOID_TO_VOID) {
        var state = btnStates[id] ?? ButtonState()
        state.action = newAction
        btnStates[id] = state
        objectWillChange.send()
    }
    
    func setTextColor(for id: UUID, newColor: Color) {
        var state = btnStates[id] ?? ButtonState()
        
        state.textColor = newColor
        
        btnStates[id] = state
        objectWillChange.send()
    }
    
    func setIsUsable(for id: UUID, newValue: Bool) {
        var state = btnStates[id] ?? ButtonState()
        state.isUsable = newValue
        btnStates[id] = state
        objectWillChange.send()
    }
//    
//    func setDisable(for id: UUID, _ newValue: Bool) {
//        var state = btnStates[id] ?? ButtonState()
//        state.isUsable = newValue
//        btnStates[id] = state
//        objectWillChange.send()
//    }
    
    // -- CircleBtn 전용 -- //
    
    func setIsSelected(for id: UUID, newValue: Bool){
        var state = btnStates[id] ?? ButtonState()
        state.isSelected = newValue
        state.foreColor = newValue ? Color(.Second.normal) : Color(.Disable.normal)
        btnStates[id] = state
        objectWillChange.send()
    }
    
    func setIsReverse(for id: UUID, newValue: Bool){
        var state = btnStates[id] ?? ButtonState()
        state.isReverse = newValue
        if state.isReverse {
            state.backColor = Color(.Second.light)
        } else {
            state.backColor = Color(.Normal.normal)
        }
        btnStates[id] = state
        objectWillChange.send()
    }
    
//    func setBackColor(for id: UUID, newColor: Color) {
//        var state = btnStates[id] ?? ButtonState()
//        
//        state.backColor = newColor
//        
//        btnStates[id] = state
//        objectWillChange.send()
//    }
//    func setForeColor(for id: UUID, newColor: Color) {
//        var state = btnStates[id] ?? ButtonState()
//        
//        state.foreColor = newColor
//        
//        btnStates[id] = state
//        objectWillChange.send()
//    }
}
