//
//  ButtonViewController.swift
//  VDSSample
//
//  Created by Jarrod Courtney on 9/16/22.
//

import Foundation
import UIKit
import VDS
import VDSColorTokens

class ButtonViewController: BaseViewController<Button> {
    
    lazy var usePickerSelectorView = {
        PickerSelectorView<Use>(title: "",
                                 picker: self.picker,
                                 items: [.primary, .secondary])
    }()
    
    lazy var buttonSizePickerSelectorView = {
        PickerSelectorView(title: "",
                            picker: self.picker,
                            items: ButtonSize.allCases)
    }()

    var label = Label()
    var disabledSwitch = Toggle()
    var textField = TextField()
    var widthTextField = NumericField()

    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: .makeWrapper(for: component, edgeSpacing: 16.0), edgeSpacing: 0.0)
        
        setupPicker()
        setupModel()
    }
    
    override func allTextFields() -> [TextField]? { [textField, widthTextField] }

    override func setupForm(){
        super.setupForm()  
        addFormRow(label: "Button Action", view: label)
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Use", view: usePickerSelectorView)
        addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch))
        addFormRow(label: "Text", view: textField)
        addFormRow(label: "Width", view: widthTextField)
        addFormRow(label: "Size", view: buttonSizePickerSelectorView)
        
        
        disabledSwitch.onChange =  { [weak self] sender in
            self?.component.disabled = sender.isOn
        }

        textField
            .textPublisher
            .sink { [weak self] text in
                self?.component.text = text
            }.store(in: &subscribers)
        
        widthTextField
            .textPublisher
            .sink { [weak self] text in
                if let n = NumberFormatter().number(from: text) {
                    self?.component.width = CGFloat(truncating: n)
                }
            }.store(in: &subscribers)

    }
    
    func setupModel() {        
        //setup UI
        component.text = "Button"
        component.labelPublisher(label)
        
        surfacePickerSelectorView.text = component.surface.rawValue
        disabledSwitch.isOn = component.disabled
        textField.text = component.text
        usePickerSelectorView.text = component.use.rawValue
        widthTextField.text = ""
        buttonSizePickerSelectorView.text = ButtonSize.large.rawValue
    }
      
    func setupPicker(){
        
        surfacePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }
        
        usePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.use = item
            self?.component.backgroundColor = item.color
        }
        
        buttonSizePickerSelectorView.onPickerDidSelect = { [weak self] item in
            self?.component.size = item
            self?.debugViewSwitch.toggle()
        }
    }
}
