//
//  BadgeViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 9/22/22.
//

import Foundation
import UIKit
import VDS
import VDSColorTokens
import Combine

class  BadgeViewController: BaseViewController<Badge> {
            
    enum NumberOfLines: String, CaseIterable {
        case unlimited
        case one
        case two
        case three
        case four
        
        var intValue: Int {
            switch self {
                
            case .unlimited:
                return 0
            case .one:
                return 1
            case .two:
                return 2
            case .three:
                return 3
            case .four:
                return 4
            }
        }
    }
    
    lazy var fillColorPickerSelectorView = {
        PickerSelectorView(title: "red",
                            picker: self.picker,
                           items: Badge.FillColor.allCases)
    }()
    
    lazy var numberOfLinesPickerSelectorView = {
        PickerSelectorView(title: "one",
                            picker: self.picker,
                            items: NumberOfLines.allCases)
    }()
    
    var textField = TextField()
    var maxWidthTextField = NumericField()
    
    override func allTextFields() -> [TextField]? { [textField, maxWidthTextField] }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: .makeWrapper(for: component))
        setupPicker()
        setupModel()
    }
    
    override func setupForm(){
        super.setupForm()  
        addFormRow(label: "Fill Color", view: fillColorPickerSelectorView)
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Text", view: textField)
        addFormRow(label: "Max Width", view: maxWidthTextField)
        addFormRow(label: "Number of Lines", view: numberOfLinesPickerSelectorView)
                
        textField
            .textPublisher
            .sink { [weak self] text in
                self?.component.text = text
            }.store(in: &subscribers)

        maxWidthTextField
            .textPublisher
            .sink { [weak self] text in
                var float: CGFloat?
                if let n = NumberFormatter().number(from: text) {
                    float = CGFloat(truncating: n)
                }
                self?.component.maxWidth = float
            }.store(in: &subscribers)
    }
    
    func setupModel() {
        component.fillColor = .red
        component.text = "Terms and conditions"
        component.numberOfLines = 1
        
        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        textField.text = component.text
    }
    
    func setupPicker(){

        surfacePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }        
        
        fillColorPickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.fillColor = item
        }
        
        numberOfLinesPickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.numberOfLines = item.intValue
        }
    }
}
