//
//  TSEmojisVC.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/15.
//

import JXSegmentedView
import JXPagingView
class TSEmojisVC: TSBaseVC {
    
    let viewModel = TSEmojisColViewModel()
    var tableHeaderViewHeight: Int = 150
    var headerInSectionHeight: Int = 50
    
    
    lazy var vipBtn: UIButton = {
        let vipBtn = UIButton.createButton(image: UIImage(named: "nav_vip")) { [weak self]  in
            guard let self = self else { return }
            TSPurchaseVC.show(target: self) {
      
            }
        }
        return vipBtn
    }()
    
    lazy var navBarView: TSBaseNavContentBarView = {
        let navBarView = TSBaseNavContentBarView()
        
        let titleImageView = UIImageView.createImageView(imageName: "nav_title_emojis",contentMode: .scaleToFill)
        navBarView.barView.addSubview(titleImageView)
        titleImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(16)
        }
        
        let setBtn = UIButton.createButton(image: UIImage(named: "setting")) { [weak self]  in
            guard let self = self else { return }
            let setingVC = TSSetingVC()
            setingVC.hidesBottomBarWhenPushed = true
            navigationController?.pushViewController(setingVC, animated: true)
        }
        navBarView.barView.addSubview(setBtn)
        setBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-16)
            make.width.height.equalTo(24)
        }
        
        navBarView.barView.addSubview(vipBtn)
        vipBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-62)
            make.width.height.equalTo(24)
        }

        return navBarView
    }()
    
    //头 view
    lazy var headerView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: CGFloat(tableHeaderViewHeight)))
        let imageView = UIImageView.createImageView(imageName: "emojis_banner")
        imageView.isUserInteractionEnabled = true
        imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickBanner)))
        view.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(343*kDesignScale)
            make.height.equalTo(117*kDesignScale)
        }
        
        let closeBtn = UIButton.createButton{ [weak self]  in
            guard let self = self else { return }
            viewModel.isHiddenBanner = true
            pagingView.reloadData()
        }
        view.addSubview(closeBtn)
        closeBtn.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.trailing.equalTo(-16)
            make.width.height.equalTo(50)
        }
        return view
    }()
    
    lazy var pagingView: JXPagingView = {
        //lazy var pagingView: JXPagingView = JXPagingListRefreshView(delegate: self) //整个刷新
        let pagingView = JXPagingListRefreshView(delegate: self) //list 刷新
        pagingView.mainTableView.backgroundColor = .clear
        pagingView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: k_ScreenHeight - k_Nav_Height  - k_Height_safeAreaInsetsBottom() - CGFloat(headerInSectionHeight) + 10)
        pagingView.listContainerView.listCellBackgroundColor = .clear
        //扣边返回处理，下面的代码要加上
        pagingView.listContainerView.scrollView.panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        pagingView.mainTableView.panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        
        return pagingView
    }()

    
    lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.isTitleColorGradientEnabled = true
        dataSource.itemSpacing = 40
        dataSource.titles = viewModel.emojisTypes
        dataSource.titleNormalColor = .white
        dataSource.titleSelectedColor = .mainBg
        dataSource.titleNormalFont = .font(size: 16.0)
        return dataSource
    }()
    
    //配置指示器
    lazy var indicator: JXSegmentedIndicatorBackgroundView = {
        let indicator = JXSegmentedIndicatorBackgroundView()
        indicator.isIndicatorConvertToItemFrameEnabled = true
        indicator.indicatorHeight = 32
        indicator.indicatorColor = .white
        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() {
        edgesForExtendedLayout = []
        navBarContentView.addSubview(navBarView)
        navBarView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(segmentedView)
        contentView.addSubview(pagingView)
    }
    
    
    @objc func clickBanner() {
        let vc = TSEmojisTutorialsVC()
        vc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc, animated: true)
    }
    
    
    override func dealThings() {
        vipBtn.isHidden = PurchaseManager.default.isVip
        NotificationCenter.default.addObserver(self, selector: #selector(vipInfoChanged), name: .kPurchaseDidChanged, object: nil)
    }
    
    @objc func vipInfoChanged() {
        kExecuteOnMainThread {
            self.vipBtn.isHidden = PurchaseManager.default.isVip
        }
    }
}



extension TSEmojisVC: JXPagingViewDelegate {

    func tableHeaderViewHeight(in pagingView: JXPagingView) -> Int {
        return viewModel.isHiddenBanner ? 0 : tableHeaderViewHeight
    }

    func tableHeaderView(in pagingView: JXPagingView) -> UIView {
        return viewModel.isHiddenBanner ? UIView() : headerView
    }

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

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

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

    func pagingView(_ pagingView: JXPagingView, initListAtIndex index: Int) -> JXPagingViewListViewDelegate {
        if let emojisType = viewModel.emojisTypes.safeObj(At: index) {
            return TSEmojisChildVC(emojisType: emojisType)
        }else{
            return TSEmojisChildVC(emojisType: "Universe🌍")
        }
    }
}

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

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