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

import UIKit

extension MineMoreTableViewCell {
    func addPress(){
        collectionView.addSubview(dragingItem)
        let longPressGes = UILongPressGestureRecognizer(target: self, action: #selector(longPressGesture(_:)))
        collectionView.addGestureRecognizer(longPressGes)
    }
    //MARK: - 长按动作
    @objc func longPressGesture(_ tap: UILongPressGestureRecognizer) {
        if self.editStatusType == .FinishEditing || self.operationType == .AddType {
            return
        }
        let point = tap.location(in: collectionView)
        
        switch tap.state {
        case UIGestureRecognizerState.began:
            dragBegan(point: point)
        case UIGestureRecognizerState.changed:
            drageChanged(point: point)
        case UIGestureRecognizerState.ended:
            drageEnded(point: point)
        case UIGestureRecognizerState.cancelled:
            drageEnded(point: point)
        default: break
            
        }
        
    }
    //MARK: - 长按开始
    private func dragBegan(point: CGPoint) {
        indexPath = collectionView.indexPathForItem(at: point)
        if indexPath == nil || (indexPath?.section)! > 0{return}
        //if indexPath == nil {return}
        let item = collectionView.cellForItem(at: indexPath!) as? MineMoreCollectionViewCell
        item?.isHidden = true
        dragingItem.isHidden = false
        dragingItem.frame = (item?.frame)!
        dragingItem.configurateTheCell(info: (item?.modelHttp)!)
        dragingItem.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
    }
    //MARK: - 长按过程
    private func drageChanged(point: CGPoint) {
        if indexPath == nil {return}
        dragingItem.center = point
        targetIndexPath = collectionView.indexPathForItem(at: point)
        
        if targetIndexPath == nil || indexPath == targetIndexPath || (targetIndexPath?.section)! > 0 {
            return
        }
        //交换位置
        // 更新数据
        //dataSource.exchangeObject(at: self.getDataSourceIndex(indexPath: indexPath!), withObjectAt: self.getDataSourceIndex(indexPath: targetIndexPath!))
        let classModel:MineMoreTwoTableViewCellModel = self.dataSource[0] as! MineMoreTwoTableViewCellModel
        let arr:NSMutableArray = classModel.collcetionViewSectionModellArray
        //let arr:NSMutableArray = self.dataSource[0] as! NSMutableArray
        arr.exchangeObject(at: self.getDataSourceIndex(indexPath: indexPath!), withObjectAt: self.getDataSourceIndex(indexPath: targetIndexPath!))
        collectionView.moveItem(at: indexPath!, to: targetIndexPath!)
        indexPath = targetIndexPath
    }
    func setdataSource(array:NSMutableArray)  {
        for index in 0..<array.count {
            let model:MineMoreCollectionViewCellModel = array[index] as! MineMoreCollectionViewCellModel
            print(model.titleValue ?? String())
        }
    }
    func getDataSourceIndex(indexPath:IndexPath) -> Int {
        if indexPath.section == 0 {
            return indexPath.row
        }
        return indexPath.section*Int(self.cellNum) + indexPath.row
    }
    //MARK: - 长按结束
    private func drageEnded(point: CGPoint) {
        if indexPath == nil  {
            return
        }
        if targetIndexPath == nil || (targetIndexPath?.section)! > 0 {
            targetIndexPath = indexPath
        }
        let endCell = collectionView.cellForItem(at: targetIndexPath!) as? MineMoreCollectionViewCell
        UIView.animate(withDuration: 0.25, animations: {
            self.dragingItem.transform = CGAffineTransform.identity
            self.dragingItem.center = (endCell?.center)!
        }, completion: {
            (finish) -> () in
            endCell?.isHidden = false
            self.dragingItem.isHidden = true
            self.indexPath = nil
        })
    }
}
