//
//  LNMateAdBannerCell.swift
//  Lanu
//
//  Created by Codex on 2026/4/8.
//

import Foundation
import UIKit
import SnapKit

final class LNMateAdBannerCell: UITableViewCell {
    static let reuseID = "LNMateAdBannerCell"

    var onTapBanner: ((_ item: LNBannerInfoVO, _ index: Int) -> Void)?

    private let pagerView = LNCyclePager()
    private let pageControl = LNCyclePageControl()
    private var items: [LNBannerInfoVO] = []

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
        setupPager()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func update(items: [LNBannerInfoVO]) {
        self.items = items
        pageControl.numberOfPages = items.count
        pageControl.currentPage = 0
        pageControl.isHidden = items.count <= 1
        pagerView.reloadData()
    }

}

private extension LNMateAdBannerCell {
    func setupViews() {
        backgroundColor = .clear
        selectionStyle = .none
        contentView.layer.masksToBounds = true
        contentView.layer.cornerRadius = 16

        contentView.addSubview(pagerView)
        pagerView.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.leading.equalToSuperview().offset(16)
            make.trailing.equalToSuperview().offset(-16)
        }

        pagerView.addSubview(pageControl)
        pageControl.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-8)
            make.height.equalTo(10)
        }
    }

    func setupPager() {
        pagerView.isInfiniteLoop = true
        pagerView.autoScrollInterval = 3
        pagerView.reloadDataNeedResetIndex = true
        pagerView.bindPageControl(pageControl)
        pagerView.register(LNAdBannerImageCell.self, forCellWithReuseIdentifier: LNAdBannerImageCell.className)

        pagerView.numberOfItemsProvider = { [weak self] in
            self?.items.count ?? 0
        }
        pagerView.cellProvider = { [weak self] pager, index in
            guard let self else { return UICollectionViewCell() }
            let cell = pager.dequeueReusableCell(
                withReuseIdentifier: LNAdBannerImageCell.className,
                for: index
            ) as! LNAdBannerImageCell
            cell.update(items[index])
            return cell
        }
        pagerView.layoutProvider = { [weak self] in
            let layout = LNCyclePagerViewLayout()
            let size = self?.pagerView.bounds.size ?? .zero
            layout.itemSize = size
            layout.itemSpacing = 0
            layout.scrollDirection = .horizontal
            layout.layoutType = .normal
            return layout
        }
        pagerView.didScrollFromIndexToIndex = { [weak self] _, toIndex in
            self?.pageControl.currentPage = toIndex
        }
        pagerView.didSelectItem = { [weak self] _, index in
            guard let self, index >= 0, index < items.count else { return }
            let item = items[index]
            onTapBanner?(item, index)
            guard !item.jump.isEmpty else { return }
            pushToWebView(.init(url: item.jump))
        }

        pageControl.pageIndicatorSize = CGSize(width: 6, height: 6)
        pageControl.currentPageIndicatorSize = CGSize(width: 16, height: 6)
        pageControl.pageIndicatorTintColor = .init(hex: "#FFFFFF80")
        pageControl.currentPageIndicatorTintColor = .white
        pageControl.pageIndicatorSpacing = 4
    }
}
