//
//  TextAreaViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 1/10/23.
//

import Foundation
import Foundation
import UIKit
import VDS
import VDSColorTokens
import Combine

class  TextAreaViewController: BaseViewController<TextArea> {

    var disabledSwitch = Toggle()
    var requiredSwitch = Toggle()
    var labelTextField = TextField()
    var errorTextField = TextField()
    var helperTextField = TextField()
    var widthTextField = NumericField()
    var showErrorSwitch = Toggle()
    var tooltipTitleTextField = TextField()
    var tooltipContentTextField = TextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: component)

        setupPicker()
        setupModel()
    }
    
    override func allTextFields() -> [TextField]? { [labelTextField, errorTextField, helperTextField, widthTextField, tooltipTitleTextField, tooltipContentTextField] }
    
    override func setupForm(){
        super.setupForm()  
        addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch))
        addFormRow(label: "Required", view: .makeWrapper(for: requiredSwitch))
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Label Text", view: labelTextField)
        addFormRow(label: "Helper Text", view: helperTextField)
        addFormRow(label: "Error", view: .makeWrapper(for: showErrorSwitch))
        addFormRow(label: "Error Text", view: errorTextField)
        addFormRow(label: "Width", view: widthTextField)
        addFormRow(label: "ToolTip Title", view: tooltipTitleTextField)
        addFormRow(label: "ToolTip Content", view: tooltipContentTextField)
        
        requiredSwitch.onChange = { [weak self] sender in
            self?.component.required = sender.isOn
        }
        
        showErrorSwitch.onChange = { [weak self] sender in
            guard let self else { return }
            self.component.showError = sender.isOn
            if self.component.showError != sender.isOn {
                self.showErrorSwitch.isOn = self.component.showError
            }
        }
        
        disabledSwitch.onChange = { [weak self] sender in
            self?.component.disabled = sender.isOn
        }

        labelTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.labelText = text
            }.store(in: &subscribers)
        
        helperTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.helperText = text
            }.store(in: &subscribers)

        errorTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.errorText = text
            }.store(in: &subscribers)

        widthTextField
            .textPublisher
            .sink { [weak self] width in
                guard let width = Float(width) else { return }
                self?.component.width = CGFloat(width)
            }.store(in: &subscribers)
        
        tooltipTitleTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.tooltipTitle = text
            }.store(in: &subscribers)

        tooltipContentTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.tooltipContent = text
            }.store(in: &subscribers)
        
    }
    
    func setupModel() {
        component.width = 328
        component.labelText = "Street Address"
        component.helperText = "For example: 123 Verizon St"
        component.errorText = "Enter a valid address."
        component.tooltipTitle = "Check the formatting of your address"
        component.tooltipContent = "House/Building number then street name"

        component
            .publisher(for: .valueChanged)
            .sink { component in
                if let text = component.value {
                    print("text entry: \(text)")
                } else {
                    print("text entry: null")
                }
            }.store(in: &subscribers)
 
        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        disabledSwitch.isOn = component.disabled
        requiredSwitch.isOn = component.required
        labelTextField.text = component.labelText
        helperTextField.text = component.helperText
        showErrorSwitch.isOn = component.showError
        errorTextField.text = component.errorText
        tooltipTitleTextField.text = component.tooltipTitle
        tooltipContentTextField.text = component.tooltipContent
    }
    
    //Picker
    func setupPicker(){
        surfacePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }
    }
}

