//
//  CustomerServiceRootVC+segment.swift
//  MobilePortal
//
//  Created by caiyang on 2020/4/7.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension CustomerServiceRootVC {
    func selectedController() -> UIViewController {
        return self.pages[self.segment.selectedSegmentIndex] as! UIViewController
    }
    
    func setCurrentPage(){
        if self.pages.count == 0 {
            SMToastView.showMessage("暂无数据！")
            segment.isHidden = true
            return
        }
        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)-contactBtnView.height)
        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(){
        self.pageViewController.view.frame = CGRect(x:0, y: segment.originY+segment.height, width:SCREEN_WHIDTH() , height: SCREEN_HEIGHT()-(segment.originY+segment.height)-contactBtnView.height)
        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 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
        }
    }
}
