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

import UIKit

class AdvisoryRootViewController: BaseViewController,UIPageViewControllerDelegate,UIPageViewControllerDataSource {
    var pages = NSMutableArray()
    var pageViewController:UIPageViewController!
    var currentPage:UInt = 0
    var index = 0
    
    var timeHotPage = NSInteger()
    var sectionTitles = NSMutableArray()
    var httpArray = NSMutableArray()
    
    var companyListModelArray = NSMutableArray()
    var departmentListModelArray = NSMutableArray()
//    var selecClassModel:AdvisoryHttpListModel!
    var selecClassModel:ApplicationHttpListModel!
    
    lazy var tixingImage: UIImageView = {
        let tixingImage = UIImageView.init()
        tixingImage.frame = CGRect(x:0, y: 0, width:SCREEN_WHIDTH() , height:SCREEN_HEIGHT() )
        tixingImage.image = UIImage.init(named: "upcoming_image")
        tixingImage.contentMode = .scaleAspectFit
        tixingImage.isHidden = true
        return tixingImage
    }()
    lazy var tixingLabel: UILabel = {
        let tixingLabel = UILabel.init()
        tixingLabel.textAlignment = .center
        tixingLabel.font = UIFont.systemFont(ofSize: 17)
        tixingLabel.text = "暂无数据"
        tixingLabel.numberOfLines = 0
        tixingLabel.isHidden = true
        tixingLabel.textColor = UIColorFromRGB(rgbValue: 0x23262A)
        return tixingLabel
    }()
    
    lazy var segment:HMSegmentedControl = {
        let segment = HMSegmentedControl.init(frame: CGRect(x:0, y: NAV_HEIGHT()+STATUBAR_HEIGHT(), width:SCREEN_WHIDTH() , height:46 ))
        segment.selectionIndicatorLocation = .none
        segment.selectionStyle = .textWidthStripe
        
        //        segment.sectionTitles = ["行业动态","公司新闻","公司公告"]
        segment.selectionIndicatorColor = statusBarColor
        //        segment.selectionStyle = .fullWidthStripe
        //        segment.isUserDraggable = false
        segment.selectionIndicatorLocation = .down
        segment.titleTextAttributes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 12),NSAttributedStringKey.foregroundColor:UIColorFromRGB(rgbValue: 0x707985)]
        segment.selectedTitleTextAttributes = [NSAttributedStringKey.font:UIFont.init(name: "Helvetica-Bold", size: 15)!,NSAttributedStringKey.foregroundColor:UIColorFromRGB(rgbValue: 0x3D444F)]
        segment.selectionIndicatorHeight = 3.5
        segment.type = .text
        return segment
    }()
    
    lazy var segmentedPageViewController: HGSegmentedPageViewController = {
        let segmentedPageViewController = HGSegmentedPageViewController()
        segmentedPageViewController.categoryView.originalIndex = 0
        segmentedPageViewController.pageViewControllers = (self.pages as! [UIViewController])
        segmentedPageViewController.categoryView.titles = (sectionTitles as! [String])
        segmentedPageViewController.categoryView.titleNomalFont = UIFont.systemFont(ofSize: 14)
        segmentedPageViewController.categoryView.titleNormalColor = UIColorFromRGB(rgbValue: 0x707985)
        segmentedPageViewController.categoryView.titleSelectedFont = UIFont.init(name: "Helvetica-Bold", size: 16)
        segmentedPageViewController.categoryView.titleSelectedColor = UIColorFromRGB(rgbValue: 0x3D444F)
        segmentedPageViewController.categoryView.separator.isHidden = true
        return segmentedPageViewController
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
//        if #available(iOS 13.0, *) {
//            if SCREEN_HEIGHT() >= 812 {
                self.wr_setNavBarShadowImageHidden(true)
//            }
//        } else {
//            self.wr_setNavBarShadowImageHidden(true)
//        }
//        addNotificationCenter()
        self.appViewsSstatistics(title: "资讯", scope: AppClickType.OftenAppType.rawValue)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
//        NotificationCenter.default.removeObserver(self)
//        addNotificationCenter()
        addRemoveObserver()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        initTitleBar()
        self.edgesForExtendedLayout = UIRectEdge.bottom
//        addTitleView()
        initReturnBtn()
//        self.title = "资讯"
//        self.view .addSubview(segment)
//        setPageViewController()
        if AccountInfo.sharedInstance().oldType == "0" {
            actionOldGCD()
        } else {
            actionHttpGCD()
        }
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   
    //左侧返回按钮
    override func initReturnBtn(){
        let titleBtn:UIButton = UIButton.init(type: .custom)
        titleBtn.frame = CGRect(x:0, y: 0, width:36 , height:36)
        titleBtn.contentMode = .scaleAspectFit
        titleBtn.setTitle("资讯", for: .normal)
        titleBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20.67)
        titleBtn.titleLabel?.font = UIFont.init(name: "Helvetica-Bold", size: 20.67)
        titleBtn.setTitleColor(UIColorFromRGB(rgbValue: 0x23262A), for: .normal)
        let messageN:UIBarButtonItem = UIBarButtonItem.init(customView: titleBtn)
//        let leftN:UIBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: ""), style: .plain, target: self, action: #selector(arrowResponse))
        self.navigationItem.leftBarButtonItem = messageN
    }
    
    
    
    
    func actionHttpGCD(){
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getScopeListHttpCmd()
        })
        group.notify(queue: DispatchQueue.main) {[weak self] in
            self?.setHttpData()
//            self?.initPageController()
//            self?.setCurrentPage()
        }
    }
    func actionOldGCD(){
        pages = NSMutableArray()
        httpArray = ["公司新闻","公司公告","行业动态"]
        let type = ["1","2","3"]
        for index in 0..<httpArray.count {
            sectionTitles.add(httpArray[index])
            pages.add(self.createHotPageVC(type: type[index], title: httpArray[index] as! String))
        }
        self.setSegment()
        self.initPageController()
        self.setCurrentPage()
    }

    override func gotoMessageVC(){
        self.actionGotoMessageWeb(messageId: AccountInfo.sharedInstance().messageId!, isHomePushType: .YesIsHomePushType)
    }
    override func gotoIntelligentCustomerService(){
        self.getApplicationByBelongAction(applicationName: "IT_ROBOT")
    }
    /// 应用访问量
    func appViewsSstatistics(title:String,scope:String){
        let model = ClickRateModel()
        model.name = title
        model.belong = "门户"
        model.scope = scope
        model.type = "1"
        self.getClickRateHttpCmd(infoModel: model)
    }
}
