//
//  CenterTiledImage.swift
//  Feels (iOS)
//
//  Created by Trey Tartt on 2/13/22.
//

import SwiftUI

struct CenterTiledImage: View {
    let imageName: String
    let imageSize: CGSize
    
    var body: some View {
        GeometryReader { geoReader in
            let horizontalTilesNeeded = ceil(geoReader.size.width / imageSize.width / 2) * 2 + 1
            let verticalTilesNeeded = ceil(geoReader.size.height / imageSize.height / 2) * 2 + 1
            
            Image(imageName)
                .resizable(resizingMode: .tile)
                .frame(
                    width: horizontalTilesNeeded * imageSize.width,
                    height: verticalTilesNeeded * imageSize.height
                )
                .position(x: geoReader.size.width * 0.5, y: geoReader.size.height * 0.5)
        }
    }
    
    init?(imageName: String) {
        guard let imageSize = UIImage(named: imageName)?.size else {
            return nil
        }
        
        self.imageName = imageName
        self.imageSize = imageSize
    }
}
