//
//  YHDavidCell0.swift
//  galaxy
//
//  Created by davidhuangA on 2024/1/17.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import FSPagerView

class YHDavidCell4: UITableViewCell {
    
    fileprivate var dataArr: [YHBannerModel] = [YHBannerModel(),YHBannerModel(),YHBannerModel(),YHBannerModel(),YHBannerModel()]
    
    static let cellHeight : CGFloat = 88 + 20
    
    static let cellReuseIdentifier = "YHDavidCell4"

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        setupUI()
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
  
    lazy var subContentView : UIView = {
        let view = UIView()
//        view.backgroundColor = .red
        return view
    }()
    
    private lazy var bannerView: FSPagerView = {
        let view = FSPagerView()
        view.delegate = self
        view.dataSource = self
        view.automaticSlidingInterval = 2
        view.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
        view.itemSize = FSPagerView.automaticSize
        view.interitemSpacing = 10
        view.isInfinite = true
        return view
    }()
    
    lazy var pageControl: FSPageControl = {
        let view = FSPageControl()
        return view
    }()
    
    func setupUI() {
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        
        contentView.addSubview(subContentView)
        subContentView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.bottom.equalToSuperview()
        }
        
        subContentView.addSubview(bannerView)
        bannerView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(80)
        }
        
        subContentView.addSubview(pageControl)
        pageControl.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(bannerView.snp.bottom)
            make.height.equalTo(20)
        }
        
        setupPageControl()
        bannerView.reloadData()
    }
    
    
    func setupPageControl() {
        self.pageControl.numberOfPages = self.dataArr.count
        self.pageControl.contentHorizontalAlignment = .center
        let normalImage = UIImage(color: .white, size: CGSize(width: 15, height: 5))
        let selectImage =  UIImage(color: UIColor(hex: 0xff0000, alpha: 1.0), size: CGSize(width: 20, height: 5))
        self.pageControl.setImage(normalImage, for: .normal)
        self.pageControl.setImage(selectImage, for: .selected)
        
        self.pageControl.interitemSpacing = 15
    }
}


// MARK: - FSPagerViewDelegate
extension YHDavidCell4: FSPagerViewDataSource, FSPagerViewDelegate {
    
    public func numberOfItems(in pagerView: FSPagerView) -> Int {
        return self.dataArr.count
    }
    
    public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
        let image = UIImage(named: "default_placeholder_image")?.bs_scaled(toWidth: UIDevice.kScreenW - 16*2, opaque: false)
        cell.imageView?.image = image
        if index < dataArr.count {
            let model = dataArr[index]
            if let url = URL(string: model.img_url) {
                cell.imageView?.kf.setImage(with: url)
            }
            
            
//            cell.textLabel?.text = model.title
        }
        cell.imageView?.contentMode = .scaleAspectFill
        cell.backgroundColor = .commonColor3
        cell.contentView.layer.shadowColor = UIColor.clear.cgColor
        
        return cell
    }
    
    func pagerViewWillEndDragging(_ pagerView: FSPagerView, targetIndex: Int) {
        self.pageControl.currentPage = targetIndex
    }
    
    func pagerViewDidEndScrollAnimation(_ pagerView: FSPagerView) {
        self.pageControl.currentPage = pagerView.currentIndex
    }
}
