//
//  WP_RankingVC.swift
//  WallpaperHD_Live
//
//  Created by 忆海16 on 2024/7/22.
//

import UIKit
import FSPagerView
import MJExtension
import SDWebImage

class WP_RankingVC: WP_RootVC {

    @IBOutlet weak var pagerView: FSPagerView!
    
    var wallpapers = [WP_4KModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setJsondata()
        setFSpageV()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    
    
    func setJsondata(){
        // 读取 JSON 文件并解析
        if let path = Bundle.main.path(forResource: "4k", ofType: "json") {
            do {
                let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
                
                // 使用 MJExtension 解析 JSON 数据
                let jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [[String: Any]]
                
                if let jsonArray = jsonArray {
                    // 使用 MJExtension 将 JSON 数组转换为模型数组
                     wallpapers = (WP_4KModel.mj_objectArray(withKeyValuesArray: jsonArray) as? [WP_4KModel])!
         
                }
            } catch {
                print("Error reading JSON file:", error.localizedDescription)
            }
        } else {
            print("JSON file not found.")
        }
    }
    
    func setFSpageV(){
        self.pagerView.delegate = self
        self.pagerView.dataSource = self
        pagerView.register(UINib(nibName: "WP_RankingCell", bundle: nil), forCellWithReuseIdentifier: "WP_RankingCell")
        pagerView.transformer = FSPagerViewTransformer(type: .overlap)
        pagerView.automaticSlidingInterval = 0
        pagerView.isInfinite = true
        pagerView.decelerationDistance = 1
        pagerView.itemSize = CGSize(width: UIScreen.main.bounds.size.width - 80, height: self.pagerView.frame.size.height)
//        pagerView.itemSize = pagerView.frame.size
        pagerView.interitemSpacing = 0

        
    }

}
extension WP_RankingVC:FSPagerViewDelegate,FSPagerViewDataSource{
    public func numberOfItems(in pagerView: FSPagerView) -> Int {
        return wallpapers.count
    }
    
    public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "WP_RankingCell", at: index) as! WP_RankingCell
        
        let model = self.wallpapers[index]
        if let imageURL = URL(string: model.thumbnail  ?? "") {
            cell.wallpaperImageV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
            
        }
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        let vc = WP_WallPaPerDetailsVC()
        vc.type = 1
        vc.model = self.wallpapers[index]
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}
