//
//  UIAccessibilityTraits+Codable.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 6/16/23.
//  Copyright © 2023 Verizon Wireless. All rights reserved.
//

import Foundation

extension UIAccessibilityTraits: Codable {
    private static func trait(from string: String) throws -> UIAccessibilityTraits {
        switch string {
        case "none":
            return .none
        case "button":
            return .button
        case "link":
            return .link
        case "image":
            return .image
        case "searchField":
            return .searchField
        case "keyboardKey":
            return .keyboardKey
        case "staticText":
            return .staticText
        case "header":
            return .header
        case "tabBar":
            return .tabBar
        case "summaryElement":
            return .summaryElement
        case "selected":
            return .selected
        case "notEnabled":
            return .notEnabled
        case "adjustable":
            return .adjustable
        case "allowsDirectInteraction":
            return .allowsDirectInteraction
        case "updatesFrequently":
            return .updatesFrequently
        case "causesPageTurn":
            return .causesPageTurn
        case "playsSound":
            return .playsSound
        case "startsMediaSession":
            return .startsMediaSession
        default:
            throw ModelRegistry.Error.decoderOther(message: "Unsupported accessibility trait: \(string)")
        }
    }
    
    public init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        
        // Iterate and decode each.
        var accessibilityTrait: UIAccessibilityTraits = []
        while !container.isAtEnd {
            let traitString = try container.decode(String.self)
            let trait = try UIAccessibilityTraits.trait(from: traitString)
            accessibilityTrait.insert(trait)
        }
        self = accessibilityTrait
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        if self.contains(.none) {
            try container.encode("none")
        }
        if self.contains(.button) {
            try container.encode("button")
        }
        if self.contains(.link) {
            try container.encode("link")
        }
        if self.contains(.image) {
            try container.encode("image")
        }
        if self.contains(.searchField) {
            try container.encode("searchField")
        }
        if self.contains(.keyboardKey) {
            try container.encode("keyboardKey")
        }
        if self.contains(.staticText) {
            try container.encode("staticText")
        }
        if self.contains(.header) {
            try container.encode("header")
        }
        if self.contains(.tabBar) {
            try container.encode("tabBar")
        }
        if self.contains(.summaryElement) {
            try container.encode("summaryElement")
        }
        if self.contains(.selected) {
            try container.encode("selected")
        }
        if self.contains(.notEnabled) {
            try container.encode("notEnabled")
        }
        if self.contains(.adjustable) {
            try container.encode("adjustable")
        }
        if self.contains(.allowsDirectInteraction) {
            try container.encode("allowsDirectInteraction")
        }
        if self.contains(.updatesFrequently) {
            try container.encode("updatesFrequently")
        }
        if self.contains(.causesPageTurn) {
            try container.encode("causesPageTurn")
        }
        if self.contains(.playsSound) {
            try container.encode("playsSound")
        }
        if self.contains(.startsMediaSession) {
            try container.encode("startsMediaSession")
        }
    }
}
