//
//  HomeDicViewController+initTableView.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2020/1/20.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension HomeDicViewController {
    func addInitTableView() {
        //self.myTableView.backgroundColor = UIColor.clear
        self.delegate = HomeDicViewControllerDelegate()
        self.delegate.slidingBlock = {[weak self] in
            self?.scrollViewDidScroll(scrollView: (self?.myTableView)!)
        }
        self.delegate.dataSource = self.dataSource
        self.delegate.cellNum = self.cellNum
        self.myTableView.delegate = self.delegate
        self.myTableView.dataSource = self.delegate
        //self.myTableView.separatorStyle = .none
    }
    func scrollViewDidScroll(scrollView: UIScrollView) {
        offsetY = scrollView.contentOffset.y
        //        if offsetY > 220-64*2 {
        let alpha = (offsetY-(220-64*2))/64
        self.wr_setNavBarBackgroundAlpha(alpha)
    }
    

}
class HomeDicViewControllerDelegate: BaseDicTableViewDelegate {
    var cellNum:Double!
    var slidingBlock:selectBlock!
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.slidingBlock()
    }
    
    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 arr=self.dataSource[String(indexPath.section)] as! [String:AnyObject]
//        let info:BaseCellModel=arr[String(indexPath.row)] as! BaseCellModel
        let array = self.dataSource[String(indexPath.section)] as! [String:AnyObject]
        let cellModel:BaseCellModel = array[String(indexPath.row)] as! BaseCellModel
        
        if cellModel.className == "HomeCarouselTableViewCell" {
            return (135/375)*SCREEN_WHIDTH()
        }else if cellModel.className == "MineDetailedTableViewCell"{
            let detailModel:MineDetailedTableViewCellModel = array[String(indexPath.row)] as! MineDetailedTableViewCellModel
            if detailModel.detaileArray.count >= Int(self.cellNum){
                let index = lroundf(Float(ceil(Double(detailModel.detaileArray.count)/self.cellNum)))
                return (SCREEN_WHIDTH()/4)*CGFloat(index)*0.8
            }
            return (SCREEN_WHIDTH()/4)*0.8
        }else if cellModel.className == "HeadlinesMoreTableViewCell"{
            return 25*HEIGHT_RATIO()
        }
        return 85*HEIGHT_RATIO()
    }
}
