//
//  TiletViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 12/19/22.
//

import Foundation
import UIKit
import VDS
import VDSColorTokens
import Combine

class TileletViewController: BaseViewController<Tilelet> {
    
    lazy var titleTextStylePickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: Tilelet.TitleModel.TextStyle.allCases.sorted{ $0.rawValue < $1.rawValue })
    }()
    
    lazy var otherTextStylePickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: Tilelet.SubTitleModel.TextStyle.allCases.sorted{ $0.rawValue < $1.rawValue })
    }()
    
    lazy var subtitleColorPickerSelectorView = {
        PickerSelectorView<Use>(title: "",
                                picker: self.picker,
                                items: [.primary, .secondary])
    }()
    
    var clickableSwitch = Toggle()
    
    var titleTextField = TextField()
    var subTitleTextField = TextField()
    var widthTextField = NumericField()
    var heightTextField = NumericField()
    var textPercentageTextField = NumericField()
    var textWidthTextField = NumericField()
    
    var showDescriptionIconSwitch = Toggle()
    var showDirectionalIconSwitch = Toggle()
    var badgeTextField = TextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: .makeWrapper(for: component))
        
        setupPicker()
        setupModel()
    }
    
    override func allTextFields() -> [TextField]? { [titleTextField, subTitleTextField, widthTextField, heightTextField, textWidthTextField, textPercentageTextField, badgeTextField] }
    
    override func setupForm(){
        super.setupForm()  
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Clickable", view: .makeWrapper(for: clickableSwitch))
        
        addFormRow(label: "Title Style", view: titleTextStylePickerSelectorView)
        addFormRow(label: "Other Style", view: otherTextStylePickerSelectorView)
        
        addFormRow(label: "Title Text", view: titleTextField)
        addFormRow(label: "Subtitle Color", view: subtitleColorPickerSelectorView)
        addFormRow(label: "Subtitle Text", view: subTitleTextField)
        addFormRow(label: "Width", view: widthTextField)
        //addFormRow(label: "Height", view: heightTextField)
        addFormRow(label: "Text Width", view: textWidthTextField)
        addFormRow(label: "Text Percentage", view: textPercentageTextField)
        addFormRow(label: "Badge Text", view: badgeTextField)
        addFormRow(label: "Description Icon", view: .makeWrapper(for: showDescriptionIconSwitch))
        addFormRow(label: "Directional Icon", view: .makeWrapper(for: showDirectionalIconSwitch))
        
        clickableSwitch.onChange = { [weak self] sender in
            guard let self else { return }
            if sender.isOn {
                self.component.onClick = { t in
                    print("you click on me!")
                }
            } else {
                self.component.onClick = nil
            }
        }
        
        widthTextField
            .textPublisher
            .sink { [weak self] text in
                if let n = NumberFormatter().number(from: text) {
                    self?.component.width = CGFloat(truncating: n)
                }
            }.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)
        
        textWidthTextField
            .textPublisher
            .sink { [weak self] text in
                guard let self else { return }
                if let n = NumberFormatter().number(from: text), n.floatValue > 50.0 {
                    self.component.textWidth = CGFloat(truncating: n)
                    self.textPercentageTextField.text = ""
                } else {
                    self.component.textWidth = nil
                }
            }.store(in: &subscribers)
        
        textPercentageTextField
            .textPublisher
            .sink { [weak self] text in
                if let n = NumberFormatter().number(from: text), n.intValue > 15 && n.intValue <= 100 {
                    self?.component.textPercentage = CGFloat(truncating: n)
                    self?.textWidthTextField.text = ""
                } else {
                    self?.component.textPercentage = nil
                }
            }.store(in: &subscribers)
        
        titleTextField
            .textPublisher
            .sink { [weak self] text in
                self?.setTitleModel()
            }.store(in: &subscribers)
        
        subTitleTextField
            .textPublisher
            .sink { [weak self] text in
                self?.setSubTitleModel()
            }.store(in: &subscribers)
        
        badgeTextField
            .textPublisher
            .sink { [weak self] text in
                self?.setBadgeModel()
            }.store(in: &subscribers)
        
        showDescriptionIconSwitch.onChange = { [weak self] sender in
            if sender.isOn {
                self?.showDirectionalIconSwitch.isOn = false
                self?.component.descriptiveIconModel = .init(size: .medium, surface: .dark)
            } else {
                self?.component.descriptiveIconModel = nil
            }
        }
        
        showDirectionalIconSwitch.onChange = { [weak self] sender in
            if sender.isOn {
                self?.showDescriptionIconSwitch.isOn = false
                self?.component.directionalIconModel = .init(size: .medium, surface: .dark)
            } else {
                self?.component.directionalIconModel = nil
            }
        }
        
    }
    
    func setupModel() {
        let titleModel = Tilelet.TitleModel(text: "Save $XX on your monthly bill.")
        let subTitleModel = Tilelet.SubTitleModel(text: "Enroll in Auto Pay & paper-free billing to save on your monthly bill.")
        
        component.surface = .light
        component.titleModel = titleModel
        component.subTitleModel = subTitleModel
        
        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        otherTextStylePickerSelectorView.text = subTitleModel.textStyle.rawValue
        titleTextStylePickerSelectorView.text = titleModel.textStyle.rawValue
        subtitleColorPickerSelectorView.text = subTitleModel.textColor.rawValue
        titleTextField.text = titleModel.text
        subTitleTextField.text = subTitleModel.text
        widthTextField.text = component.width != nil ? "\(component.width!)" : ""
    }
    
    //sub models
    func setBadgeModel() {
        if let text = badgeTextField.text, !text.isEmpty {
            component.badgeModel = Tilelet.BadgeModel(text: text)
        } else {
            component.badgeModel = nil
        }
    }
    
    func setTitleModel() {
        if let text = titleTextField.text, !text.isEmpty {
            component.titleModel = Tilelet.TitleModel(text: text, textStyle: titleTextStylePickerSelectorView.selectedItem)
        } else {
            component.titleModel = nil
        }
    }
    
    func setSubTitleModel() {
        if let text = subTitleTextField.text, !text.isEmpty {
            component.subTitleModel = Tilelet.SubTitleModel(text: text, textStyle: otherTextStylePickerSelectorView.selectedItem)
        } else {
            component.subTitleModel = nil
        }
    }
    
    //Picker
    func setupPicker(){
        surfacePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }
        
        titleTextStylePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.setTitleModel()
        }
        
        otherTextStylePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.setSubTitleModel()
        }
        
        subtitleColorPickerSelectorView.onPickerDidSelect  = {  [weak self] item in
            self?.setSubTitleModel()
        }
    }
}
