//
//  WA_RankVC.swift
//  wallpaper_project


import UIKit
import MJExtension

class WA_RankVC: WA_RootVC {
    
    @IBOutlet weak var collectionView: UICollectionView!
    var wallpapers = [WA_3DModel]()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let layout = CustomLayout()
        collectionView.collectionViewLayout =  layout
        collectionView.backgroundColor = .white
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(UINib(nibName: "WA_RankCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "WA_RankCollectionViewCell")
        setJsondata()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    func setJsondata(){
        // 读取 JSON 文件并解析
        if let path = Bundle.main.path(forResource: "rank", 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 数组转换为模型数组
                    
                   let wallpapersarr = (WA_3DModel.mj_objectArray(withKeyValuesArray: jsonArray) as? [WA_3DModel])!
                    self.wallpapers = wallpapersarr
                    
                }
            } catch {
                print("Error reading JSON file:", error.localizedDescription)
            }
        } else {
            print("JSON file not found.")
        }

    }
    
}


extension WA_RankVC:UICollectionViewDataSource, UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return wallpapers.count // 你可以设置你的图片数量
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WA_RankCollectionViewCell", for: indexPath) as! WA_RankCollectionViewCell

        cell.model = self.wallpapers[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = WA_LiveVideoVC()
        vc.model = self.wallpapers[indexPath.row]
        navigationController?.pushViewController(vc, animated: true)
        
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
           guard let collectionView = collectionView else { return }
           
           let itemHeight = collectionView.bounds.height * 2.5 / 3
           let currentOffset = scrollView.contentOffset.y
           let targetOffset = targetContentOffset.pointee.y
           
           let currentItemIndex = round(currentOffset / itemHeight)
           let targetItemIndex = targetOffset > currentOffset ? currentItemIndex + 1 : currentItemIndex - 1
           
           let newOffsetY = targetItemIndex * itemHeight
           targetContentOffset.pointee.y = newOffsetY
        
       }
    
}
