//
//  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 arr = NSMutableArray()
    
    var companyNewsVC:AdvisoryViewController!
    var companyAnnouncementVC:AdvisoryViewController!
    var industryNewsVC:AdvisoryViewController!
    var screenDateArray:NSMutableArray!
    var timeDateArray:NSMutableArray!
    var heatDateArray:NSMutableArray!
    var timeHotPage = NSInteger()
   
    override func viewDidLoad() {
        super.viewDidLoad()
        initTitleBar()
        initReturnBtn()
        isNavigationBarObstructed()
        self.title = "资讯"
        self.view .addSubview(segment)
        setPageViewController()
        setPage()
        initPageController()
        setCurrentPage()
        // 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 leftN:UIBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: ""), style: .plain, target: self, action: #selector(arrowResponse))
        self.navigationItem.leftBarButtonItem = leftN
    }
    
    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.selectionIndicatorLocation = .down
        segment.titleTextAttributes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 17*WHIDTH_RATIO()),NSAttributedStringKey.foregroundColor:blackTitleColor]
        segment.selectedTitleTextAttributes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 17*WHIDTH_RATIO()),NSAttributedStringKey.foregroundColor:statusBarColor]
        segment.selectionIndicatorHeight = 3.0
        segment.type = .text
        segment.indexChangeBlock = {[weak self](index:NSInteger)->() in
            let index = self?.pages.index(of: self?.pageViewController.viewControllers?.last ?? NSInteger())
            let direction:UIPageViewControllerNavigationDirection = segment.selectedSegmentIndex > index! ? .forward:.reverse
            self?.pageViewController.setViewControllers([(self?.selectedController())!], direction: direction, animated: true, completion: nil)
        }
        return segment
    }()
    
    func selectedController() -> UIViewController {
        return self.pages[self.segment.selectedSegmentIndex] as! UIViewController
    }
    
    func setCurrentPage(){
        self.segment.setSelectedSegmentIndex(self.currentPage, animated: false)
        let index=self.pages.index(of: (self.pageViewController.viewControllers?.last)!)
        let direction:UIPageViewControllerNavigationDirection = self.segment.selectedSegmentIndex > index ? .forward:.reverse
        self.pageViewController.setViewControllers([self.selectedController()], direction: direction, animated: false, completion: nil)
        
    }
    func setPageViewController(){
        self.pageViewController = UIPageViewController.init(transitionStyle: .scroll,navigationOrientation: .horizontal,options: nil)
        self.pageViewController.view.frame = CGRect(x:0, y: segment.originY+segment.height, width:SCREEN_WHIDTH() , height: SCREEN_HEIGHT()-(segment.originY+segment.height)-2)
        self.pageViewController.delegate = self
        self.pageViewController.dataSource = self
        self.pageViewController.view.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        self.addChildViewController(self.pageViewController)
        self.view .addSubview(self.pageViewController.view)
        
    }
    
    func initPageController(){
        if self.pages.count > 0{
            self.pageViewController.setViewControllers([self.pages[0] as! UIViewController ], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
        }
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        var index = self.pages.index(of: viewController)
        if index == NSNotFound||index == 0 {
            return nil
        }
        index -= 1
        return self.pages[index] as? UIViewController
    }
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        var index = self.pages.index(of: viewController)
        if index == NSNotFound||index+1>=self.pages.count {
            self.timeHotPage=index-1
            return nil
        }
        index += 1
        return self.pages[index] as? UIViewController
    }
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if !completed {
            return
        }
        let index:UInt = UInt(self.pages.index(of: (pageViewController.viewControllers?.last)!))
        //if index==2{
        //self.setFilterSelected()
        //}else{
        self.segment.setSelectedSegmentIndex(index, animated: true)
        //self.setFilterUnselect()
        //}
    }
    func setPage(){
        pages = NSMutableArray()
        self.companyNewsVC = self.createHotPageViewControllerWithTitle(title: "公司新闻",sequenPageType: SequenPageType.CompanyNews)
        self.companyAnnouncementVC = self.createTimeWithTitle(title: "公司公告",sequenPageType: SequenPageType.CompanyAnnouncement)
        self.industryNewsVC = self.createScreenWithTitle(title: "行业动态",sequenPageType: SequenPageType.IndustryNews)
        pages.add(self.industryNewsVC)
        pages.add(self.companyNewsVC)
        pages.add(self.companyAnnouncementVC)
        
    }
    func createHotPageViewControllerWithTitle(title:String,sequenPageType:SequenPageType) -> AdvisoryViewController{
        let vc:AdvisoryViewController = AdvisoryViewController.createViewController(createArgs: nil) as! AdvisoryViewController
        vc.title = title
        vc.sequenPageType = sequenPageType
        return vc
    }
    func createTimeWithTitle(title:String,sequenPageType:SequenPageType) -> AdvisoryViewController{
        let vc:AdvisoryViewController = AdvisoryViewController.createViewController(createArgs: nil) as! AdvisoryViewController
        vc.title = title
        vc.sequenPageType = sequenPageType
        return vc
    }
    func createScreenWithTitle(title:String,sequenPageType:SequenPageType) -> AdvisoryViewController{
        let vc:AdvisoryViewController = AdvisoryViewController.createViewController(createArgs: nil) as! AdvisoryViewController
        vc.title = title
        vc.sequenPageType = sequenPageType
        return vc
    }
    
    
    func gotoFilterPage(){
        self.segment.setSelectedSegmentIndex(0, animated: false)
        let index=self.pages.index(of: (self.pageViewController.viewControllers?.last)!)
        let direction:UIPageViewControllerNavigationDirection = self.segment.selectedSegmentIndex > index ? .forward:.reverse
        
        let vc=self.pages[0] as! UIViewController
        self.pageViewController.setViewControllers([vc], direction: direction, animated: true, completion: nil)
        if index != 2{
            self.timeHotPage = index
        }
        
    }
    override func gotoMessageVC(){
        self.actionGotoMessageWeb(messageId: AccountInfo.sharedInstance().messageId!, isHomePushType: .YesIsHomePushType)
    }
    
    
}
