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

import Foundation
import UIKit
import VDS
import VDSColorTokens
import Combine

class  RadioButtonViewController: BaseViewController<RadioButtonGroup> {
   
    var disabledSwitch = Toggle()
    var labelTextField = TextField()
    var childTextField = TextField()
    var showErrorSwitch = Toggle()
    
    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?.radioButton?.labelText = text
            }.store(in: &subscribers)
        
        childTextField
            .textPublisher
            .sink { [weak self] text in
                self?.radioButton?.childText = text
            }.store(in: &subscribers)
        
    }

    func setupModel(){
        var radioButton1 = RadioButtonGroup.RadioButtonModel()
        radioButton1.inputId = "model1"
        radioButton1.value = "model 1 Value"
        radioButton1.labelText = "iPhone 11 Bundle 1"
        radioButton1.childText = "Apple iPhone 11 - 64 GB\nOtterbox Case Red\nScreen Protector"
        
        var radioButton2 = RadioButtonGroup.RadioButtonModel()
        radioButton2.inputId = "model2"
        radioButton2.value = "model 2 Value"
        radioButton2.labelText = "iPhone 11 Bundle 2"
        radioButton2.childText = "Apple iPhone 11 - 128 GB\nOtterbox Case Black\nScreen Protector"
        
        var radioButton3 = RadioButtonGroup.RadioButtonModel()
        radioButton3.inputId = "model3"
        radioButton3.value = "model 3 Value"
        radioButton3.labelText = "iPhone 11 Bundle 3"
        radioButton3.childText = "Apple iPhone 11 - 256 GB\nOtterbox Case Black\nScreen Protector"
        
        component.selectorModels = [radioButton1, radioButton2, radioButton3]
        
        component.onChange = { [weak self] group in
            let alertController:UIAlertController = UIAlertController(title: "Alert",
                                                                      message: "Selected:\r\(group.selectedHandler?.labelText ?? "none")",
                                                                      preferredStyle: UIAlertController.Style.alert)
            alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:nil))
            self?.present(alertController, animated: true)
            
            print("Selected: \(group.selectedHandler?.labelText ?? "none")")
        }

        //set UI values
        surfacePickerSelectorView.text = component.surface.rawValue
        disabledSwitch.isOn = component.disabled
        showErrorSwitch.isOn = component.showError
        labelTextField.text = radioButton1.labelText
        childTextField.text = radioButton1.childText
    }
    
    var radioButton: RadioButtonItem? {
        component.selectorViews.first
    }

    //Picker
    func setupPicker(){
        
        picker.isHidden = true
        surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }

    }
}
