//
//  WA_MonRankngCell.swift
//  wallpaper_project
//
//  Created by 忆海16 on 2024/3/28.
//

import UIKit

class WA_MonRankngCell: UITableViewCell {

    @IBOutlet weak var collectionView: UICollectionView!
    
    let cellWidth: CGFloat = 150
    let cellHeight: CGFloat = 300
    let cellSpacing: CGFloat = 10
    var TexturesWallpapers: [WallpaperData] = []
    var jpgblock:(()->())?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setcollectionV()
        settypeNetwork()
    }

    func setcollectionV(){
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.collectionViewLayout = layout
        collectionView.register(UINib(nibName: "WA_ToDayCollectCell", bundle: nil), forCellWithReuseIdentifier: "WA_ToDayCollectCell")
        
    }
    func settypeNetwork(){
        // 读取 JSON 文件路径
        guard let jsonFilePath = Bundle.main.path(forResource: "my_wallpaper", ofType: "json") else {
            fatalError("Unable to locate my_wallpaper.json file.")
        }
        
        // 读取 JSON 数据
        do {
            // 读取 JSON 数据
            let jsonData = try Data(contentsOf: URL(fileURLWithPath: jsonFilePath))
            
            // 尝试将 JSON 数据转换为 Swift 对象
            let decoder = JSONDecoder()
            let wallpaperModels = try decoder.decode([WallpaperModel].self, from: jsonData)
            
            // 遍历解析后的 WallpaperModel 对象数组
            for wallpaperModel in wallpaperModels {
                // 根据名称将数据放入不同的数组中
                switch wallpaperModel.name {
                case "High quality wallpapers":
                    TexturesWallpapers = wallpaperModel.data
                default:
                    break
                }
            }
            
        } catch {
            print("Error reading or parsing JSON file: \(error)")
        }
    }
    
}
extension WA_MonRankngCell: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WA_ToDayCollectCell", for: indexPath) as! WA_ToDayCollectCell
        cell.model = self.TexturesWallpapers[indexPath.row]
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: cellWidth, height: cellHeight)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return cellSpacing
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: cellSpacing, bottom: 0, right: cellSpacing)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let topViewController = UIApplication.getTopViewController() {
            // 创建您要跳转的视图控制器
            let vc = WA_DetailsVC()
            vc.type = 2
            vc.modeltype = self.TexturesWallpapers[indexPath.row]
            
            // 在主视图控制器中执行跳转
            topViewController.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
}
