//
//  HomeViewController.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/11.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController,UIPopoverPresentationControllerDelegate {
    @objc
    
    var dataSource = NSMutableArray()
    var delegate:HomeViewControllerDelegate!
    var cellNum:Double = 4
    var offsetY : CGFloat!
    
    
    var bannerArray = NSMutableArray()
    var classArray = NSMutableArray()
    var headlinesTitleArray = NSMutableArray()
    var headlinesArray = NSMutableArray()
    
    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 searchTextField : SearchTextField = {
        let searchTextField = SearchTextField.init(frame: CGRect(x:90*WHIDTH_RATIO(), y: 7, width: SCREEN_WHIDTH()-(90+55)*WHIDTH_RATIO() , height:30 ))
        searchTextField.isEnabled = false
//        searchTextField.delegate = self
        return searchTextField;
    }()
    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)
//        
//    }
    override func viewDidLoad() {
        super.viewDidLoad()
        initTitleBar()
        initReturnBtn()
        addSearchBar()
        hideNavigationBar()
        addPositionView()
        addInitTableView()
        addHeaderFooter()
        actionHttpGCD()
        setStatusBarBackgroundColor()
        self.title = "首页"
        // Do any additional setup after loading the view.
    }
    //左侧返回按钮
    override func initReturnBtn(){
        let rightImageView:UIImageView = UIImageView.init(frame: CGRect(x:16, y: 8, width:20 , height:20 ))
        rightImageView.contentMode = .scaleAspectFit
        rightImageView.image = UIImage.init(named: "add_image")
        let rightBtn:UIButton = UIButton.init(type: .custom)
        rightBtn.frame = CGRect(x:0, y: 0, width:36 , height:36)
        rightBtn.contentMode = .scaleAspectFit
        rightBtn.addTarget(self, action: #selector(shouwPopover), for: .touchUpInside)
        //rightBtn.setBackgroundImage(UIImage.init(named: "add_image"), for: .normal)
        rightBtn.addSubview(rightImageView)
        //rightImageView.centerY = rightBtn.centerY
        // rightBtn.backgroundColor = UIColor.red
        let rightN:UIBarButtonItem = UIBarButtonItem.init(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = rightN
    }
 
    func addSearchBar(){
        self.navigationItem.titleView = searchTextField
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    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()
        }
    }
    override func gotoMessageVC(){
        self.actionGotoMessageWeb(messageId: AccountInfo.sharedInstance().messageId!, isHomePushType: .YesIsHomePushType)
    }
}
