//
//  StateLabelModel.swift
//  MVMCoreUI
//
//  Created by Matt Bruce on 12/14/21.
//  Copyright © 2021 Verizon Wireless. All rights reserved.
//

import Foundation

public class FormLabelModel: EnableableModelProtocol {
    static let defaultFontStyle = Styler.Font.RegularMicro
    static let defaultEnabledTextColor = Color(uiColor: .mvmBlack)
    static let defaultDisabledTextColor = Color(uiColor: .mvmCoolGray3)
    static let defaultRequiredTextColor = Color(uiColor: .mvmCoolGray6)

    private var enabledModel: LabelModel
    private var disabledModel   = LabelModel(fontStyle: FormLabelModel.defaultFontStyle, textColor: FormLabelModel.defaultDisabledTextColor)

    //current state
    public var enabled: Bool = true

    //model is based on current state
    public var model: LabelModel {
        return enabled ?  enabledModel: disabledModel
    }

    //init
    public init(model: LabelModel){
       
        //ensure the fontStyle exist and if so,
        //make sure the disabledModel font matches
        //otherwise set it to defaultFontStyle
        if let modelFontStyle = model.fontStyle {
            self.disabledModel.fontStyle = modelFontStyle
        } else {
            model.fontStyle = FormLabelModel.defaultFontStyle
        }
        
        //ensure the textColor is set
        //otherwise use the defaultEnabledTextColor
        if model.textColor == nil {
            model.textColor = FormLabelModel.defaultEnabledTextColor
        }
        
        //set the enabledModel to the model passed in
        self.enabledModel = model
                
        //make sure the enabled & disabled text match
        self.disabledModel.text = self.enabledModel.text
    }
    
    public init(text: String){
        //create the enabled model
        self.enabledModel = LabelModel(fontStyle: FormLabelModel.defaultFontStyle, textColor: FormLabelModel.defaultEnabledTextColor)
        self.enabledModel.text = text
        
        //make sure the enabled & disabled text match
        self.disabledModel.text = self.enabledModel.text
    }
    
    public var requiredModel: LabelModel {
        let required = LabelModel(fontStyle: model.fontStyle!, textColor: model.textColor!)
        if enabled {
            required.attributes = [LabelAttributeColorModel(FormLabelModel.defaultRequiredTextColor, model.text.count + 1, 8)]
        }
        required.text = "\(model.text) Optional"
        return required
    }
    
    //methods
    public func reset(){
        self.enabled = true
    }
    
    //set text for state
    public func set(text:String){
        self.enabledModel.text = text
        self.disabledModel.text = text
    }
}
