//
//  DetailsViewController+initTableView.swift
//  MobilePortal
//
//  Created by caiyang on 2020/4/8.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension DetailsViewController {
    func addInitTableView() {
        initData()
        self.delegate = DetailsViewControllerDelegate()
        self.delegate.dataSource = self.dataSource
        self.myTableView.delegate = self.delegate
        self.myTableView.dataSource = self.delegate
        self.myTableView.reloadData()
//        myTableView.snp.makeConstraints { (make) in
//            make.width.height.equalToSuperview()
//            make.center.equalToSuperview()
//        }
    }
    func initData(){
        addTitleCell()
//        addRecommendTitleTableViewCell()
//
//        addRecommendTableViewCellModel()
//        addRecommendTableViewCellModel()
//        addRecommendTableViewCellModel()
//        addRecommendTableViewCellModel()
//        addRecommendTableViewCellModel()
//        addRecommendTableViewCellModel()
    }
    func addTitleCell(){
        let titleModel = DetailsTableViewCellModel()
        titleModel.className = "DetailsTableViewCell"
        titleModel.titleStr = judgmentStrNil(str: self.httpModel.title)
        titleModel.content = judgmentStrNil(str: self.httpModel.content)
//        titleModel.h = SCREEN_HEIGHT()-NAV_HEIGHT()-STATUBAR_HEIGHT()
        titleModel.h = self.getCountentH(str: titleModel.content!)
        self.dataSource.add(titleModel)
    }
    func getCountentH(str:String)->CGFloat {
        let h = SCREEN_HEIGHT()-NAV_HEIGHT()-STATUBAR_HEIGHT()
        let attrStr = try! NSMutableAttributedString(
            data: (str.data(using: .unicode, allowLossyConversion: true)!),
            options:[.documentType: NSAttributedString.DocumentType.html,
                     .characterEncoding: String.Encoding.utf8.rawValue],
            documentAttributes: nil)
        var contentHegiht = attrStr.boundingRect(with: CGSize(width:  SCREEN_WHIDTH()-10, height: 1000), options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil).height + 93
        if contentHegiht < h {
            contentHegiht = h
        }
        return contentHegiht
    }
    func addRecommendTitleTableViewCell(){
        let titleModel = RecommendTitleTableViewCellModel()
        titleModel.className = "RecommendTitleTableViewCell"
        titleModel.titleStr = "相关推荐"
        self.dataSource.add(titleModel)
    }
    func addRecommendTableViewCellModel(){
        let titleModel = RecommendTableViewCellModel()
        titleModel.className = "RecommendTableViewCell"
        titleModel.titleStr = "订单来源分类有哪些？"
        self.dataSource.add(titleModel)
    }
}
class DetailsViewControllerDelegate: BaseOneTableViewDelegate {
    var cellNum:Double!
    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 infoMode:AdvisoryTableViewCellModel = self.dataSource[indexPath.row] as! AdvisoryTableViewCellModel
//        return 53.33
        let tableCellModel:BaseCellModel = self.dataSource[indexPath.row] as! BaseCellModel
        if tableCellModel.className == "DetailsTableViewCell" {
            let model:DetailsTableViewCellModel = tableCellModel as! DetailsTableViewCellModel
            return model.h
        } else if tableCellModel.className == "RecommendTitleTableViewCell"{
            return 40*HEIGHT_RATIO()
        } else if tableCellModel.className == "RecommendTableViewCell"{
            return 40*HEIGHT_RATIO()
        }
        return 90
    }
}
