//
//  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 startEditingH = 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 self.editStatusType == .StartEditing {
            startEditingH = 0
        } else {
            startEditingH = 90
        }

        if tableCellModel.className == "MineMoreAllTableViewCell" {
            let moreModel:MineMoreAllTableViewCellModel = tableCellModel as! MineMoreAllTableViewCellModel
            moreModel.editStatusType = self.editStatusType
            return 45
        }else if tableCellModel.className == "NormalTableViewCell"{
            return 19.33
        } else if tableCellModel.className == "StartEditingTableViewCell"{
            return startEditingH
        }else if indexPath.row == 1 && tableCellModel.className == "MineMoreTableViewCell"{
//            if self.editStatusType == .StartEditing{
                oneH = self.getoneSectionHTwo(indexPath: indexPath)
//            }else{
//                oneH = 0
//            }
//            print(oneH)
            return oneH
        }
        return self.getoneSectionHTwo(indexPath: indexPath)
//        return tableView.height-oneH-90-tableView.contentInset.top-19.33-startEditingH
    }
    ///根据内容 获取tableCell的高度
    func getoneSectionHTwo(indexPath: IndexPath)->CGFloat{
        var h = CGFloat()
        let tableCellModel:MineMoreTableViewCellModel = self.dataSource[indexPath.row] as! MineMoreTableViewCellModel
        var headerH:CGFloat = CGFloat(tableCellModel.collcetionViewSectionModellArray.count*45)
        if tableCellModel.lineViewShowType {
            headerH = 0
        }
        for index in 0..<tableCellModel.collcetionViewSectionModellArray.count {
            let collcetionViewModel:MineMoreTwoTableViewCellModel = tableCellModel.collcetionViewSectionModellArray[index] as! MineMoreTwoTableViewCellModel
            if collcetionViewModel.retractUnfoldType == .UnfoldType{
                h = h + 0
            } else {
                if collcetionViewModel.collcetionViewSectionModellArray.count >= Int(self.cellNum){
                    let index = lroundf(Float(ceil(Double(collcetionViewModel.collcetionViewSectionModellArray.count)/self.cellNum)))
                    h = h + (SCREEN_WHIDTH()/4)*CGFloat(index)*0.8
                } else if collcetionViewModel.collcetionViewSectionModellArray.count == 0 {
                    h = h + 20
                } else {
                    h = h + (SCREEN_WHIDTH()/4)*0.8
                }
            }
        }
        return h+headerH
    }
    func getoneSectionH(indexPath: IndexPath)->CGFloat{
        let tableCellModel:MineMoreTableViewCellModel = self.dataSource[indexPath.row] as! MineMoreTableViewCellModel
        var headerH:CGFloat = CGFloat(tableCellModel.collcetionViewSectionModellArray.count*45)
        if tableCellModel.lineViewShowType {
            headerH = 0
        }
        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()/4)*CGFloat(index)*0.8+headerH
        }
        return (SCREEN_WHIDTH()/4)*0.8+headerH
    }
    
    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 == "MineMoreTableViewCell"{
            let tableCellModel:MineMoreTableViewCellModel = info as! MineMoreTableViewCellModel
            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 {
            if tableCellModel.lineViewShowType {
                cell.operationType = .DeleteType
            }else{
                cell.operationType = .AddType
            }
            cell.configurateTheCell(info)
            return cell
        }
        let cellClass = swiftClassFromString(info.className) as! BaseTableViewCell.Type
        let cell = getCell(tableView, cell: cellClass.self, indexPath: indexPath as IndexPath)
        cell.configurateTheCell(info)
        
        return cell
    }
    
}
