//
//  VoiceDialogueLeftAppTVC+collectionView.swift
//  MobilePortal
//
//  Created by caiyang on 2020/5/18.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension VoiceDialogueLeftAppTVC {
    //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)
        layout.scrollDirection = .horizontal
        collectionView.collectionViewLayout = layout
        collectionView.register(ChannelHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: VoiceDialogueLeftAppIdentifier)
        registerCollectinViewCell(self.collectionView, cell: VoiceDialogueLeftAppCTC.self)
        
        dragCollectionViewDelegate.dataSource = self.xinfo.appArray
        dragCollectionViewDelegate.cellNum = self.cellNum
        dragCollectionViewDelegate.btnViewWidth = self.btnViewWidth
        dragCollectionViewDelegate.btnViewHeight = self.btnViewHeight
        collectionView.delegate = self.dragCollectionViewDelegate
        collectionView.dataSource = self.dragCollectionViewDelegate
//        collectionView.isScrollEnabled = false
        collectionView.showsHorizontalScrollIndicator = false
        self.collectionView.reloadData()
        //        isEdite = true
    }
}
private let VoiceDialogueLeftAppIdentifier = "VoiceDialogueLeftAppIdentifier"
class VoiceDialogueLeftAppTVCDelegate: 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, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let info:VoiceDialogueLeftAppCTCModel = self.dataSource[self.getDataSourceIndex(indexPath: indexPath)] as! VoiceDialogueLeftAppCTCModel
        return CGSize(width:info.cellW, height:btnViewHeight)
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VoiceDialogueLeftAppCTC", for: indexPath) as! VoiceDialogueLeftAppCTC
        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: 0, left: 10, bottom: 0, right: 10)
    }
    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
    }
}
