//
//  MineMoreViewController+initTableView.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/18.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

extension MineMoreViewController {

    func addInitTableView() {
        self.delegate = MineMoreViewControllerDelegate()
        self.delegate.dataSource = self.dataSource
        self.delegate.cellNum = 4
        self.delegate.editStatusType = self.editStatusType
        self.delegate.navigationBarH = (self.navigationController?.navigationBar.frame.size.height)!
        self.view.addSubview(myTableView)
        myTableView.snp.makeConstraints { (make) in
            make.width.height.equalToSuperview()
            make.center.equalToSuperview()
        }
        self.myTableView.delegate = self.delegate
        self.myTableView.dataSource = self.delegate
    }

}
class MineMoreViewControllerDelegate: BaseOneTableViewDelegate {
    var cellNum:Double!
    var navigationBarH = CGFloat()
    var oneH = CGFloat()
    var editStatusType:EditStatusType = .StartEditing
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if (section==0){
            return 0
        }
        return 0
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let tableCellModel:BaseCellModel = self.dataSource[indexPath.row] as! BaseCellModel
        if tableCellModel.className == "MineMoreAllTableViewCell" {
            return 26*HEIGHT_RATIO()
        }else if indexPath.row == 0 {
            if self.editStatusType == .StartEditing{
                oneH = self.getoneSectionH(indexPath: indexPath)
            }else{
                oneH = 0
            }
            return oneH
        }
//        tableView.contentInset.top
        return tableView.height-oneH-26*HEIGHT_RATIO()-tableView.contentInset.top
    }
    ///根据内容 获取tableCell的高度
    func getoneSectionH(indexPath: IndexPath)->CGFloat{
        let tableCellModel:MineMoreTableViewCellModel = self.dataSource[0] as! MineMoreTableViewCellModel
        let headerH:CGFloat = CGFloat(tableCellModel.collcetionViewSectionModellArray.count*26)
        let collcetionViewModel:MineMoreTwoTableViewCellModel = tableCellModel.collcetionViewSectionModellArray[0] as! MineMoreTwoTableViewCellModel
        if collcetionViewModel.collcetionViewSectionModellArray.count >= Int(self.cellNum){
            let index = lroundf(Float(ceil(Double(collcetionViewModel.collcetionViewSectionModellArray.count)/self.cellNum)))
            return ((SCREEN_WHIDTH()+10)/4)*CGFloat(index)*0.8+headerH*HEIGHT_RATIO()
        }
        return ((SCREEN_WHIDTH()+10)/4)*0.8+headerH*HEIGHT_RATIO()
    }
    
    internal override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return self.tableViewSectionZero(tableView: tableView, cellForRowAtIndexPath: indexPath as NSIndexPath)
    }
    
    override func tableViewSectionZero(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell{
        let info:BaseCellModel = self.dataSource.object(at: indexPath.row) as! BaseCellModel
        if info.className == "MineMoreAllTableViewCell" {
            let cellClass = swiftClassFromString(info.className) as! BaseTableViewCell.Type
            let cell = getCell(tableView, cell: cellClass.self, indexPath: indexPath as IndexPath)
            cell.configurateTheCell(info)
            return cell
        }
        let cellClass = swiftClassFromString(info.className) as! MineMoreTableViewCell.Type
        let cell = getCell(tableView, cell: cellClass.self, indexPath: indexPath as IndexPath)
        cell.editStatusType = self.editStatusType
        if indexPath.row == 0 {
            cell.operationType = .DeleteType
        }else{
            cell.operationType = .AddType
        }
        cell.configurateTheCell(info)
        return cell
    }
    
}
