//
//  TextLinkCaretViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 11/2/22.
//

import Foundation
import UIKit
import VDS
import VDSColorTokens

class TextLinkCaretViewController: BaseViewController<TextLinkCaret> {
    
    lazy var caretPositionPickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: TextLinkCaret.IconPosition.allCases)
    }()
    
    var label = Label()
    var disabledSwitch = Toggle()
    var textField = TextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: .makeWrapper(for: component, edgeSpacing: 16.0), edgeSpacing: 0.0)

        setupPicker()
        setupModel()
    }
    
    override func allTextFields() -> [TextField]? { [textField] }

    override func setupForm(){
        super.setupForm()  
        addFormRow(label: "Button Action", view: label)
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch))
        addFormRow(label: "Label", view: textField)
        addFormRow(label: "Caret Position", view: caretPositionPickerSelectorView)
        
        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)
    }
    
    func setupModel() {
        component.text = "Text Link Caret"
        component.labelPublisher(label)
        
        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        disabledSwitch.isOn = component.disabled
        textField.text = component.text
        caretPositionPickerSelectorView.text = component.iconPosition.rawValue
    }
      
    func setupPicker(){
        
        surfacePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }
        
        caretPositionPickerSelectorView.onPickerDidSelect = { [weak self] item in
            self?.component.iconPosition = item
        }
    }
}
