//
//  TabsViewController.swift
//  VDSSample
//
//  Created by Matt Bruce on 5/18/23.
//

import Foundation
import UIKit
import VDS
import Combine
import VDSColorTokens

class TabsViewController: BaseViewController<Tabs> {
    
    var disabledSwitch = Toggle()
    var borderlineSwitch = Toggle()
    var fillContainerSwitch = Toggle()
    var sampleSwitch = Toggle()

    var horizontalOrientationFormStackView = FormSection()
    
    lazy var orientationPickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: Tabs.Orientation.allCases)
    }()
    
    lazy var indicatorPositionPickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: Tabs.IndicatorPosition.allCases)
    }()
    
    lazy var sizePickerSelectorView = {
        PickerSelectorView(title: "",
                           picker: self.picker,
                           items: Tabs.Size.allCases)
    }()

//    var overflowRow: UIView?
//    lazy var overflowPickerSelectorView = {
//        PickerSelectorView(title: "",
//                           picker: self.picker,
//                           items: Tabs.Overflow.allCases)
//    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        addContentTopView(view: component)
        setupPicker()
        setupModel()
    }
    
    override func setupForm(){
        super.setupForm()
        addFormRow(label: "Large Sample", view: .makeWrapper(for: sampleSwitch))
        addFormRow(label: "Show Borderline", view: .makeWrapper(for: borderlineSwitch))
        addFormRow(label: "Surface", view: surfacePickerSelectorView)
        addFormRow(label: "Size", view: sizePickerSelectorView)
        
        if UIDevice.isIPad {
            addFormRow(label: "Orientation", view: orientationPickerSelectorView)
        }

        //only in horizontal mode
        horizontalOrientationFormStackView.addFormRow(label: "Fill Container", view: .makeWrapper(for: fillContainerSwitch))
        horizontalOrientationFormStackView.addFormRow(label: "Indicator Position", view: indicatorPositionPickerSelectorView)
        //overflowRow = horizontalOrientationFormStackView.addFormRow(label: "Overflow", view: overflowPickerSelectorView)

        append(section: horizontalOrientationFormStackView)
        
        disabledSwitch.onChange =  { [weak self] sender in
            self?.component.disabled = sender.isOn
        }
        
        borderlineSwitch.onChange =  { [weak self] sender in
            self?.component.borderLine = sender.isOn
        }
        
        fillContainerSwitch.onChange =  { [weak self] sender in
            self?.component.fillContainer = sender.isOn
            //self?.overflowRow?.isHidden = sender.isOn
        }

        sampleSwitch.onChange =  { [weak self] sender in
            guard let self else { return }
            self.component.selectedIndex = 0
            self.component.tabModels = sender.isOn ? self.allTabs : self.some
        }
    }
    
    var allTabs: [Tabs.TabModel] = [
        .init(text: "Accessories"),
        .init(text: "Internet and TV"),
        .init(text: "Customer Service"),
        .init(text: "Contact Us")
    ]
    
    var some: [Tabs.TabModel] = [
        .init(text: "Accessories"),
        .init(text: "Internet and TV")
    ]
    
    func setupModel() {
        //set to the large sample
        component.tabModels = allTabs      

        //setup UI
        surfacePickerSelectorView.text = component.surface.rawValue
        sizePickerSelectorView.text = component.size.rawValue
        orientationPickerSelectorView.text = component.orientation.rawValue
        indicatorPositionPickerSelectorView.text = component.indicatorPosition.rawValue
        //overflowPickerSelectorView.text = component.overflow.rawValue
        disabledSwitch.isOn = component.disabled
        borderlineSwitch.isOn = component.borderLine
        fillContainerSwitch.isOn = component.fillContainer
        sampleSwitch.isOn = true
    }
    
    func setupPicker(){
        surfacePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.surface = item
            self?.contentTopView.backgroundColor = item.color
        }
        
        sizePickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.size = item
        }
        
        orientationPickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.orientation = item
            self?.horizontalOrientationFormStackView.isHidden = item == .vertical
        }
        
        indicatorPositionPickerSelectorView.onPickerDidSelect = {  [weak self] item in
            self?.component.indicatorPosition = item
        }

//        overflowPickerSelectorView.onPickerDidSelect = {  [weak self] item in
//            self?.component.overflow = item
//        }
    }
}
