//
//  MineMoreTableViewCell+initCollectionView.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/26.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

extension MineMoreTableViewCell {
    func initDragCollectionView(){
        cellNum = 4
        btnViewWidth = (SCREEN_WHIDTH()-10)/CGFloat(cellNum)
        btnViewHeight = btnViewWidth*0.8
        
        self.dataSource = xinfo.collcetionViewSectionModellArray
        registerCollectinViewCell(self.collectionView, cell: MineMoreCollectionViewCell.self)
        collectionView.register(ChannelHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: ChannelViewHeaderIdentifier)
        collectionView.backgroundColor = UIColor.white
        collectionViewDelegate.btnViewHeight = btnViewHeight
        collectionViewDelegate.btnViewWidth = btnViewWidth
        collectionViewDelegate.vcTitle = xinfo.vcTitle
        collectionViewDelegate.dataSource = self.dataSource
        collectionViewDelegate.cellNum = self.cellNum
        collectionViewDelegate.editStatusType = self.editStatusType
        collectionViewDelegate.changeBlock = self.xinfo.changeBlock
        collectionViewDelegate.updateBlock = {[weak self] (value:Any,value1:Any) in
            self?.arrDelete(classModel: value as! MineMoreTwoTableViewCellModel, model: value1 as! MineMoreCollectionViewCellModel)
        }
        collectionView.delegate = self.collectionViewDelegate
        collectionView.dataSource = self.collectionViewDelegate
        addPress()
    }
    func arrDelete(classModel: MineMoreTwoTableViewCellModel,model:MineMoreCollectionViewCellModel){
        self.dataSource[0] = classModel
        self.xinfo.block(self.dataSource,model)
        //self.collectionViewDelegate.dataSource = self.dataSource
        //self.collectionView.delegate = self.collectionViewDelegate
        //self.collectionView.reloadData()
    }
}
private let ChannelViewHeaderIdentifier = "ChannelViewHeaderIdentifier"
class HomeMoreTwoTableViewCellCollectionViewDelegate: NSObject ,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    var dataSource:NSMutableArray!
    var oneCellArray:NSMutableArray!
    var twoCellArray:NSMutableArray!
    var updateBlock:passTwoParameterBlock!
    var btnViewWidth = CGFloat()
    var btnViewHeight = CGFloat()
    var cellNum:Double!
    var arrayIndex: Int!
    var editStatusType:EditStatusType = .StartEditing
    var operationType:OperationType = .DeleteType
    var changeBlock:passParameterBlock!
    var vcTitle:String?
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width:btnViewWidth, height:btnViewHeight)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: SCREEN_WHIDTH(), height: 26*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
    }
    //MARK: - 代理方法
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return self.dataSource.count
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let arr:MineMoreTwoTableViewCellModel = self.dataSource[section] as! MineMoreTwoTableViewCellModel
        return arr.collcetionViewSectionModellArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MineMoreCollectionViewCell", for: indexPath) as! MineMoreCollectionViewCell
        let classModel:MineMoreTwoTableViewCellModel = self.dataSource[indexPath.section] as! MineMoreTwoTableViewCellModel
        let info:BaseCollectionViewCellModel = classModel.collcetionViewSectionModellArray[indexPath.row] as! BaseCollectionViewCellModel
        cell.configurateTheCell(info: info)
        
        cell.editStatusType = self.editStatusType
        //cell.operationType = self.operationType
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell:MineMoreCollectionViewCell = collectionView.cellForItem(at: indexPath) as! MineMoreCollectionViewCell
        if self.editStatusType == .FinishEditing {
            cell.modelHttp.block("" as AnyObject)
            return
        }
        let classModel:MineMoreTwoTableViewCellModel = self.dataSource[indexPath.section] as! MineMoreTwoTableViewCellModel
        let info:MineMoreCollectionViewCellModel = classModel.collcetionViewSectionModellArray[indexPath.row] as! MineMoreCollectionViewCellModel
        
        //classModel.collcetionViewSectionModellArray.count
        if info.operationType == .AddType && self.operationType == .AddType {
            if AccountInfo.sharedInstance().myApplicationCellNum == nil || (AccountInfo.sharedInstance().myApplicationCellNum! as NSString).floatValue < 11 {
                info.operationType = .DeleteType
                cell.configurateTheCell(info: info)
                self.changeBlock(info)
            }else{
                SMToastView.showMessage("最多可选择11个！")
            }
        }else if info.operationType == .DeleteType && self.operationType == .AddType {
            //cell.operationType = .AddType
            info.operationType = .AddType
            cell.configurateTheCell(info: info)
            self.changeBlock(info)
        }else if self.operationType == .DeleteType{
            classModel.collcetionViewSectionModellArray.remove(info)
            self.updateBlock(classModel,info)
        }
        
    }
    //是否可以移动
    func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let classModel:MineMoreTwoTableViewCellModel = self.dataSource[indexPath.section] as! MineMoreTwoTableViewCellModel
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: ChannelViewHeaderIdentifier, for: indexPath) as! ChannelHeaderView
        header.text = classModel.vcTitle
        return header
    }
}
