//
//  NW_HomeVC.swift
//  wallpaper_BProject
//
//  Created by 忆海16 on 2024/8/28.
//

import UIKit
import JXSegmentedView
import SnapKit
import FSPagerView

class NW_HomeVC: NW_RootVC {
    
    @IBOutlet weak var pagerView: FSPagerView!{
        didSet{
            self.pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "NW_PGBannerCell")
        }
    }
    //    var blocklive:(()->())?
    //    var blocknew:(()->())?
    //    var blockpopu:(()->())?
    lazy var viewcontrollers: [UIViewController] = { makeExampleViewControlers() }()
    var segmentedPageVC: SegmentedPageViewController!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setFSpageV()
        setpageVC()
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        self.navigationController?.navigationBar.isHidden = true
    }
    
    
    //    MARK: - banner设置
    func setFSpageV(){
        self.pagerView.delegate = self
        self.pagerView.dataSource = self
        pagerView.transformer = FSPagerViewTransformer(type: .linear)
        pagerView.automaticSlidingInterval = 2.0
        pagerView.isInfinite = true
        pagerView.decelerationDistance = 2
        pagerView.itemSize = CGSize(width: 240, height: 120)
        pagerView.interitemSpacing = 10
        
        //        pageControl.numberOfPages = 3 // 设置总页数
        //        pageControl.currentPage = 0
        //        pageControl.currentPageIndicatorTintColor = UIColor.white // 设置当前页的颜色
        //        pageControl.pageIndicatorTintColor = UIColor.clear
        
    }
    
    func setpageVC(){
        addSegmentedPageViewController()
        customizeSegmentedPageViewController2()
    }
    
    private func addSegmentedPageViewController() {
        
        segmentedPageVC = SegmentedPageViewController(
            pages: [
                .init(viewController: viewcontrollers[0],
                      icon: UIImage(named: "bg"),
                      selectedIcon: UIImage(named: "panda"),
                      title: ""),
                    
                
                
                .init(viewController: viewcontrollers[1],
                      icon: UIImage(named: "bg"),
                      selectedIcon: UIImage(named: "panda"),
                      title: ""),
                
                .init(viewController: viewcontrollers[2],
                      icon: UIImage(named: "bg"),
                      selectedIcon: UIImage(named: "panda"),
                      title: "")
            ]
            
            ,segmentControllerFillMode: .proportionally)
        
        
        self.addChild(segmentedPageVC)
        view.addSubview(segmentedPageVC.view)
        segmentedPageVC.view.frame = CGRect(x: 0, y: 250, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 250)
        segmentedPageVC.didMove(toParent: self)
        
    }
    private func makeExampleViewControlers() -> [UIViewController] {
        let vc1 = NW_JXOneVC()
//        vc1.labelTitle = "First View Controller"
//        vc1.view.backgroundColor = .systemYellow
        
        let vc2 = NW_JXTwoVC()
//        vc2.labelTitle = "Second View Controller"
//        vc2.view.backgroundColor = .systemRed
        
        let vc3 = NW_JXThreeVC()
//        vc3.labelTitle = "Third View Controller"
//        vc3.view.backgroundColor = .systemGreen
        
        return [vc1, vc2, vc3]
    }
    
}


extension NW_HomeVC {
    private func customizeSegmentedPageViewController1() {
        segmentedPageVC.segmentController.highlightColor = .systemRed
        segmentedPageVC.segmentController.underlineHeight = 20
        segmentedPageVC.segmentController.itemsHeight = 40
        segmentedPageVC.segmentController.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
        segmentedPageVC.segmentController.textColor = .systemGray6
//        segmentedPageVC.segmentController.underlineImage = UIImage(systemName: "triangle.fill")
    }
    
    
    private func customizeSegmentedPageViewController2() {
        segmentedPageVC.segmentController.highlightColor = .label
        segmentedPageVC.segmentController.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        segmentedPageVC.segmentController.textColor = .secondaryLabel
        segmentedPageVC.segmentController.underlineHeight = 10
        segmentedPageVC.segmentController.underlineImageViewCornerRadius = 5
        segmentedPageVC.segmentController.underlineImage = nil
    }
}


//    MARK: - banner代理
extension NW_HomeVC:FSPagerViewDelegate,FSPagerViewDataSource{
    public func numberOfItems(in pagerView: FSPagerView) -> Int {
        return 6
    }
    
    public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "NW_PGBannerCell", at: index)
        
        // 设置图片
        cell.imageView?.image = UIImage(named: "banner_\(index)")
        
        
        return cell
    }
    
    //    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
    //        print("Tapped item at index: \(index)")
    //
    ////        var viewControllerToPresent: UIViewController?
    //
    //        // 根据索引跳转到不同的视图控制器
    //        switch index {
    //        case 0:
    //            if self.blocklive != nil{
    //                self.blocklive!()
    //            }
    //        case 1:
    //            if self.blockpopu != nil{
    //                self.blockpopu!()
    //            }
    //        case 2:
    //            if self.blocknew != nil{
    //                self.blocknew!()
    //            }
    //        default:
    //            break
    //        }
    //    }
    
}
