//
//  RadioSwatchGroupViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 8/30/22.
//

import Foundation
import UIKit
import VDS
import VDSColorTokens
import Combine

class  RadioSwatchGroupViewController: BaseViewController<RadioSwatchGroup> {
    
    var disabledSwitch = Toggle()
    var strikeThroughSwitch = Toggle()
        
    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: component)

        setupPicker()
        setupModel()
    }
    
    override func setupForm() {
        super.setupForm()
        addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch))
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Strikethrough", view: .makeWrapper(for: strikeThroughSwitch))
        
        disabledSwitch.onChange = { [weak self] sender in
            self?.component.disabled = sender.isOn
        }
        
        strikeThroughSwitch.onChange = { [weak self] sender in
            self?.component.selectorViews.first?.strikethrough = sender.isOn
            self?.component.reload()
        }
    }
    
    func setupModel(){
        
        var radioSwatch1 = RadioSwatchGroup.RadioSwatchModel()
        radioSwatch1.fillImage = UIImage(named: "imageSwatch")
        radioSwatch1.text = "Image"
        radioSwatch1.inputId = "radioSwatch1"
                
        var radioSwatch2 = RadioSwatchGroup.RadioSwatchModel()
        radioSwatch2.primaryColor = .red
        radioSwatch2.secondaryColor = .blue
        radioSwatch2.text = "Red/Blue"
        radioSwatch2.inputId = "radioSwatch2"
        
        var radioSwatch3 = RadioSwatchGroup.RadioSwatchModel()
        radioSwatch3.primaryColor = .green
        radioSwatch3.text = "Green"
        radioSwatch3.inputId = "radioSwatch3"

        var radioSwatch4 = RadioSwatchGroup.RadioSwatchModel()
        radioSwatch4.primaryColor = .orange
        radioSwatch4.text = "Orange"
        radioSwatch4.inputId = "radioSwatch4"

        var radioSwatch5 = RadioSwatchGroup.RadioSwatchModel()
        radioSwatch5.primaryColor = .brown
        radioSwatch5.text = "Brown"
        radioSwatch5.inputId = "radioSwatch5"
        
        var radioSwatch6 = RadioSwatchGroup.RadioSwatchModel()
        radioSwatch6.primaryColor = .yellow
        radioSwatch6.text = "Yellow"
        radioSwatch6.inputId = "radioSwatch6"

        var radioSwatch7 = RadioSwatchGroup.RadioSwatchModel()
        radioSwatch7.primaryColor = .purple
        radioSwatch7.text = "Puple"
        radioSwatch7.inputId = "radioSwatch7"

        var radioSwatch8 = RadioSwatchGroup.RadioSwatchModel()
        radioSwatch8.primaryColor = .systemPink
        radioSwatch8.text = "Pink"
        radioSwatch8.inputId = "radioSwatch8"
        
        component.selectorModels = [radioSwatch1, radioSwatch2, radioSwatch3, radioSwatch4, radioSwatch5, radioSwatch6, radioSwatch7, radioSwatch8]
        
        component.onChange = { group in
            print("Selected: \(group.selectedHandler?.text ?? "none")")
        }
        
        //set UI values
        surfacePickerSelectorView.text = component.surface.rawValue
        disabledSwitch.isOn = component.disabled
    }
    
    //Picker
    func setupPicker(){
        
        surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }
        
    }
}
