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

import UIKit

extension ServiceViewController {
    func initDragCollectionView(){
        cellNum = 3
        self.dataSource = NSMutableArray()
        //initData()
        delegate.dataSource = self.dataSource
        delegate.cellNum = self.cellNum
        delegate.block = {[weak self](value:Any) in
            let model:ServiceCollectionViewCellModel = value as! ServiceCollectionViewCellModel
            //print(model.titleValue)
            self?.actionGotoWeb(webViewUrl: model.url!, isHomePushType: .YesIsHomePushType, isShowTitleType: .YesShowTitleType, appid: "", isEncryptionType: .NoEncryptionType)
        }
        self.view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.width.height.equalToSuperview()
            make.center.equalToSuperview()
        }
        collectionView.delegate = self.delegate
        collectionView.dataSource = self.delegate
    }
    func initData(){
        //        for index in 0..<titleArr.count {
        //            self.dataSource.add(self.getServiceCollectionViewCellModel(title: self.titleArr[index], imageName: self.imageArr[index]))
        //        }
    }
    func getServiceCollectionViewCellModel(httpModel:ServiceHttpListModel)->ServiceCollectionViewCellModel{
        let model = ServiceCollectionViewCellModel()
        model.className = "ServiceCollectionViewCell"
        model.titleValue = httpModel.title
        model.imageName = httpModel.imageUrl
        model.url = httpModel.comments
        model.id = httpModel.id
        model.value1 = httpModel.value1
        model.block = {[weak self](value:Any) in
            self?.openWeb(httpModel: httpModel)
        }
        return model
    }
    func openWeb(httpModel:ServiceHttpListModel){
        let url:String = AccountInfo.sharedInstance().getServiceVCUrl()+httpModel.id!
        self.appViewsSstatistics(httpModel: httpModel)
        //value1 备选页面
        if httpModel.value1?.count != 0 {
            self.actionGotoTitleWeb(webViewUrl: httpModel.value1!, title: httpModel.title!, isHomePushType: .YesIsHomePushType, appid: "", isEncryptionType: .NoEncryptionType)
        }else{
            self.actionGotoTitleWeb(webViewUrl: url, title: httpModel.title!, isHomePushType: .YesIsHomePushType, appid: "", isEncryptionType: .NoEncryptionType)
        }
    }
    /// 应用访问量
    func appViewsSstatistics(httpModel:ServiceHttpListModel){
        let model = ClickRateModel()
        model.name = httpModel.title
        model.belong = "门户"
        model.scope = httpModel.type
        model.type = "4"
        self.getClickRateHttpCmd(infoModel: model)
    }
    
}
class ServiceViewControllerDelegate: NSObject ,UICollectionViewDelegate,UICollectionViewDataSource{
    var dataSource:NSMutableArray!
    var oneCellArray:NSMutableArray!
    var twoCellArray:NSMutableArray!
    var updateBlock:passParameterBlock!
    var btnViewWidth = CGFloat()
    var btnViewHeight = CGFloat()
    var cellNum:Double!
    var arrayIndex: Int!
    var editStatusType:EditStatusType = .StartEditing
    var block:passParameterBlock!
    
    //MARK: - 代理方法
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        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: "ServiceCollectionViewCell", for: indexPath) as! ServiceCollectionViewCell
        let info:BaseCollectionViewCellModel = dataSource[indexPath.row] as! BaseCollectionViewCellModel
        cell.configurateTheCell(info: info)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let info:ServiceCollectionViewCellModel = dataSource[indexPath.row] as! ServiceCollectionViewCellModel
        if info.id != nil && info.id?.count != 0 {
            info.block(info)
        }
    }
}
