//
//  EditStatusParameters.swift
//  Pachyderm
//
//  Created by Shadowfacts on 5/10/23.
//

import Foundation

struct EditStatusParameters: Encodable, Sendable {
    let id: String
    let text: String
    let contentType: StatusContentType
    let spoilerText: String?
    let sensitive: Bool
    let language: String?
    let mediaIDs: [String]
    let mediaAttributes: [EditStatusMediaAttributes]
    let poll: EditPollParameters?
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(self.id, forKey: .id)
        try container.encode(self.text, forKey: .text)
        try container.encode(self.contentType.mimeType, forKey: .contentType)
        try container.encodeIfPresent(self.spoilerText, forKey: .spoilerText)
        try container.encode(self.sensitive, forKey: .sensitive)
        try container.encodeIfPresent(self.language, forKey: .language)
        try container.encode(self.mediaIDs, forKey: .mediaIDs)
        try container.encode(self.mediaAttributes, forKey: .mediaAttributes)
        try container.encodeIfPresent(self.poll, forKey: .poll)
    }
    
    enum CodingKeys: String, CodingKey {
        case id
        case text = "status"
        case contentType = "content_type"
        case spoilerText = "spoiler_text"
        case sensitive
        case language
        case mediaIDs = "media_ids"
        case mediaAttributes = "media_attributes"
        case poll
    }
}

public struct EditPollParameters: Encodable, Sendable {
    let options: [String]
    let expiresIn: Int
    let multiple: Bool
    
    public init(options: [String], expiresIn: Int, multiple: Bool) {
        self.options = options
        self.expiresIn = expiresIn
        self.multiple = multiple
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(self.options, forKey: .options)
        try container.encode(self.expiresIn, forKey: .expiresIn)
        try container.encode(self.multiple, forKey: .multiple)
    }
    
    enum CodingKeys: String, CodingKey {
        case options
        case expiresIn = "expires_in"
        case multiple
    }
}

public struct EditStatusMediaAttributes: Encodable, Sendable {
    let id: String
    let description: String
    let focus: (Float, Float)?
    
    public init(id: String, description: String, focus: (Float, Float)?) {
        self.id = id
        self.description = description
        self.focus = focus
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(description, forKey: .description)
        if let focus {
            try container.encode("\(focus.0),\(focus.1)", forKey: .focus)
        }
    }
    
    enum CodingKeys: String, CodingKey {
        case id
        case description
        case focus
    }
}
