//
//  ButtonIconViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 5/12/23.
//

import Foundation
import UIKit
import VDS
import Combine
import VDSColorTokens

class ButtonIconViewController: BaseViewController<ButtonIcon> {
    
    lazy var kindPickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: ButtonIcon.Kind.allCases)
    }()

    lazy var surfaceTypePickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: ButtonIcon.SurfaceType.allCases)
    }()

    lazy var colorPickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: UIColor.VDSColor.allCases)
    }()
    
    lazy var namePickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: Icon.Name.all.sorted{ $0.rawValue < $1.rawValue })
    }()
    
    lazy var sizePickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: ButtonIcon.Size.allCases)
    }()
    
    var customSizeField = TextField()
    var centerX = TextField()
    var centerY = TextField()
    var fitToIcon = Toggle()
    var floating = Toggle()
    var hideBorder = Toggle()
    var disabledSwitch = Toggle()

    override func allTextFields() -> [TextField]? { [customSizeField, centerX, centerY] }

    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: .makeWrapper(for: component, edgeSpacing: 16.0), edgeSpacing: 0.0)
        setupPicker()
        setupModel()
    }
    
    override func setupForm(){
        super.setupForm()
        addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch))
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Surface Type", view: surfaceTypePickerSelectorView)
        addFormRow(label: "Size", view: sizePickerSelectorView)
        addFormRow(label: "Kind", view: kindPickerSelectorView)
        addFormRow(label: "Floating", view: .makeWrapper(for: floating))
        addFormRow(label: "Hide Border", view: .makeWrapper(for: hideBorder))
        addFormRow(label: "Fit To Icon", view: .makeWrapper(for: fitToIcon))
        addFormRow(label: "Custom Size", view: customSizeField)
        addFormRow(label: "Name", view: namePickerSelectorView)
        addFormRow(label: "X Offset", view: centerX)
        addFormRow(label: "Y Offset", view: centerY)
        
        disabledSwitch.onChange =  { [weak self] sender in
            self?.component.disabled = sender.isOn
        }

        component.onClick = { sender in
            print("Button Icon was clicked")
        }
        
        floating.onChange =  { [weak self] sender in
            self?.component.floating = sender.isOn
        }

        hideBorder.onChange =  { [weak self] sender in
            self?.component.hideBorder = sender.isOn
        }

        fitToIcon.onChange =  { [weak self] sender in
            self?.component.fitToIcon = sender.isOn
        }

        customSizeField.textPublisher.sink { [weak self] value in
            if let intValue = Int(value) {
                self?.component.customSize = intValue
            } else if value.isEmpty {
                self?.component.customSize = nil
            }
        }.store(in: &subscribers)
        
        centerX.textPublisher.sink { [weak self] value in
            self?.updateOffset()
        }.store(in: &subscribers)

        centerY.textPublisher.sink { [weak self] value in
            self?.updateOffset()
        }.store(in: &subscribers)
    }
    
    func setupModel() {
        let name = Icon.Name.addToFavorite
        
        component.iconName = name
        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        surfaceTypePickerSelectorView.text = component.surfaceType.rawValue
        kindPickerSelectorView.text = component.kind.rawValue
        sizePickerSelectorView.text = component.size.rawValue
        namePickerSelectorView.text = name.rawValue
        disabledSwitch.isOn = component.disabled
    }
    
    func updateOffset() {
        if let x = centerX.floatValue, let y = centerY.floatValue {
            component.iconOffset = .init(x: x, y: y)
        } else if let x = centerX.floatValue {
            component.iconOffset = .init(x: x, y: 0)
        } else if let y = centerY.floatValue {
            component.iconOffset = .init(x: 0, y: y)
        } else {
            component.iconOffset = .init(x: 0, y: 0)
        }
    }
    
    
    func setupPicker(){

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

        surfaceTypePickerSelectorView.onPickerDidSelect = { [weak self] item in
            self?.component.surfaceType = item
        }
        
        
        kindPickerSelectorView.onPickerDidSelect = { [weak self] item in
            self?.component.kind = item
        }
        
        sizePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.size = item
        }
        
        namePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.iconName = item
        }
    }
}

extension UITextField {
    public var floatValue: CGFloat? {
        guard let text, let double = Double(text) else { return nil }
        return CGFloat(double)
    }
}
