//
//  RadioBoxGroupViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 8/23/22.
//

import Foundation
import UIKit
import VDS
import VDSColorTokens
import Combine

class  RadioBoxGroupViewController: BaseViewController<RadioBoxGroup>{
    
    var disabledSwitch = Toggle()
    var strikeThroughSwitch = Toggle()
    var textField = TextField()
    var subTextField = TextField()
    var subTextRightField = TextField()
    var showErrorSwitch = Toggle()
        
    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: component)

        setupPicker()
        setupModel()
    }
    
    override func allTextFields() -> [TextField]? { [textField, subTextField, subTextRightField] }
    
    override func setupForm() {
        super.setupForm()
        addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch))
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Strikethrough", view: .makeWrapper(for: strikeThroughSwitch))
        addFormRow(label: "Text", view: textField)
        addFormRow(label: "Sub Text", view: subTextField)
        addFormRow(label: "Sub Text Right", view: subTextRightField)

        disabledSwitch.onChange = { [weak self] sender in
            self?.component.disabled = sender.isOn
        }

        strikeThroughSwitch.onChange = { [weak self] sender in
            self?.radioBox?.strikethrough = sender.isOn
        }

        textField
            .textPublisher
            .sink { [weak self] text in
                self?.radioBox?.text = text
            }.store(in: &subscribers)
        
        subTextField
            .textPublisher
            .sink { [weak self] text in
                self?.radioBox?.subText = text
            }.store(in: &subscribers)
        
        subTextRightField
            .textPublisher
            .sink { [weak self] text in
                self?.radioBox?.subTextRight = text
            }.store(in: &subscribers)
    }
    
    func setupModel(){

        var radioBox1 = RadioBoxGroup.RadioBoxModel()
        radioBox1.inputId = "model1"
        radioBox1.selected = true
        radioBox1.value = "model 1 Value"
        radioBox1.text = "iPhone 11 Bundle 1"
        radioBox1.subText = "Apple iPhone 11 - 64 GB\nOtterbox Case Red\nScreen Protector"
        radioBox1.subTextRight = "Right Text"

        var radioBox2 = RadioBoxGroup.RadioBoxModel()
        radioBox2.inputId = "model2"
        radioBox2.value = "model 2 Value"
        radioBox2.text = "iPhone 11 Bundle 2"
        radioBox2.subText = "Apple iPhone 11 - 128 GB\nOtterbox Case Black\nScreen Protector"

        component.selectorModels = [radioBox1, radioBox2]

        component
            .publisher(for: .valueChanged)
            .sink { [weak self] group in
                let alertController:UIAlertController = UIAlertController(title: "Alert",
                                                                          message: "Selected:\r\(group.selectedHandler?.text ?? "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?.text ?? "none")")
            }.store(in: &subscribers)

        //set UI values
        surfacePickerSelectorView.text = component.surface.rawValue
        disabledSwitch.isOn = component.disabled
        textField.text = radioBox1.text
        subTextField.text = radioBox1.subText
        subTextRightField.text = radioBox1.subTextRight
    }

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