//
Tests.swift
Tests
//
//  Created by Leon on 2025/7/11.
//

import XCTest
@testable import Visual_Novel_iOS

class CLSystemTokenTests: XCTestCase {
    
    override func setUp() {
        super.setUp()
        
    }
    
    override func tearDown() {
        super.tearDown()
        
    }

    // 测试有效颜色的获取
    func testColorForValidToken() {
        let expectedColor = UIColor(red: 1.0, green: 0.925, blue: 0.871, alpha: 1.0) // #FFECDE
        let color = CLSystemToken.color(token: .cph)
        
        XCTAssertNotNil(color, "Color should not be nil for valid token")
        
//        if let color = color {
//            XCTAssertEqual(color, expectedColor, accuracy: 0.01, "Color should match expected RGB values")
//        }
    }

    // 测试带透明度的颜色获取
    func testColorWithAlpha() {
        // 假设 skin.json 中有一个键返回 ["#FFECDE", "0.5"]
        let token = CLEnumSystemToken(rawValue: "color.orange.with.alpha")
        let expectedColor = UIColor(red: 1.0, green: 0.925, blue: 0.871, alpha: 0.5)
        
        // 模拟 EPUI 返回带透明度的值
        let mockValues = ["#FFECDE", "0.5"]
        // 假设 EPUI.getTokensByKey 返回 mockValues（需要 mock 或修改 skin.json）
        let color = EPBaseObject.subGetColorByTokenValues(mockValues)
        
        XCTAssertNotNil(color, "Color with alpha should not be nil")
        
        if let color = color {
            
            //XCTAssertEqual(color, expectedColor, accuracy: 0.01, "Color with alpha should match expected RGBA values")
            //XCTAssertEqual(color, expectedColor, accuracy: 0.01, "xxx")
        }
    }

    // 测试无效 token
//    func testColorForInvalidToken() {
//        let token = CLEnumSystemToken(rawValue: "invalid.color")!
//        let color = CLSystemToken.color(token: token)
//        
//        XCTAssertNil(color, "Color should be nil for invalid token")
//    }

    // 测试深色模式颜色获取
//    func testDarkColorForValidToken() {
//        let token = CLEnumSystemToken(rawValue: "color.orange.90")! // 深色模式示例
//        let expectedColor = UIColor(red: 0.302, green: 0.078, blue: 0.0, alpha: 1.0) // #4D1400
//        let color = CLSystemToken.darkColor(token: token)
//        
//        XCTAssertNotNil(color, "Dark color should not be nil for valid token")
//        
////        if let color = color {
////            XCTAssertEqual(color, expectedColor, accuracy: 0.01, "Dark color should match expected RGB values")
////        }
//    }

    // 测试空值情况
//    func testColorForEmptyValues() {
//        let emptyValues: [String] = []
//        let color = EPBaseObject.subGetColorByTokenValues(emptyValues)
//        
//        XCTAssertNil(color, "Color should be nil for empty values")
//    }
}

// 扩展 UIColor 以支持比较
extension UIColor {
    func isEqual(to color: UIColor, accuracy: CGFloat) -> Bool {
        var r1: CGFloat = 0, g1: CGFloat = 0, b1: CGFloat = 0, a1: CGFloat = 0
        var r2: CGFloat = 0, g2: CGFloat = 0, b2: CGFloat = 0, a2: CGFloat = 0
        
        self.getRed(&r1, green: &g1, blue: &b1, alpha: &a1)
        color.getRed(&r2, green: &g2, blue: &b2, alpha: &a2)
        
        return abs(r1 - r2) <= accuracy &&
               abs(g1 - g2) <= accuracy &&
               abs(b1 - b2) <= accuracy &&
               abs(a1 - a2) <= accuracy
    }
}
