//
//  LabelViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 8/16/22.
//

import Foundation
import UIKit
import VDS
import VDSColorTokens

class LabelViewController: BaseViewController<Label> {

    lazy var textSizePickerSelectorView = {
        TextSizePickerSelectorView(title: "",
                                   picker: self.picker)
    }()
    
    lazy var textPositionPickerSelectorView = {
        PickerSelectorView(title: TextPosition.left.rawValue,
                            picker: self.picker,
                           items: TextPosition.allCases)
    }()

    lazy var fontCategoryPickerSelectorView = {
        FontCategoryPickerSelectorView(title: "",
                                       picker: self.picker)
    }()
    
    var boldSwitch = Toggle()
    var disabledSwitch = Toggle()
    var textField = TextField()
    private var isBold: Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: component)

        setupPicker()
        setupModel()
    }
    
    override func allTextFields() -> [TextField]? { [textField] }

    override func setupForm(){
        super.setupForm()  
        addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch))
        addFormRow(label: "Bold", view: .makeWrapper(for: boldSwitch))
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Text Position", view: textPositionPickerSelectorView)
        addFormRow(label: "Font Category", view: fontCategoryPickerSelectorView)
        addFormRow(label: "Text Size", view: textSizePickerSelectorView)
        addFormRow(label: "Text", view: textField)
        
        disabledSwitch.onChange = { [weak self] sender in
            self?.component.disabled = sender.isOn
        }

        boldSwitch
            .publisher(for: .valueChanged)
            .sink { [weak self] sender in
                self?.isBold = sender.isOn
                self?.updateLabelStyle()
            }.store(in: &subscribers)

        textField
            .textPublisher
            .sink { [weak self] text in
                self?.component.text = text
            }.store(in: &subscribers)
    }
        
    func setupModel() {
        component.text = "Label Component"
        component.textStyle = .featureSmall
        
        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        disabledSwitch.isOn = component.disabled
        boldSwitch.isOn = isBold
        textField.text = component.text
        
        //set the font
        fontCategory = .feature
        fontCategoryPickerSelectorView.text = "Feature"
        textSize = .small
        textSizePickerSelectorView.text = "Small"
    }
    
    //Picker
    private var fontCategory: TextStyle.FontCategory = .feature {
        didSet {
            fontCategoryPickerSelectorView.text = fontCategory.rawValue
            textSizePickerSelectorView.items = fontCategory.sizes
            if textSizePickerSelectorView.items.count > 0 {
                textSize = textSizePickerSelectorView.items[0]
            } else {
                textSize = nil
            }
            updateLabelStyle()
        }
    }
    
    private var textSize: TextStyle.FontSize? = .large {
        didSet {
            textSizePickerSelectorView.text = textSize?.rawValue ?? ""
            updateLabelStyle()
        }
        
    }
    
    func updateLabelStyle(){
        if let style = fontCategory.style(for: textSize, isBold: isBold) {
            component.textStyle = style
        }
    }

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

        textPositionPickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.textPosition = item
        }
        
        textSizePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.textSize = item
        }

        fontCategoryPickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.fontCategory = item
        }
    }
}
