//
//  TSLightVC.swift
//  Girly
//
//  Created by 100Years on 2025/1/7.
//
import JXSegmentedView
class TSLightVC : TSBaseVC{

    lazy var vcArray: [JXSegmentedListContainerViewListDelegate] = {
        return [TSLightSolidColorVC(),TSLightGradientVC(),TSLightShapeVC()]
    }()
    
    lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
        let titles = ["Solid Color".localized, "Gradient Color".localized, "Shape".localized]
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.isTitleColorGradientEnabled = true
        dataSource.titles = titles
        dataSource.titleNormalColor = UIColor.fromHex("#FFFFFF", alpha: 0.6)
        dataSource.titleSelectedColor = .white
        return dataSource
    }()
    
    //头 view
    lazy var segmentedView: JXSegmentedView = {
        let segmentedView = JXSegmentedView()
        segmentedView.delegate = self
        segmentedView.dataSource = segmentedDataSource
        
        //配置指示器
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = 26
        indicator.indicatorHeight = 4
        indicator.indicatorColor = .themeColor
        segmentedView.indicators = [indicator]
        segmentedView.dataSource = segmentedDataSource
        return segmentedView
    }()
    
    //显示的 View
    lazy var listContainerView: JXSegmentedListContainerView = {
        let listContainerView = JXSegmentedListContainerView(dataSource: self)
        listContainerView.scrollView.isScrollEnabled = false
        return listContainerView
    }()
    
    
    override func createView() {
        setNavBarViewHidden(true)
        edgesForExtendedLayout = [.top,.left,.right]
        segmentedView.listContainer = listContainerView
        contentView.addSubview(listContainerView)
        contentView.addSubview(segmentedView)
        
        listContainerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        segmentedView.snp.makeConstraints { make in
            make.leading.trailing.equalTo(0)
            make.height.equalTo(44.0)
            make.bottom.equalTo(-128.0)
        }
    }
}

extension TSLightVC: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        if let dotDataSource = segmentedDataSource as? JXSegmentedDotDataSource {
            //先更新数据源的数据
            dotDataSource.dotStates[index] = false
            //再调用reloadItem(at: index)
            segmentedView.reloadItem(at: index)
        }

        navigationController?.interactivePopGestureRecognizer?.isEnabled = (segmentedView.selectedIndex == 0)
    }
}

extension TSLightVC: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        if let titleDataSource = segmentedView.dataSource as? JXSegmentedBaseDataSource {
            return titleDataSource.dataSource.count
        }
        return 0
    }

    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        return vcArray[index]
    }
}


