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

import UIKit

class HomeDicViewController: BaseViewController {

    var dataSource: [String:AnyObject] = [:]
    var delegate:HomeDicViewControllerDelegate!
    var cellNum:Double = 4
    var offsetY:CGFloat = 0
    
    
    var bannerArray: [String:AnyObject] = [:]
    var classArray: [String:AnyObject] = [:]
    var headlinesTitleArray: [String:AnyObject] = [:]
    var headlinesArray: [String:AnyObject] = [:]
    
    var carouseCellModel = HomeCarouselTableViewCellModel()
    var detailedModel = MineDetailedTableViewCellModel()
    var detailModel = MineCollectionViewCellModel()
    var headlinesMoreCellModel = HeadlinesMoreTableViewCellModel()
    
    //顶部刷新
    var header = MJRefreshNormalHeader()
    //底部刷新
    var footer = MJRefreshAutoNormalFooter()
    var page = 0
    var httpInfoModel = HomeHttpInfoModel()
    var placeholderString:String!
    
    
    lazy var myTableView: UITableView = {
        let myTableView = UITableView.init()
        self.view.addSubview(myTableView)
        myTableView.snp.makeConstraints { (make) in
            make.width.height.equalToSuperview()
            make.center.equalToSuperview()
        }
        if #available(iOS 11.0, *) {
            myTableView.contentInsetAdjustmentBehavior = .never
            myTableView.scrollIndicatorInsets = myTableView.contentInset
            myTableView.contentInset = UIEdgeInsetsMake(UIApplication.shared.statusBarFrame.size.height, 0, 0, 0)
        }else{
            myTableView.contentInset = UIEdgeInsetsMake(-44, 0, 0, 0)
        }
        registerCell(myTableView, cell: HomeCarouselTableViewCell.self)
        registerCell(myTableView, cell: MineDetailedTableViewCell.self)
        registerCell(myTableView, cell: AdvisoryTableViewCell.self)
        registerCell(myTableView, cell: HeadlinesMoreTableViewCell.self)
        myTableView.backgroundColor = loginBg_Color
        setExtraCellLineHidden(tableView: myTableView)
        return myTableView
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        hideNavigationBar()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        initTitleBar()
        addInitTableView()
        addHeaderFooter()
        actionHttpGCD()
        setStatusBarBackgroundColor()
        // Do any additional setup after loading the view.
    }
    func addHeaderFooter(){
        self.header.setRefreshingTarget(self, refreshingAction: #selector(upPullLoadData))
        self.header.lastUpdatedTimeLabel.isHidden = true
        self.header.stateLabel.isHidden = true
        self.footer.setRefreshingTarget(self, refreshingAction: #selector(downPlullLoadData))
        self.footer.stateLabel.isHidden = false
        self.footer.isRefreshingTitleHidden = true
        self.myTableView.mj_header = self.header
        self.myTableView.mj_footer = self.footer
        self.footer.setTitle("", for: .idle)
    }
    
    //MARK:下拉刷新
    @objc func upPullLoadData(){
        actionHttpGCD()
        
    }
    //MARK:上拉加载
    @objc func downPlullLoadData(){
        self.myTableView.mj_footer.endRefreshing()
        self.footer.setTitle("已经到底啦", for: .idle)
    }
    
    func actionHttpGCD(){
        showHintView()
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getHttpHomeClassListCmd()
        })
        group.notify(queue: DispatchQueue.main) {[weak self] in
            self?.delegate.dataSource = (self?.dataSource)!
            self?.myTableView.delegate = self?.delegate
            self?.myTableView.dataSource = self?.delegate
            self?.myTableView.reloadData()
            self?.myTableView.mj_header.endRefreshing()
            self?.hidenHintView()
        }
    }

}
