//
//  SessionUploadImage.swift
//  Sailassist
//
//  Created by 三浦薫巳 on 2023/11/23.
//

import Foundation
import SwiftUI

class SessionUploadImage : ObservableObject {
    @Published var status = false
    // シングルトン宣言
    static let OnlyOne = SessionUploadImage()

    private var serverSession = ServerSession()
    private var Calling : Bool = false      // 通信中

    /**
     * メッセージ
     */
    func RequestUploadImage(_ uploadImage : ReqUploadImage ,completion: @escaping ((Result<Data, APIError>)) -> Void) {
        print(debug: "calld")
        if Calling {
            return
        }
        Calling = true

        // リクエストURLの組み立て
        let url_string : String = HttpRequestType.UploadImage.rawValue
        guard let req_url = URL(string : url_string) else {
            Calling = false
            return
        }

//        let boundary = "----------" + UUID().uuidString
        let boundary = "----WebKitFormBoundaryZLdHZy8HNaBmUX0d"
        if let postdata = serverSession.httpBody(boundary: boundary, uploadImage) {
            serverSession.postForm(boundary: boundary, req_url, postdata, completion: completion)
        }
        else {
            Calling = false
            return
        }
    }
}
