//
//  TSThemeTutorialsVC.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/10.
//


import JXSegmentedView
import JXPagingView
class TSThemeTutorialsVC: TSBaseVC {
    
    var headerInSectionHeight: Int = 44

    let titles:[String] = ["Ringtone".localized,"Poster".localized,"Photo".localized]
    let tutorialsImageNamed:[String] = ["tutorials_ringtone","tutorials_poster","tutorials_photo"]

    lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.isTitleColorGradientEnabled = true
        dataSource.itemSpacing = 16
        dataSource.itemWidthIncrement = 32
        
        dataSource.titles = titles
        dataSource.titleNormalColor = .white.withAlphaComponent(0.6)
        dataSource.titleSelectedColor = .white
        dataSource.titleNormalFont = .font(size: 16.0)
        return dataSource
    }()
    
    //配置指示器
    lazy var indicator: JXSegmentedIndicatorBackgroundView = {
        let indicator = JXSegmentedIndicatorBackgroundView()
        indicator.isIndicatorConvertToItemFrameEnabled = true
        indicator.indicatorHeight = 2
        indicator.indicatorPosition = .bottom
        
        let gradientView = JXSegmentedComponetGradientView()
        gradientView.gradientLayer.endPoint = CGPoint(x: 0, y: 1)
        gradientView.gradientLayer.endPoint = CGPoint(x: 1, y: 1)
        gradientView.gradientLayer.colors = ["#E961F6".uiColor.cgColor, "#7E57F4".uiColor.cgColor]
        gradientView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        gradientView.cornerRadius = 1
        indicator.addSubview(gradientView)
        
        return indicator
    }()
    

    lazy var segmentedView: JXSegmentedView = {
        let segmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: CGFloat(headerInSectionHeight)))
        segmentedView.delegate = self
        segmentedView.indicators = [indicator]
        segmentedView.dataSource = segmentedDataSource
        segmentedView.backgroundColor = .clear
        segmentedView.contentEdgeInsetLeft = 26
        segmentedView.listContainer = pagingView.listContainerView
        
        let lineView = UIView(frame: CGRect(x: 0, y: CGFloat(headerInSectionHeight)-1, width: k_ScreenWidth, height: 1))
        lineView.backgroundColor = .white.withAlphaComponent(0.2)
        segmentedView.addSubview(lineView)

        return segmentedView
    }()
 
    lazy var pagingView: JXPagingView = {
        let pagingView: JXPagingView = JXPagingListRefreshView(delegate: self) //整个刷新
//        let pagingView = JXPagingView(delegate: self)
        pagingView.mainTableView.backgroundColor = .clear
        pagingView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: k_ScreenHeight - k_Nav_Height)
        pagingView.listContainerView.listCellBackgroundColor = .clear
        //扣边返回处理，下面的代码要加上
        pagingView.listContainerView.scrollView.panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        pagingView.mainTableView.panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        return pagingView
    }()

    
    override func createView() {
        addNormalNavBarView()
        setPageTitle("Tutorial".localized)
        edgesForExtendedLayout = []
        contentView.addSubview(segmentedView)
        contentView.addSubview(pagingView)
        
    }
    
    
    @objc func clickBanner() {
    }
    
    
    override func dealThings() {

    }
    
}



extension TSThemeTutorialsVC: JXPagingViewDelegate {

    func tableHeaderViewHeight(in pagingView: JXPagingView) -> Int {
        return 0
    }

    func tableHeaderView(in pagingView: JXPagingView) -> UIView {
        return UIView()
    }

    func heightForPinSectionHeader(in pagingView: JXPagingView) -> Int {
        return headerInSectionHeight
    }

    func viewForPinSectionHeader(in pagingView: JXPagingView) -> UIView {
        return segmentedView
    }

    func numberOfLists(in pagingView: JXPagingView) -> Int {
        return titles.count
    }

    func pagingView(_ pagingView: JXPagingView, initListAtIndex index: Int) -> JXPagingViewListViewDelegate {
        return TSThemeTutorialsSetVC(pageTitle: "", tutorialNamed: tutorialsImageNamed[index])
    }
}

extension TSThemeTutorialsVC: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = (index == 0)
    }
}


//MARK: TSThemeTutorialsSetVC
class TSThemeTutorialsSetVC: TSTutorialsVC,JXPagingViewListViewDelegate {
    
    //联动滚动回调
    var listViewDidScrollCallback: ((UIScrollView) -> ())?
    
    override func createView() {
        super.createView()
        setNavBarViewHidden(true)
        normalNavBarView.isHidden = true
        edgesForExtendedLayout = []
        self.view.backgroundColor = .clear
    }
    
    func listScrollView() -> UIScrollView {
        return scrollView
    }

    func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> ()) {
        listViewDidScrollCallback = callback
    }

    func listView() -> UIView { view }
    
    func listWillAppear() {

    }

    func listDidAppear() {
//        print("\(title ?? ""):\(#function)")
    }

    func listWillDisappear() {
//        print("\(title ?? ""):\(#function)")
    }

    func listDidDisappear() {
//        print("\(title ?? ""):\(#function)")
    }
}
