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

import Foundation
import UIKit
import VDS
import VDSColorTokens
import Combine

class  CheckboxViewController: BaseViewController<CheckboxItem> {

    var disabledSwitch = Toggle()
    var labelTextField = TextField()
    var childTextField = TextField()
    var errorTextField = TextField()
    var showErrorSwitch = Toggle()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: component)
        
        setupPicker()
        setupModel()
    }
    
    override func allTextFields() -> [TextField]? { [labelTextField, childTextField, errorTextField] }

    override func setupForm(){
        super.setupForm()  
        addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch))
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Label Text", view: labelTextField)
        addFormRow(label: "Child Text", view: childTextField)
        addFormRow(label: "Error", view: .makeWrapper(for: showErrorSwitch))
        addFormRow(label: "Error Text", view: errorTextField)
        
        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)
        
        childTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.childText = text
            }.store(in: &subscribers)

        errorTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.errorText = text
            }.store(in: &subscribers)
    }
    
    func setupModel() {
        component.labelText = "Terms and conditions"
        component.childText = "I agree to Verizon's terms and conditions click here"
        component.errorText = "Error Text"
        
        component.onChange = { [weak self] checkbox in
            let alertController:UIAlertController = UIAlertController(title: "Alert",
                                                                      message: "\(checkbox.labelText!): \(checkbox.isSelected)",
                                                                      preferredStyle: UIAlertController.Style.alert)
            alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:nil))
            self?.present(alertController, animated: true)
            self?.showErrorSwitch.isOn = checkbox.showError
            print("checkbox selected: \(checkbox.isSelected)")
        }
        
        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        disabledSwitch.isOn = component.disabled
        labelTextField.text = component.labelText
        childTextField.text = component.childText
        showErrorSwitch.isOn = component.showError
        errorTextField.text = component.errorText
        
    }
            
    //Picker
    func setupPicker(){
        surfacePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }
    }
}

