//
//  PhotoBrowserModel.swift

//
//  Created by Leon on 2025/7/26.
//

import UIKit

class PhotoBrowserModel:NSObject {
    var image: UIImage?
    var imageContentMode: UIView.ContentMode = .scaleAspectFill
    var imageUrl: String?
    var placeHolder: UIImage?
    var sourceRect: CGRect = .zero
    var tagInfo: Any?
    
    var deleteTapBlock: ((PhotoBrowserModel, @escaping (Bool) -> Void) -> Void)?
    var likeTapBlock: ((PhotoBrowserModel, @escaping (Bool) -> Void) -> Void)?
    
    // MARK:  🚩 一些扩展字段
    // 相册处的模型
    var aiAlbum: AlbumPhotoItem!
    /// aiId
    var aiId: Int?
    var chatBackground: IMChatBackground?
    // IM中
    var sessionModel: SessionBaseModel?
    
    override init() {
        sourceRect = .zero
        imageContentMode = .scaleAspectFill
    }
    
    var computedSourceRect: CGRect {
        if sourceRect == .zero {
            if let image = image {
                let width = image.size.width
                let height = image.size.height
                return CGRect(
                    x: UIScreen.main.bounds.width * 0.5 - width * 0.5,
                    y: UIScreen.main.bounds.height * 0.5 - height * 0.5,
                    width: width,
                    height: height
                )
            } else {
                return CGRect(
                    x: UIScreen.main.bounds.width * 0.5 - 40,
                    y: UIScreen.main.bounds.height * 0.5 - 40,
                    width: 80,
                    height: 80
                )
            }
        }
        return sourceRect
    }
    
    static func getViewRectForScreen(with view: UIView) -> CGRect {
        guard let window = UIApplication.shared.windows.first else { return .zero }
        return view.superview?.convert(view.frame, to: window) ?? .zero
    }
    
    deinit {
        print("♻️EGPhotoBrowserModel dealloc")
    }
}
