//
//  NotificationViewController.swift
//  VDSSample
//
//  Created by Nadigadda, Sumanth on 14/03/23.
//

import Foundation
import VDS

class NotificationViewController: BaseViewController<VDS.Notification> {
    
    let label = Label()
    let titleTextField = TextField()
    let subTitleTextField = TextField()
    let buttonGroupToggle = Toggle()
    let firstButtonTextField = TextField()
    let secondButtonTextField = TextField()
    let hideCloseButtonToggle = Toggle()
    let fullBleedToggle = Toggle()
    
    let titleDefaultText =  "This is title"
    let subtitleDefaultText = "This is subtitle"
    let firstButtonDefaultText = "Button 1"
    let secondButtonDefaultText = "Button 2"
    
    lazy var notificationTypePickerSelectorView = {
        PickerSelectorView(title: "info",
                            picker: self.picker,
                           items: Notification.Style.allCases)
    }()
    
    lazy var layoutTypePickerSelectorView = {
        PickerSelectorView(title: "vertical", picker: self.picker, items: Notification.Layout.allCases)
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: component)
        
        component.title = titleDefaultText
        component.subTitle = subtitleDefaultText
        titleTextField.text = titleDefaultText
        subTitleTextField.text = subtitleDefaultText
        
        setupButtons(with: firstButtonDefaultText, secondButtonText: secondButtonDefaultText)
        firstButtonTextField.text = firstButtonDefaultText
        secondButtonTextField.text = secondButtonDefaultText
        
        setupPicker()
    }
    
    override func setupForm() {
        super.setupForm()
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Type", view: notificationTypePickerSelectorView)
        addFormRow(label: "Layout", view: layoutTypePickerSelectorView)
        addFormRow(label: "Title", view: titleTextField)
        addFormRow(label: "SubTitle", view: subTitleTextField)
        addFormRow(label: "Hide Button Group", view: buttonGroupToggle)
        addFormRow(label: "Hide Close Button", view: hideCloseButtonToggle)
        addFormRow(label: "Full bleed", view: fullBleedToggle)
        addFormRow(label: "Button Action", view: label)
        addFormRow(label: "First Button Text", view: firstButtonTextField)
        addFormRow(label: "Second Button Text", view: secondButtonTextField)
        
        component.onCloseClick = { notification in
            print("onCloseClick: \(notification.title)")
        }
        
        titleTextField.textPublisher.sink { newString in
            self.component.title = newString
        }.store(in: &subscribers)
        
        subTitleTextField.textPublisher.sink { newString in
            self.component.subTitle = newString
        }.store(in: &subscribers)
        
        buttonGroupToggle.onChange = { [weak self] toggle in
            if toggle.isOn {
                self?.component.primaryButtonModel = nil
                self?.component.secondaryButtonModel = nil
                self?.label.text = ""
            } else {
                self?.setupButtons(with: self?.firstButtonDefaultText, secondButtonText: self?.secondButtonDefaultText)
            }
        }
        
        firstButtonTextField.textPublisher.sink { newString in
            
            if newString.isEmpty {
                self.component.primaryButtonModel = nil
                self.component.secondaryButtonModel = nil
            } else {
                self.setupButtons(with: newString, secondButtonText: self.secondButtonTextField.text)
            }
            
        }.store(in: &subscribers)
        
        secondButtonTextField.textPublisher.sink { newString in
            if !(self.firstButtonTextField.text?.isEmpty ?? true){
                self.setupButtons(secondButtonText: newString)
            }
        }.store(in: &subscribers)
        
        hideCloseButtonToggle.onChange = { [weak self] toggle in
            self?.component.hideCloseButton = toggle.isOn
        }
        
        fullBleedToggle.onChange = { [weak self] toggle in
            self?.component.fullBleed = toggle.isOn
        }
    }
    
    func setupPicker() {
        surfacePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }
        
        notificationTypePickerSelectorView.onPickerDidSelect = { [weak self] item in
            self?.component.type = item
        }
        
        layoutTypePickerSelectorView.onPickerDidSelect = { [weak self] item in
            guard let self else { return }
            self.component.layout = item
            if self.component.layout != item {
                self.layoutTypePickerSelectorView.set(item: self.component.layout)
            }
        }
    }
    
    func setupButtons(with firstButtonText: String? = nil, secondButtonText: String? = nil) {
        if let firstButtonText {
            component.primaryButtonModel = .init(text: firstButtonText, onClick: { [weak self] button in
                self?.label.text = "\(button.text!) button click"
            })
        }
        
        if let secondButtonText {
            component.secondaryButtonModel = .init(text: secondButtonText, onClick: { [weak self] button in
                self?.label.text = "\(button.text!) button click"
            })
        }
    }
}
