//
//  BadgeIndicatorViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 6/15/23.
//

import Foundation
import UIKit
import VDS
import VDSColorTokens
import Combine

class  BadgeIndicatorViewController: BaseViewController<BadgeIndicator> {
            
    lazy var fillColorPickerSelectorView = {
        PickerSelectorView(title: "red",
                            picker: self.picker,
                           items: BadgeIndicator.FillColor.allCases)
    }()

    lazy var kindPickerSelectorView = {
        PickerSelectorView(title: "red",
                            picker: self.picker,
                           items: BadgeIndicator.Kind.allCases)
    }()

    lazy var textSizePickerSelectorView = {
        PickerSelectorView(title: "",
                            picker: self.picker,
                           items: BadgeIndicator.Size.allCases)
    }()
    
    lazy var maxDigitsPickerSelectorView = {
        PickerSelectorView(title: "",
                            picker: self.picker,
                           items: BadgeIndicator.MaxDigits.allCases)
    }()
    
    lazy var borderColorLightPickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: UIColor.VDSColor.allCases)
    }()

    lazy var borderColorDarkPickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: UIColor.VDSColor.allCases)
    }()

    
    var textField = TextField()
    var hideDotSwitch = Toggle()
    var hideBorderSwitch = Toggle()

    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: .makeWrapper(for: component))
        setupPicker()
        setupModel()
    }
    
    var heightTextField = NumericField()
    var widthTextField = NumericField()
    var horizontalPaddingTextField = NumericField()
    var verticalPaddingTextField = NumericField()
    var dotSizeTextField = NumericField()

    override func allTextFields() -> [TextField]? { [textField, heightTextField, widthTextField, horizontalPaddingTextField, verticalPaddingTextField, dotSizeTextField] }

    
    var numberedForm = FormSection().with { $0.title = "Numbered Kind"}
    var dotForm = FormSection().with { $0.title = "Simple Kind"}
    
    override func setupForm(){
        super.setupForm()
        addFormRow(label: "Kind", view: kindPickerSelectorView)
        addFormRow(label: "Height", view: heightTextField)
        addFormRow(label: "Width", view: widthTextField)
        addFormRow(label: "Horiztonal Padding", view: horizontalPaddingTextField)
        addFormRow(label: "Vertical Padding", view: verticalPaddingTextField)
        addFormRow(label: "Fill Color", view: fillColorPickerSelectorView)
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Hide Border", view: .makeWrapper(for: hideBorderSwitch))
        addFormRow(label: "Border Light", view: borderColorLightPickerSelectorView)
        addFormRow(label: "Border Dark", view: borderColorDarkPickerSelectorView)
        addFormRow(label: "Size", view: textSizePickerSelectorView)

        dotForm.addFormRow(label: "Hide Dot", view: .makeWrapper(for: hideDotSwitch))
        dotForm.addFormRow(label: "Dot Size", view: dotSizeTextField)
        numberedForm.addFormRow(label: "Text", view: textField)
        numberedForm.addFormRow(label: "Max Digits", view: maxDigitsPickerSelectorView)
        append(section: numberedForm)
        append(section: dotForm)
        
        hideDotSwitch.publisher(for: .valueChanged).sink { [weak self] control in
            self?.component.hideDot = control.isOn
        }.store(in: &subscribers)

        hideBorderSwitch.publisher(for: .valueChanged).sink { [weak self] control in
            self?.component.hideBorder = control.isOn
        }.store(in: &subscribers)

        textField
            .textPublisher
            .sink { [weak self] text in
                if let n = NumberFormatter().number(from: text) {
                    self?.component.number = n.intValue
                }
            }.store(in: &subscribers)
        
        heightTextField
            .textPublisher
            .sink { [weak self] text in
                if let n = NumberFormatter().number(from: text) {
                    self?.component.height = CGFloat(truncating: n)
                } else {
                    self?.component.height = nil
                }
            }.store(in: &subscribers)

        widthTextField
            .textPublisher
            .sink { [weak self] text in
                if let n = NumberFormatter().number(from: text) {
                    self?.component.width = CGFloat(truncating: n)
                } else {
                    self?.component.width = nil
                }
            }.store(in: &subscribers)

        horizontalPaddingTextField
            .textPublisher
            .sink { [weak self] text in
                if let n = NumberFormatter().number(from: text) {
                    self?.component.horitonalPadding = CGFloat(truncating: n)
                } else {
                    self?.component.horitonalPadding = nil
                }
            }.store(in: &subscribers)

        verticalPaddingTextField
            .textPublisher
            .sink { [weak self] text in
                if let n = NumberFormatter().number(from: text) {
                    self?.component.verticalPadding = CGFloat(truncating: n)
                } else {
                    self?.component.verticalPadding = nil
                }
            }.store(in: &subscribers)

        dotSizeTextField
            .textPublisher
            .sink { [weak self] text in
                if let n = NumberFormatter().number(from: text) {
                    self?.component.dotSize = CGFloat(truncating: n)
                } else {
                    self?.component.dotSize = nil
                }
            }.store(in: &subscribers)

    }
    
    func setupModel() {
        component.fillColor = .red
        component.number = 23
        component.kind = .simple
        component.size = .medium
        
        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        kindPickerSelectorView.text = component.kind.rawValue
        fillColorPickerSelectorView.text = component.fillColor.rawValue
        textSizePickerSelectorView.text = component.size.rawValue
        maxDigitsPickerSelectorView.text = component.maxDigits.rawValue
        borderColorDarkPickerSelectorView.text = component.borderColorDark?.toVDSColor()?.rawValue ?? ""
        borderColorLightPickerSelectorView.text = component.borderColorLight?.toVDSColor()?.rawValue ?? ""
        textField.text = "\(component.number!)"
        visibleForms()
    }
    
    func visibleForms() {
        numberedForm.isHidden = kindPickerSelectorView.selectedItem != .numbered
        dotForm.isHidden = kindPickerSelectorView.selectedItem == .numbered
    }
    
    func setupPicker(){

        surfacePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }
       
        kindPickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.kind = item
            self?.visibleForms()
        }
        
        fillColorPickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.fillColor = item
        }
        
        textSizePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.size = item
        }
        
        maxDigitsPickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.maxDigits = item
        }
        
        borderColorDarkPickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.borderColorDark = item.uiColor
        }
        
        borderColorLightPickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.borderColorLight = item.uiColor
        }
    }
}
