//
//  Toggle.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 12/4/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import MVMCore
import MVMCoreUI
import UIKit
import VDS

extension MoleculeViewProtocol  {
    public func onModelChange(model: MoleculeModelProtocol) {
        if let backgroundColor = model.backgroundColor {
           self.backgroundColor = backgroundColor.uiColor
        }
        
        if let accessibilityIdentifier = model.accessibilityIdentifier {
            self.accessibilityIdentifier = accessibilityIdentifier
        }
    }
}

extension Modelable where Self: MoleculeViewProtocol {
    public init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
        self.init(with: model as! ModelType)
        self.set(with: model, delegateObject, additionalData)
    }
}

open class TestToggle: VDSToggleBase<TestToggleModel>, MoleculeViewProtocol, MVMCoreViewProtocol {
    
    /// The state on the toggle. Default value: false.
    open override var isOn: Bool {
        didSet {
            _ = FormValidator.validate(delegate: delegateObject?.formHolderDelegate)
        }
    }
    
    open override var isEnabled: Bool {
        didSet {
            model.enabled = isEnabled && !model.readOnly
        }
    }
    
    //--------------------------------------------------
    // MARK: - Delegate
    //--------------------------------------------------
    
    private var delegateObject: MVMCoreUIDelegateObject?
       
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    open override func onStateChange(viewModel: ModelType) {
        super.onStateChange(viewModel: viewModel)
        onModelChange(model: viewModel)
    }
    
    // MARK:- MoleculeViewProtocol
    public func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        
        guard let castedModel = model as? ModelType else { return }
        set(with: castedModel)
        
        self.delegateObject = delegateObject
        
        guard let formFieldProtocol = model as? FormFieldProtocol else { return }
        FormValidator.setupValidation(for: formFieldProtocol, delegate: delegateObject?.formHolderDelegate)

        let additionalDataWithSource = additionalData.dictionaryAdding(key: KeySourceModel, value: model)
        if castedModel.action != nil || castedModel.alternateAction != nil {
            onChange = { [weak self] in
                guard let self = self else { return }
                if self.isOn {
                    if let action = castedModel.action {
                        MVMCoreActionHandler.shared()?.asyncHandleAction(with: action, additionalData: additionalDataWithSource, delegateObject: delegateObject)
                    }
                } else {
                    if let action = castedModel.alternateAction ?? castedModel.action {
                        MVMCoreActionHandler.shared()?.asyncHandleAction(with: action, additionalData: additionalDataWithSource, delegateObject: delegateObject)
                    }
                }
            }
        }
    }
            
    public static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 24
    }
}

// MARK: - MVMCoreUIViewConstrainingProtocol
extension TestToggle: MVMCoreUIViewConstrainingProtocol {
    
    public func needsToBeConstrained() -> Bool { true }
    
    public func horizontalAlignment() -> UIStackView.Alignment { .trailing }
}
