//
//  ImageExtensionGeneratorTests.swift
//  
//
//  Created by Thibaut Schmitt on 05/09/2022.
//

import Foundation
import XCTest
import ToolCore

@testable import ResgenSwift

final class ImageExtensionGeneratorTests: XCTestCase {
    
    func test_uiKit_GeneratedExtensionContent() {
        // Given
        let images = [
            ParsedImage(name: "image_one", tags: "id", width: 10, height: 10),
            ParsedImage(name: "image_two", tags: "id", width: 180, height: 90),
        ]
        
        // When
        let extensionContent = ImageExtensionGenerator.getExtensionContent(
            images: images,
            isStatic: false,
            extensionName: "GenImages",
            inputFilename: "myInputFilename",
            isSwiftUI: false,
            visibility: .public,
            assetBundle: .main
        )
        
        // Expect
        let expect = """
        // Generated by ResgenSwift.Images \(ResgenSwiftVersion)
        // Images from myInputFilename
        
        import UIKit
        
        extension GenImages {
        
            public var image_one: UIImage {
                UIImage(named: "image_one", in: Bundle.main, with: nil)!
            }
        
            public var image_two: UIImage {
                UIImage(named: "image_two", in: Bundle.main, with: nil)!
            }
        }
        """
        
        XCTAssertEqual(extensionContent.adaptForXCTest(), expect.adaptForXCTest())
    }
    
    func test_uiKit_GeneratedExtensionContentForIsStatic() {
        // Given
        let images = [
            ParsedImage(name: "image_one", tags: "id", width: 10, height: 10),
            ParsedImage(name: "image_two", tags: "id", width: 180, height: 90),
        ]
        
        // When
        let extensionContent = ImageExtensionGenerator.getExtensionContent(
            images: images,
            isStatic: true,
            extensionName: "GenImages",
            inputFilename: "myInputFilename",
            isSwiftUI: false,
            visibility: .internal,
            assetBundle: .module
        )
        
        // Expect
        let expect = """
        // Generated by ResgenSwift.Images \(ResgenSwiftVersion)
        // Images from myInputFilename
        
        import UIKit
        
        extension GenImages {
        
            internal static var image_one: UIImage {
                UIImage(named: "image_one", in: Bundle.module, with: nil)!
            }
        
            internal static var image_two: UIImage {
                UIImage(named: "image_two", in: Bundle.module, with: nil)!
            }
        }
        """
        
        XCTAssertEqual(extensionContent.adaptForXCTest(), expect.adaptForXCTest())
    }
    
    func test_swiftUI_GeneratedExtensionContent() {
        // Given
        let images = [
            ParsedImage(name: "image_one", tags: "id", width: 10, height: 10),
            ParsedImage(name: "image_two", tags: "id", width: 180, height: 90),
        ]
        
        // When
        let extensionContent = ImageExtensionGenerator.getExtensionContent(
            images: images,
            isStatic: false,
            extensionName: "GenImages",
            inputFilename: "myInputFilename",
            isSwiftUI: true,
            visibility: .public,
            assetBundle: .main
        )
        
        // Expect
        let expect = """
        // Generated by ResgenSwift.Images \(ResgenSwiftVersion)
        // Images from myInputFilename
        
        import SwiftUI
        
        extension GenImages {
        
            public var image_one: Image {
                Image("image_one", bundle: Bundle.main)
            }
        
            public var image_two: Image {
                Image("image_two", bundle: Bundle.main)
            }
        }
        """
        
        XCTAssertEqual(extensionContent.adaptForXCTest(), expect.adaptForXCTest())
    }
    
    func test_swiftUI_GeneratedExtensionContentForIsStatic() {
        // Given
        let images = [
            ParsedImage(name: "image_one", tags: "id", width: 10, height: 10),
            ParsedImage(name: "image_two", tags: "id", width: 180, height: 90),
        ]
        
        // When
        let extensionContent = ImageExtensionGenerator.getExtensionContent(
            images: images,
            isStatic: true,
            extensionName: "GenImages",
            inputFilename: "myInputFilename",
            isSwiftUI: true,
            visibility: .package,
            assetBundle: .module
        )
        
        // Expect
        let expect = """
        // Generated by ResgenSwift.Images \(ResgenSwiftVersion)
        // Images from myInputFilename
        
        import SwiftUI
        
        extension GenImages {
        
            package static var image_one: Image {
                Image("image_one", bundle: Bundle.module)
            }
        
            package static var image_two: Image {
                Image("image_two", bundle: Bundle.module)
            }
        }
        """
        
        XCTAssertEqual(extensionContent.adaptForXCTest(), expect.adaptForXCTest())
    }
}
