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

import Foundation
import UIKit
import VDS
import VDSColorTokens

class TitleLockupViewController: BaseViewController<TitleLockup> {

    lazy var textPositionPickerSelectorView = {
        PickerSelectorView(title: "left",
                           picker: self.picker,
                           items: TitleLockup.TextPosition.allCases)
    }()
    
    lazy var titleTextStylePickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: TitleLockup.TitleTextStyle.allCases.sorted{ $0.rawValue < $1.rawValue })
    }()
    
    lazy var otherTextStylePickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: TitleLockup.OtherTextStyle.allCases.sorted{ $0.rawValue < $1.rawValue })
    }()
    
    lazy var subtitleColorPickerSelectorView = {
        PickerSelectorView<Use>(title: "",
                                 picker: self.picker,
                                 items: [.primary, .secondary])
    }()

    var eyebrowTextField = TextField()
    var titleTextField = TextField()
    var subTitleTextField = TextField()

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

        setupPicker()
        setupModel()
    }
    
    override func allTextFields() -> [TextField]? { [eyebrowTextField, titleTextField, subTitleTextField] }

    override func setupForm(){
        super.setupForm()  
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Text Position", view: textPositionPickerSelectorView)
        
        addFormRow(label: "Title Style", view: titleTextStylePickerSelectorView)
        addFormRow(label: "Other Style", view: otherTextStylePickerSelectorView)
        
        addFormRow(label: "Eyebrow Text", view: eyebrowTextField)
        addFormRow(label: "Title Text", view: titleTextField)

        addFormRow(label: "Subtitle Color", view: subtitleColorPickerSelectorView)
        addFormRow(label: "Subtitle Text", view: subTitleTextField)

        eyebrowTextField
            .textPublisher
            .sink { [weak self] text in
                self?.setEyebrowModel()
            }.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)
    }
    
    func setupModel() {
        let eyebrowModel = TitleLockup.EyebrowModel(text: "Today only.")
        let titleModel = TitleLockup.TitleModel(text: "Get more of our best")
        let subTitleModel = TitleLockup.SubTitleModel(text: "Get both of our best and pay less. Pair 5G Home Internet with Verizon mobile to save every month.")
        
        component.eyebrowModel = eyebrowModel
        component.titleModel = titleModel
        component.subTitleModel = subTitleModel
        
        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        textPositionPickerSelectorView.text = component.textPosition.rawValue
        otherTextStylePickerSelectorView.text = component.otherTextStyle.rawValue
        titleTextStylePickerSelectorView.text = titleModel.textStyle.rawValue
        subtitleColorPickerSelectorView.text = subTitleModel.textColor.rawValue
        eyebrowTextField.text = eyebrowModel.text
        titleTextField.text = titleModel.text
        subTitleTextField.text = subTitleModel.text
    }

    func setEyebrowModel() {
        if let text = eyebrowTextField.text, !text.isEmpty {
            component.eyebrowModel = TitleLockup.EyebrowModel(text: text)
        } else {
            component.eyebrowModel = nil
        }
    }
    
    func setTitleModel() {
        if let text = titleTextField.text, !text.isEmpty {
            component.titleModel = TitleLockup.TitleModel(text: text, textStyle: titleTextStylePickerSelectorView.selectedItem)
        } else {
            component.titleModel = nil
        }
    }
    
    func setSubTitleModel() {
        if let text = subTitleTextField.text, !text.isEmpty {
            component.subTitleModel = TitleLockup.SubTitleModel(text: text, textColor: subtitleColorPickerSelectorView.selectedItem)
        } else {
            component.subTitleModel = nil
        }
    }
    
    //Picker
    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.textPosition = item
        }
        
        titleTextStylePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.setTitleModel()
        }

        otherTextStylePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.otherTextStyle = item
        }
        
        subtitleColorPickerSelectorView.onPickerDidSelect  = {  [weak self] item in
            self?.setSubTitleModel()
        }
    }
}
