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

import UIKit

class MineViewController: BaseViewController,UIPopoverPresentationControllerDelegate {
    
    var delegate:MineViewControllerDelegate!
    var dataSource:NSMutableArray!
    var cellNum:Double = 4
    var companyLabel:UILabel!
    var linView:UIView!
    var offsetY : CGFloat!
    
    var headArray = NSMutableArray()
    var headCellModel = MineHeadTableViewCellModel()
    var inforHttpModel = MyInformationHttpModel()
    
    var myClassArray = NSMutableArray()
    var oftenClassArray = NSMutableArray()
    //底部刷新
    var footer = MJRefreshAutoNormalFooter()
    //顶部刷新
    var header = MJRefreshNormalHeader()
    
    var companyListModelArray = NSMutableArray()
    var departmentListModelArray = NSMutableArray()
    var selecClassModel:AdvisoryHttpListModel!
    
    lazy var loginView : LoginView = {
        let loginView = LoginView.init(frame: self.view.frame)
        return loginView;
    }()

    
    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()
        if #available(iOS 11.0, *) {
            myTableView.contentInsetAdjustmentBehavior = .never
            myTableView.scrollIndicatorInsets = myTableView.contentInset
            myTableView.contentInset = UIEdgeInsetsMake(UIApplication.shared.statusBarFrame.size.height, 0, 20, 0)
        }else{
            myTableView.contentInset = UIEdgeInsetsMake(-44, 0, 0, 0)
        }
        registerCell(myTableView, cell: MineTitleTableViewCell.self)
        registerCell(myTableView, cell: MineDetailedTableViewCell.self)
        registerCell(myTableView, cell: MineHeadTableViewCell.self)
        myTableView.backgroundColor = loginBg_Color
        myTableView.separatorStyle = .none
        setExtraCellLineHidden(tableView: myTableView)
        return myTableView
    }()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        initTitleBar()
        hideNavigationBar()
        initIsLogin()
        
        // Do any additional setup after loading the view.
    }
    //左侧返回按钮
    override func initReturnBtn(){
        let leftN:UIBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: ""), style: .plain, target: self, action: #selector(arrowResponse))
        self.navigationItem.leftBarButtonItem = leftN
        self.navigationItem.rightBarButtonItem = leftN
    }
    //左侧返回按钮
    func initAddBtn(){
        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(arrowResponse), for: .touchUpInside)
        rightBtn.addSubview(rightImageView)
        let rightN:UIBarButtonItem = UIBarButtonItem.init(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = rightN
    }
    func addSearchBar(){
        self.navigationItem.titleView = searchTextField
    }
    override func arrowResponse(){
        shouwPopover()
    }
    func gotoMineInfoVC(){
        let vc:MineInfoViewController=MineInfoViewController()
        vc.signOutBlock = {[weak self] in
            self?.loginAction()
        }
        vc.hidesBottomBarWhenPushed = true
        self.pushViewController(viewController: vc, animated: true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //MARK:判断是否登录
    func initIsLogin(){
        if AccountInfo.sharedInstance().UserId == nil {
            loginAction()
        }else{
            notLoginAction()
        }
        
    }
    //页面清除
    func removeView(){
        loginView.isHidden = true
        //companyLabel.isHidden = true
        //linView.isHidden = true
        self.myTableView.isHidden = false
        //self.navigationItem.leftBarButtonItem = nil
        //self.navigationItem.rightBarButtonItem = nil
    }
    //MARK:登陆
    func loginAction(){
        searchTextField.isHidden = true
        self.myTableView.isHidden = true
        initReturnBtn()
        addInitLoginView()
        setStatusBarBackgroundColor()
    }
    func notLoginAction(){
        searchTextField.isHidden = false
        loginView.isHidden = true
        self.myTableView.isHidden = false
        initAddBtn()
        addSearchBar()
        addPositionView()
        addInitTableView()
        addHeaderFooter()
        actionHttpGCD()
        setStatusBarBackgroundColor()
    }
    
    //MARK:添加上拉加载 下拉刷新
    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 = true
        self.footer.isRefreshingTitleHidden = true
        self.myTableView.mj_header = self.header
        //self.myTableView.mj_footer = self.footer
    }
    
    //MARK:下拉刷新
    @objc func upPullLoadData(){
        
        actionHttpGCD()
        //self.myTableView.reloadData()
    }
    //MARK:上拉加载
    @objc func downPlullLoadData(){
        
    }
    func actionHttpGCD(){
        showHintView()
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getMyInformationHttpGetCmd()
//            print("aaaaaaa\(Thread.current)")
        })
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getMyApplicationsHttpCmdTwo()
//            print("aaaaaaa\(Thread.current)")
        })
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getMyApplicationsHttpCmd()
//            print("aaaaaaa\(Thread.current)")
        })
        group.notify(queue: DispatchQueue.main) {[weak self] in
//            print("aaaaaaa\(Thread.current)")
            self?.dataSource = NSMutableArray()
            self?.dataSource.add(self?.headArray ?? NSMutableArray())
            self?.dataSource.add(self?.oftenClassArray ?? NSMutableArray())
            self?.dataSource.add(self?.myClassArray ?? NSMutableArray())
            self?.myTableView.mj_header.endRefreshing()
            self?.delegate.dataSource = self?.dataSource
            self?.myTableView.delegate = self?.delegate
            self?.myTableView.dataSource = self?.delegate
            self?.myTableView.reloadData()
            self?.hidenHintView()
        }
    }
    override func gotoMessageVC(){
        self.actionGotoMessageWeb(messageId: AccountInfo.sharedInstance().messageId!, isHomePushType: .YesIsHomePushType)
    }
}
