//
//  TooltipViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 4/13/23.
//

import Foundation
import UIKit
import VDS
import VDSColorTokens
import Combine

class  TooltipViewController: BaseViewController<Tooltip> {

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

    lazy var sizePickerSelectorView = {
        PickerSelectorView(title: Tooltip.Size.medium.rawValue,
                           picker: self.picker,
                           items: Tooltip.Size.allCases)
    }()

    lazy var fillColorPickerSelectorView = {
        PickerSelectorView(title: Tooltip.FillColor.primary.rawValue,
                            picker: self.picker,
                           items: Tooltip.FillColor.allCases)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: .makeWrapper(for: component, edgeSpacing: 16.0), edgeSpacing: 0.0)
        
        setupPicker()
        setupModel()
    }
    
    override func allTextFields() -> [TextField]? { [titleTextField, contentTextField, closeButtonTextField] }

    override func setupForm(){
        super.setupForm()
        addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch))
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Size", view: sizePickerSelectorView)
        addFormRow(label: "FillColor", view: fillColorPickerSelectorView)
        addFormRow(label: "Title", view: titleTextField)
        addFormRow(label: "Content", view: contentTextField)
        addFormRow(label: "Close Button Text", view: closeButtonTextField)
        
        disabledSwitch.onChange =  { [weak self] sender in
            self?.component.disabled = sender.isOn
        }

        titleTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.title = text
            }.store(in: &subscribers)
        
        contentTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.content = text
            }.store(in: &subscribers)

        closeButtonTextField
            .textPublisher
            .sink { [weak self] text in
                self?.component.closeButtonText = text
            }.store(in: &subscribers)
    }
    
    func setupModel() {
        component.title = "5G Ultra Wideband is available in your area."
        component.content = "$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
        titleTextField.text = component.title
        contentTextField.text = component.content
        closeButtonTextField.text = component.closeButtonText
    }
            
    //Picker
    func setupPicker(){
        surfacePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }
        
        sizePickerSelectorView.onPickerDidSelect = { [weak self] item in
            self?.component.size = item
        }
        
        fillColorPickerSelectorView.onPickerDidSelect = { [weak self] item in
            self?.component.fillColor = item
        }
    }
    
}

class TooltipPopup: UIAlertController {
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        
    }
}
