//
//  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 cellNum:Double!
    var btnViewWidth = CGFloat()
    var btnViewHeight = CGFloat()
    var dragCollectionViewDelegate = HomeCollectionViewDelegate()
    var dataSource = NSMutableArray()
    var layout = UICollectionViewFlowLayout()
    var xinfo = HomeCarouselTableViewCellModel()
    
    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
//        if #available(iOS 11.0, *) {
//            collectionView.contentInsetAdjustmentBehavior = .never
//            collectionView.scrollIndicatorInsets = collectionView.contentInset
//            collectionView.contentInset = UIEdgeInsetsMake(UIApplication.shared.statusBarFrame.size.height+44, 0, HEIGHT_TAB_BAR()+HEIGHT_BOTTOM_MARGIN(), 0)
//        }else{
//            collectionView.contentInset = UIEdgeInsetsMake(44+UIApplication.shared.statusBarFrame.size.height, 0, 0, 0)
//        }
        collectionView.frame = CGRect(x:0, y: 0, width:self.width , height:100 )
        collectionView.collectionViewLayout = layout
        registerCollectinViewCell(collectionView, cell: HomeCollectionViewCell.self)
        collectionView.backgroundColor = UIColor.clear
        
        return collectionView
    }()
    
    
    private lazy var pageControlView : PageControlVIew = {
        let pageControlView = PageControlVIew()
        pageControlView.frame = CGRect(x:0, y: 0, width:SCREEN_HEIGHT() , height:2.67 )
        pageControlView.num = 0
        return pageControlView
    }()
    
    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")
        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.white
        bannerView?.pageControlBottomOffset = 25
        bannerView?.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated
        bannerView?.showPageControl = false
        bannerView?.currentPageDotImage = UIImage.init(named: "pageControl_select_image")
        bannerView?.pageDotImage = UIImage.init(named: "pageControl_image")
//        bannerView?.currentPageDotImage = UIImage.init(named: "searchTextField_Bagimage")
        bannerView?.currentPageDotColor = statusBarColor
        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()
        self.contentView.addSubview(pageControlView)
//        self.addSubview(pagerView)
        self.contentView.addSubview(collectionView)
    }
    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:(190/360)*SCREEN_WHIDTH())
        initPageControlView()
//        pagerView.delegate = self
//        pagerView.dataSource = self
//        pagerView.frame = CGRect(x:0, y: 0, width:self.width , height:(200/360)*SCREEN_WHIDTH() + 10 )
//        pagerView.itemSize = CGSize.init(width: self.width - 60, height: self.height-10)
        self.cellNum = xinfo.detailedModel.cellNum
        btnViewWidth = (SCREEN_WHIDTH()-10)/CGFloat(cellNum)
        btnViewHeight = btnViewWidth*0.8
        collectionView.frame = CGRect(x:0, y: self.height-self.xinfo.collectionViewH, width:self.width , height:self.xinfo.collectionViewH)
        collectionView.backgroundColor = UIColor.white
        collectionView.layer.mask = self.configRectCorner(view: collectionView, corner: [.topRight, .topLeft], radii: CGSize(width: 15, height: 15))
        initCollectionView()
    }
    
    func initbannerView(){
        self.contentView.addSubview(bannerView)
    }
    func initPageControlView() {
        pageControlView.frame = CGRect(x:0, y: bannerView.height-40, width:getPageControlViewW() , height:2.67)
        if pageControlView.num != self.xinfo.imageUrlArray.count {
            pageControlView.num = self.xinfo.imageUrlArray.count
            pageControlView.addItemImage()
        }
        pageControlView.centerX = SCREEN_WHIDTH()/2
    }
    func getPageControlViewW() -> CGFloat {
        let w:CGFloat = CGFloat(self.xinfo.imageUrlArray.count) * CGFloat(13.33)
        let b:CGFloat = CGFloat(self.xinfo.imageUrlArray.count) * CGFloat(1.33) - CGFloat(1.33)
        return w+b
    }
    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)
        }
    }
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didScrollTo index: Int) {
//        print(index)
        pageControlView.switchImage(selectIndex: index)
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    func configRectCorner(view: UIView, corner: UIRectCorner, radii: CGSize) -> CALayer {
        let maskPath = UIBezierPath.init(roundedRect: view.bounds, byRoundingCorners: corner, cornerRadii: radii)
        let maskLayer = CAShapeLayer.init()
        maskLayer.frame = view.bounds
        maskLayer.path = maskPath.cgPath
        return maskLayer
    }
}
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 bannerViewH = CGFloat()
    var collectionViewH = CGFloat()
    
    var block:passParameterBlock!
    var detailedModel = MineDetailedTableViewCellModel()
}

