//
//  TSAIPhotoVC.swift
//  AIRingtone
//
//  Created by 100Years on 2025/2/27.
//


import JXSegmentedView
import JXPagingView
class TSAIPhotoVC: TSBaseVC {
    
    lazy var navBarView: TSBaseNavContentBarView = {
        let navBarView = TSBaseNavContentBarView()

        let titleImageView = UIImageView.createImageView(imageName: "nav_title_aiAvatar",contentMode: .scaleToFill)
        navBarView.barView.addSubview(titleImageView)
        titleImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(375)
            make.height.equalTo(48)
        }

        return navBarView
    }()
    
    var tableHeaderViewHeight: CGFloat{
        get{
            generalPicVC.viewH <= 10 ? 542.0 : generalPicVC.viewH
        }
    }
    var headerInSectionHeight: Int = 44

    let titles:[String] = ["Poster".localized,"Photo".localized]
    let styles:[TSGennerateType] = [.poster,.photo]
    
    lazy var generalPicVC: TSTextGeneralPicVC = {
        let generalPicVC = TSTextGeneralPicVC()
        generalPicVC.selectedIndex = { [weak self] index in
            guard let self = self else { return }
            self.segmentedView.selectItemAt(index: index)
        }
        return generalPicVC
    }()
    
    //头 view
    lazy var headerView: UIView = {
        let view = generalPicVC.view!
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: CGFloat(tableHeaderViewHeight))
//        view.backgroundColor = .yellow
        return view
    }()
    
    lazy var pagingView: JXPagingView = {
        //lazy var pagingView: JXPagingView = JXPagingListRefreshView(delegate: self) //整个刷新
//        let pagingView = JXPagingListRefreshView(delegate: self) //list 刷新
        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)// - CGFloat(headerInSectionHeight) + 10)
        pagingView.listContainerView.listCellBackgroundColor = .clear
        //扣边返回处理，下面的代码要加上
        pagingView.listContainerView.scrollView.panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        pagingView.mainTableView.panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        pagingView.pinSectionHeaderVerticalOffset = Int(TSTGPTitleView.viewH)
        return pagingView
    }()

    
    lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.isTitleColorGradientEnabled = true
        dataSource.itemSpacing = 16
        dataSource.itemWidthIncrement = 32
        
        dataSource.isItemSpacingAverageEnabled = false
        dataSource.titles = titles
        dataSource.titleNormalColor = .assist
        dataSource.titleSelectedColor = .white
        dataSource.titleNormalFont = .font(size: 14.0)
        return dataSource
    }()
    
    //配置指示器
    lazy var indicator: JXSegmentedIndicatorBackgroundView = {
        let indicator = JXSegmentedIndicatorBackgroundView()
        indicator.isIndicatorConvertToItemFrameEnabled = true
        indicator.indicatorHeight = 28
        
        
//        indicator.indicatorColor = .themeColor
        
        
        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 = 14
        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
        return segmentedView
    }()
 
    override func createView() {
        setViewBgImageNamed(named: kViewBJ)
  
//        navBarContentView.addSubview(navBarView)
//        navBarView.snp.makeConstraints { make in
//            make.edges.equalToSuperview()
//        }
        
        addNormalNavBarView()
        setPageTitle("Ai Avatar".localized)
        
        contentView.addSubview(segmentedView)
        contentView.addSubview(pagingView)
        
        kDelayMainShort {
            self.headerView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: CGFloat(self.tableHeaderViewHeight))
            self.pagingView.reloadData()
        }
    }
    
    
    @objc func clickBanner() {
    }
    
    
    override func dealThings() {
        NotificationCenter.default.addObserver(forName: .kReloadUIData, object: nil, queue: nil) { notification in
            if let userInfo = notification.userInfo as? [String: TSGennerateType], let myEnum = userInfo["TSGennerateType"] {
                self.segmentedView.selectItemAt(index: myEnum.rawValue)
            }
        }
    }
    
}



extension TSAIPhotoVC: JXPagingViewDelegate {

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

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

    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 TSAIPhotoChildVC(style: styles.safeObj(At: index) ?? .poster)
    }
}

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

//联动的关键代码
extension JXPagingListContainerView: @retroactive JXSegmentedViewListContainer {}
