//
//  HomeCarouselTableViewCell.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/12.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit
import SDCycleScrollView
import FSPagerView

class HomeCarouselTableViewCell: BaseTableViewCell ,SDCycleScrollViewDelegate{
    
    var xinfo = HomeCarouselTableViewCellModel()
    
    private lazy var pagerView : FSPagerView = {
        let pagerView = FSPagerView()
        pagerView.automaticSlidingInterval =  3
        pagerView.isInfinite = true
        pagerView.interitemSpacing = 15
        pagerView.transformer = FSPagerViewTransformer(type: .linear)
        pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
        self.addSubview(pagerView)
        return pagerView
    }()
    private lazy var bannerView : SDCycleScrollView = {
        let bannerView = SDCycleScrollView.init(frame: CGRect(x:0, y: 0, width:self.width , height:self.height ), delegate: self, placeholderImage: UIImage.init(named: "center_image"))
        bannerView?.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter
        bannerView?.titleLabelBackgroundColor = UIColor.clear
        bannerView?.pageDotColor = UIColor.clear
        bannerView?.currentPageDotColor = UIColor.clear
        bannerView?.autoScrollTimeInterval = 5.0
        return bannerView!
    }()
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.backgroundColor = UIColor.white
        self.separatorInset=UIEdgeInsetsMake(0, SCREEN_WHIDTH(), 0, 0)
        initbannerView()
    }
    override func configurateTheCell(_ info: BaseCellModel) {
        xinfo = info as! HomeCarouselTableViewCellModel
        bannerView.imageURLStringsGroup = xinfo.imageUrlArray as? [Any]
        bannerView.frame = CGRect(x:0, y: 0, width:self.width , height:self.height )
//        pagerView.delegate = self
//        pagerView.dataSource = self
//        pagerView.frame = CGRect(x:0, y: 0, width:self.width , height:self.height )
//        pagerView.itemSize = CGSize.init(width: self.width - 60, height: self.height-10)
    }
    func initbannerView(){
        self.addSubview(bannerView)
    }
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didSelectItemAt index: Int) {
        //print(index)
        //self.xinfo.block(xinfo.pageUrlArray[index] as AnyObject)
        let url:String = xinfo.pageUrlArray[index] as! String
        if url != nil && url.count != 0 {
            self.xinfo.block(url as AnyObject)
        }
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        
    }
    
}
extension HomeCarouselTableViewCell:FSPagerViewDelegate,FSPagerViewDataSource{
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return xinfo.imageUrlArray.count
    }
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
        let imageUrl = xinfo.imageUrlArray[index]
        cell.imageView?.sd_setImage(with: URL(string:imageUrl as! String), placeholderImage: UIImage.init(named: "center_image"), completed: { (image, error, SDImageCacheTypeNone, url) in
            if (error != nil) {print(error ?? Error.self)}
        })
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
//        let url:String = self.focus?.data?[index].link ?? ""
//        delegate?.recommendHeaderBannerClick(url: url)
        let url:String = xinfo.pageUrlArray[index] as! String
        if url != nil && url.count != 0 {
            xinfo.block(url as AnyObject)
        }
        
    }
}
class HomeCarouselTableViewCellModel: BaseCellModel {
    
    var imageUrlArray = NSMutableArray()
    var titleUrlArray = NSMutableArray()
    var pageUrlArray = NSMutableArray()
    var block:passParameterBlock!
}

