//
//  BsHomeBannerView.swift
//  BaiSiSMApp
//
//  Created by SJ on 2022/11/9.
//  Copyright © 2022 www.davidhuang.com. All rights reserved.
//

import Foundation
import FSPagerView
import Kingfisher
import SwifterSwift

class YHHomeBannerView: UIView {
    
    // MARK: - constant 常量
    // 位置、大小、图片、文本
    
    fileprivate var dataArr: [YHBannerModel] = [YHBannerModel(),YHBannerModel(),YHBannerModel(),YHBannerModel(),YHBannerModel()]
    
    
    // MARK: - life cycle 生命周期
    //工程的viewDidLoad、viewWillAppear、init、didReceiveMemoryWarning等方法都放在这里
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - view layout 子视图的布局
    //界面布局
    
    func initView() {
        addSubview(bannerView)
        bannerView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
        }
        
        bannerView.addSubview(pageControl)
        pageControl.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(25)
        }
        
        setupPageControl()
        bannerView.reloadData()
    }
    
    func setupPageControl() {
        self.pageControl.numberOfPages = self.dataArr.count
        self.pageControl.contentHorizontalAlignment = .center
        self.pageControl.contentInsets = UIEdgeInsets(top: 0, left: 12, bottom: 10, right: 12)
        
        
        let normalImage = UIImage(color: UIColor(hex: 0xff0000, alpha: 1.0), size: CGSize(width: 5, height: 5))//UIImage(color: UIColor(white: 1, alpha: 0.5), size: CGSize(width: 5, height: 5))
        let selectImage = UIImage(color: .white, size: CGSize(width: 8, height: 5))
        self.pageControl.setImage(normalImage, for: .normal)
        self.pageControl.setImage(selectImage, for: .selected)
        
        self.pageControl.interitemSpacing = 3
    }
    
//    public func refreshUI(_ arr: [YHBannerModel]) {
//        
//        let model : YHBannerModel = YHBannerModel()
//        model.img_url = "https://upload-cdn.galaxy-immi.com/sell/test/1668772072522.jpg"
//        
//        model.link_type = 0
//        model.title = "我是测试数据"
//        
//        let arr = [model,model,model,model,model]
//        
//        self.dataArr = arr
//        setupPageControl()
//        bannerView.reloadData()
//    }
    
    private lazy var bannerView: FSPagerView = {
        let view = FSPagerView()
        view.delegate = self
        view.dataSource = self
        view.automaticSlidingInterval = 3
        view.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
        view.itemSize = FSPagerView.automaticSize
        view.interitemSpacing = 10
        return view
    }()
    
    lazy var pageControl: FSPageControl = {
        let view = FSPageControl()
        return view
    }()
    
}


// MARK: - delegate 具体到某个delegate，比如UITableViewDelegate
//代理或者数据源


// MARK: - FSPagerViewDelegate
extension YHHomeBannerView: 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
    }
    
    // MARK: - FSPagerView Delegate
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: true)
        pagerView.scrollToItem(at: index, animated: true)
        if index >= dataArr.count {
            return
        }
        let model = dataArr[index]
        switch model.link_type {
        case 10: //跳转H5
//            let vc = BsWebViewController()
//            vc.urlString = model.link_url
//            UIViewController.navTopViewController?.navigationController?.pushViewController(vc, animated: true)
            break
        case 20: //跳转原生页面
            if let vc = UIViewController.classFromString(model.link_url) {
                UIViewController.navTopViewController?.navigationController?.pushViewController(vc, animated: true)
            }
            break
        default:
            break
        }
    }
    
    func pagerViewWillEndDragging(_ pagerView: FSPagerView, targetIndex: Int) {
        self.pageControl.currentPage = targetIndex
    }
    
    func pagerViewDidEndScrollAnimation(_ pagerView: FSPagerView) {
        self.pageControl.currentPage = pagerView.currentIndex
    }
}
