//
//  WA_SCVC.swift
//  wallpaper_project
//
//  Created by 忆海16 on 2024/3/29.
//

import UIKit

class WA_SCVC: WA_RootVC {
    @IBOutlet weak var dataView: UIView!
    
    @IBOutlet weak var collectionView: UICollectionView!
    var model = [WA_3DModel]()
    var modeltype = [WallpaperData]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setCollectionView()
    }
    
    func setCollectionView(){
        collectionView.delegate = self
        collectionView.dataSource = self
        
        // 设置 collection view 的布局
        let layout = CustomCollectionViewFlowLayout()
        collectionView.collectionViewLayout = layout
        layout.scrollDirection = .vertical
        
        collectionView.register(UINib(nibName: "WA_WallpaperCollectionCell", bundle: nil), forCellWithReuseIdentifier: "WA_WallpaperCollectionCell")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.isHidden = true
        self.model = sc4kManager.shared.history
        self.modeltype = scjpgManager.shared.history
        if self.model.count + self.modeltype.count  == 0{
            self.dataView.isHidden = false
        }else{
            self.dataView.isHidden = true
        }
    }
    
    @IBAction func backBtn(_ sender: Any) {
        navigationController?.popViewController(animated: true)
    }
    
}
extension WA_SCVC:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return self.model.count + self.modeltype.count
        
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WA_WallpaperCollectionCell", for: indexPath)as!WA_WallpaperCollectionCell
        //        if
        
        if indexPath.item < self.model.count {
            cell.model = self.model[indexPath.item]
        }else{
            let newIndex = indexPath.item - self.model.count
            if newIndex < self.modeltype.count {
                let model = self.modeltype[newIndex]
                if let imageURL = URL(string: model.source ?? "") {
                    //                wpImagV.sd_setImage(with: imageURL, completed: nil)
                    cell.wpImagV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
                }
            }
        }
        
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = WA_DetailsVC()
        if indexPath.item < self.model.count {
            vc.model = self.model[indexPath.item]
            vc.type = 1
            navigationController?.pushViewController(vc, animated: true)
        }else{
            let newIndex = indexPath.item - self.model.count
            if newIndex < self.modeltype.count {
                vc.type = 2
                vc.modeltype = self.modeltype[newIndex]
                navigationController?.pushViewController(vc, animated: true)
            }
        }
        
        
        
    }
}
