//
//  ToggleViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 8/1/22.
//

import Foundation
import UIKit
import VDS
import VDSColorTokens

class ToggleViewController: BaseViewController<Toggle> {
    
    lazy var textSizePickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: Toggle.TextSize.allCases)
        
    }()
    
    lazy var textPositionPickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: Toggle.TextPosition.allCases)
    }()

    var disabledSwitch = Toggle()
    var showTextSwitch = Toggle()

    var textFormStackView = FormSection()
    
    var boldSwitch = Toggle()
    var onTextField = TextField()
    var offTextField = TextField()

    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: .makeWrapper(for: component))
                
        setupPicker()
    }
    
    override func allTextFields() -> [TextField]? { [onTextField, offTextField] }

    override func setupForm() {
        super.setupForm()
        
        addFormRow(label: "Show Text", view: .makeWrapper(for: showTextSwitch))
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch))
        
        //add the hidden elements to textFormStackView
        textFormStackView.addFormRow(label: "Bold", view: .makeWrapper(for: boldSwitch))
        textFormStackView.addFormRow(label: "Text Size", view: textSizePickerSelectorView)
        textFormStackView.addFormRow(label: "Text Position", view: textPositionPickerSelectorView)
        textFormStackView.addFormRow(label: "On Text", view: onTextField)
        textFormStackView.addFormRow(label: "Off Text", view: offTextField)
        
        //add textFormStackView to main form
        append(section: textFormStackView)
        
        component.onChange = { [weak self] toggle in
            let alertController:UIAlertController = UIAlertController(title: "Alert",
                                                                      message: "Toggle Value: \(toggle.isOn)",
                                                                      preferredStyle: UIAlertController.Style.alert)
            alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:nil))
            self?.present(alertController, animated: true)
            print("toggle changed: \(toggle.isOn)")
        }
        
        showTextSwitch.onChange = { [weak self] sender in
            self?.component.showText = sender.isOn
            self?.textFormStackView.isHidden = !sender.isOn
        }
        
        disabledSwitch.onChange = { [weak self] sender in
            self?.component.disabled = sender.isOn
        }
        
        boldSwitch.onChange = { [weak self] sender in
            self?.component.textWeight = sender.isOn ? .bold : .regular
        }

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

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

        textFormStackView.isHidden = true
        
        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        textSizePickerSelectorView.text = component.textSize.rawValue
        textPositionPickerSelectorView.text = component.textPosition.rawValue
        onTextField.text = component.onText
        offTextField.text = component.offText
    }
    
    //Picker        
    func setupPicker(){
        
        surfacePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }
                
        textSizePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.textSize = item
        }

        textPositionPickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.textPosition = item
        }
        
    }
}
