//
//  WP_PhotoAlbumVC.swift
//  WallpaperHD_Live
//
//  Created by 忆海16 on 2024/7/23.
//

import UIKit
import Photos

class WP_PhotoAlbumVC: WP_RootVC {
    @IBOutlet weak var collectionView: UICollectionView!
    
    @IBOutlet weak var dataView: UIView!
    var model = [WP_PhotoAlbum]()
    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: "WP_WallpaperCollectionCell", bundle: nil), forCellWithReuseIdentifier: "WP_WallpaperCollectionCell")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
        let retrievedModels = PreferencesManager.shared.retrieveRakModels()
       if retrievedModels != nil{
           self.model = retrievedModels!
       }else{
           
       }
        
        if self.model.count == 0{
            self.dataView.isHidden = false
        }else{
            self.dataView.isHidden = true
        }
    }
    
    @IBAction func add(_ sender: Any) {
        let imagePicker = UIImagePickerController()
        imagePicker.sourceType = .photoLibrary
        imagePicker.delegate = self
        present(imagePicker, animated: true, completion: nil)
        
    }
    
    
    @IBAction func back(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    
    
    // 将 UIImage 转换为字符串
    func convertImageToString(image: UIImage) -> String? {
        if let imageData = image.jpegData(compressionQuality: 1.0) {
            // 将图片数据转换为 Base64 编码的字符串
            let base64String = imageData.base64EncodedString()
            return base64String
        }
        return nil
    }
    
    // 将 Base64 字符串转换为图片
    func convertStringToImage(base64String: String) -> UIImage? {
        if let imageData = Data(base64Encoded: base64String) {
            if let image = UIImage(data: imageData) {
                return image
            }
        }
        return nil
    }
    
}
extension WP_PhotoAlbumVC:UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    
    // 当选择图片完成时调用的方法
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true, completion: nil)
        
        // 获取选定的图片
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            // 存储图片到 UserDefaults
            //            saveImageToUserDefaults(image, forKey: "selectedImage")
            let base64imgString = convertImageToString(image: image)
            let dataModel1 = WP_PhotoAlbum( img: base64imgString)
            PreferencesManager.shared.addRakModel(dataModel1)
            
            let retrievedModels = PreferencesManager.shared.retrieveRakModels()
           if retrievedModels != nil{
               self.model = retrievedModels!
               self.collectionView.reloadData()
           }else{
               
           }
            
            if self.model.count == 0{
                self.dataView.isHidden = false
            }else{
                self.dataView.isHidden = true
            }
            
        }
    }
    
    // 当用户取消选择图片时调用的方法
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
 
    }
    
    
}
extension WP_PhotoAlbumVC:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return model.count
        
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WP_WallpaperCollectionCell", for: indexPath)as!WP_WallpaperCollectionCell
        let data = self.model[indexPath.row]
        cell.wpImagV.image = convertStringToImage(base64String: data.img ?? "")
        return cell
    }
    
  
}
