//
//  XcString.swift
//
//
//  Created by Quentin Bandera on 12/04/2024.
//

import SwiftUI

struct DynamicKey: CodingKey {
    var intValue: Int?
    init?(intValue: Int) {
        self.intValue = intValue
        self.stringValue = "\(intValue)"
    }

    var stringValue: String
    init?(stringValue: String) {
        self.stringValue = stringValue
    }
}

struct Root: Codable, Equatable {
    let sourceLanguage: String
    let strings: XCStringDefinitionContainer
    let version: String
}

struct XCStringDefinitionContainer: Codable, Equatable {
    let strings: [XCStringDefinition]

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)

        for str in strings {
            if let codingKey = DynamicKey(stringValue: str.title) {
                try container.encode(str.content, forKey: codingKey)
            }
        }
    }
}

struct XCStringDefinition: Codable, Equatable {
    let title: String // json key -> custom encoding methods
    let content: XCStringDefinitionContent
}

struct XCStringDefinitionContent: Codable, Equatable {
    let extractionState: String
    var localizations: XCStringLocalizationContainer

}

struct XCStringLocalizationContainer: Codable, Equatable {
    let localizations: [XCStringLocalization]

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: DynamicKey.self)

        for loca in localizations {
            if let codingKey = DynamicKey(stringValue: loca.lang) {
                try container.encode(loca.content, forKey: codingKey)
            }
        }
    }
}

struct XCStringLocalization: Codable, Equatable {
    let lang: String // json key -> custom encoding method
    let content: XCStringLocalizationLangContent
}

struct XCStringLocalizationLangContent: Codable, Equatable {
    let stringUnit: DefaultStringUnit
}

//enum VarationOrStringUnit: Encodable {
//    case variations([Varation])
//    case stringUnit: (DefaultStringUnit)
//
//    func encode(to encoder: any Encoder) throws {
//        if let varations {
//
//        } else if let  {
//
//        }
//    }
//}

struct DefaultStringUnit: Codable, Equatable {
    let state: String
    let value: String
}
