//
//  HomeCarouselTableViewCell+collectionView.swift
//  MobilePortal
//
//  Created by caiyang on 2020/3/30.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension HomeCarouselTableViewCell {
    //MARK: CollectionViewCell
    func initCollectionView(){
        //最小行间距
        layout.minimumLineSpacing = 0
        //设置垂直间距
        layout.minimumInteritemSpacing = 0
        //设置 cell 上下左右的间距
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.itemSize = CGSize(width:btnViewWidth, height:btnViewHeight)
        collectionView.collectionViewLayout = layout
        collectionView.register(ChannelHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: MineViewHeaderIdentifier)
        registerCollectinViewCell(self.collectionView, cell: HomeCollectionViewCell.self)
//        collectionView.backgroundColor = UIColor.clear
        
        dragCollectionViewDelegate.dataSource = self.xinfo.detailedModel.detaileArray
        dragCollectionViewDelegate.cellNum = self.cellNum
//        dragCollectionViewDelegate.titleValue = self.xinfo.titleValue
//        dragCollectionViewDelegate.lineViewShowType = self.xinfo.lineViewShowType
        collectionView.delegate = self.dragCollectionViewDelegate
        collectionView.dataSource = self.dragCollectionViewDelegate
        //collectionView.isPagingEnabled = false
        //collectionView.showsVerticalScrollIndicator = false
        collectionView.isScrollEnabled = false
        //        collectionView.addSubview(dragingItem)
        self.collectionView.reloadData()
        //        isEdite = true
    }
    
    
}
private let MineViewHeaderIdentifier = "MineViewHeaderIdentifier"
class HomeCollectionViewDelegate: NSObject ,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    var dataSource:NSMutableArray!
    var updateBlock:passParameterBlock!
    var btnViewWidth = CGFloat()
    var btnViewHeight = CGFloat()
    var cellNum:Double!
    var arrayIndex: Int!
    var lineViewShowType: Bool!
    
    var titleValue:String? // 服务范围
    //MARK: - 代理方法
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        //return lroundf(Float(Double(dataSource.count)/self.cellNum))
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionViewCell", for: indexPath) as! HomeCollectionViewCell
        let info:BaseCollectionViewCellModel = self.dataSource[self.getDataSourceIndex(indexPath: indexPath)] as! BaseCollectionViewCellModel
        cell.configurateTheCell(info: info)
        return cell
    }
    func getDataSourceIndex(indexPath:IndexPath) -> Int {
        if indexPath.section == 0 {
            return indexPath.row
        }
        return indexPath.section*Int(self.cellNum) + indexPath.row
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //添加 删除
        
    }
    //是否可以移动
    func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
    }
    //    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    //        if self.titleValue == nil || self.titleValue?.count == 0 {
    //           return CGSize(width: SCREEN_WHIDTH(), height: 0)
    //        }
    //        return CGSize(width: SCREEN_WHIDTH(), height: 25*HEIGHT_RATIO())
    //    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    //    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    //        let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: MineViewHeaderIdentifier, for: indexPath) as! ChannelHeaderView
    //        header.lineViewShowType = lineViewShowType
    //        header.text = titleValue
    //        return header
    //    }
}
