//
//  TrailingTooltipLabel.swift
//  VDSSample
//
//  Created by Matt Bruce on 4/14/23.
//

import Foundation
import UIKit
import VDS
import VDSColorTokens

class TrailingTooltipLabelViewController: BaseViewController<TrailingTooltipLabel> {

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

    var boldSwitch = Toggle()
    var disabledSwitch = Toggle()
    var textField = TextField()
    
    var titleTextField = TextField()
    var contentTextField = TextField()
    var closeButtonTextField = TextField()

    private var isBold: Bool = false

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

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

    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)
        addFormRow(label: "Tooltip Title", view: titleTextField)
        addFormRow(label: "Tooltip Content", view: contentTextField)
        addFormRow(label: "Tooltip Close Button Text", view: closeButtonTextField)

        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.labelText = text
            }.store(in: &subscribers)
        
        titleTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.tooltipTitle = text
            }.store(in: &subscribers)
        
        contentTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.tooltipContent = text
            }.store(in: &subscribers)

        closeButtonTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.tooltipCloseButtonText = text
            }.store(in: &subscribers)

    }
    
    func setupModel() {
        component.labelText = "5G Ultra Wideband is available in your area"
        component.tooltipTitle = "5G Ultra Wideband is available in your area."
        component.tooltipContent = "$799.99 (128 GB only) device payment purchase w/new or upgrade smartphone line on postpaid 5G Unlimited plans only req'd. Less up to $800 trade-in/promo credit applied over 36 mos.; promo credit ends if eligibility req’s are no longer met; 0% APR. Trade-in conditions apply.$799.99 (128 GB only) device payment purchase w/new or upgrade smartphone line on postpaid 5G Unlimited plans only req'd. Less up to $800 trade-in."

        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        disabledSwitch.isOn = component.disabled
        boldSwitch.isOn = isBold
        textField.text = component.labelText
        
        //set the font
        fontCategory = .body
        textSize = .small
        
        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        disabledSwitch.isOn = component.disabled
        titleTextField.text = component.tooltipTitle
        contentTextField.text = component.tooltipContent
        closeButtonTextField.text = component.tooltipCloseButtonText

    }
    
    //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.labelTextStyle = 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.labelTextPosition = item
        }

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