//
//  CheckBoxGroup2ViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 8/24/22.
//

import Foundation
import UIKit
import VDS
import VDSColorTokens
import Combine

class  CheckboxGroupViewController: BaseViewController<CheckboxGroup> {
        
    var disabledSwitch = Toggle()
    var labelTextField = TextField()
    var childTextField = TextField()
    var showErrorSwitch = Toggle()

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

    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))
        
        showErrorSwitch.onChange = { [weak self] sender in
            self?.component.showError = sender.isOn
        }
        
        disabledSwitch.onChange = { [weak self] sender in
            self?.component.disabled = sender.isOn
        }

        labelTextField
            .textPublisher
            .sink { [weak self] text in
                self?.checkbox?.labelText = text
            }.store(in: &subscribers)
        
        childTextField
            .textPublisher
            .sink { [weak self] text in
                self?.checkbox?.childText = text
            }.store(in: &subscribers)
    }
    
    func setupModel() {
        var checkbox1 = CheckboxGroup.CheckboxModel()
        checkbox1.inputId = "model1"
        checkbox1.value = "model 1 Value"
        checkbox1.labelText = "iPhone 11 Bundle 1"
        checkbox1.childText = "Apple iPhone 11 - 64 GB\nOtterbox Case Red\nScreen Protector"
        checkbox1.errorText = "Please Choose 1"

        var checkbox2 = CheckboxGroup.CheckboxModel()
        checkbox2.inputId = "model2"
        checkbox2.value = "model 2 Value"
        checkbox2.labelText = "iPhone 11 Bundle 2"
        checkbox2.childText = "Apple iPhone 11 - 128 GB\nOtterbox Case Black\nScreen Protector"
        checkbox2.errorText = "Please Choose 2"

        component.selectorModels = [checkbox1, checkbox2]
        
        component.onChange =  { [weak self] group in
            let selected = group.selectedHandlers?
                .compactMap{"\($0.labelText!)"}
                .joined(separator: "\r") ?? "none selected"
            
            let alertController:UIAlertController = UIAlertController(title: "Alert",
                                                                      message: "Selected Checkboxes:\r\(selected)",
                                                                      preferredStyle: UIAlertController.Style.alert)
            alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:nil))
            self?.present(alertController, animated: true)
        }

        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        disabledSwitch.isOn = component.disabled
        labelTextField.text = checkbox1.labelText
        childTextField.text = checkbox1.childText
        showErrorSwitch.isOn = component.showError
        
    }

    var checkbox: CheckboxItem? {
        component.selectorViews.first
    }
    
    //Picker
    func setupPicker(){
        surfacePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }
    }

}
