//
//  SessionShipStatus.swift
//  Sailassist
//
//  Created by 三浦薫巳 on 2023/11/14.
//

import Foundation
import SwiftUI

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

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

    /**
     * 船情報
     */
    func RequestShipStatus(_ completion: @escaping ((Result<Data, APIError>)) -> Void) {
        print(debug: "calld")
        if Calling {
            return
        }
        Calling = true

        // リクエストURLの組み立て
        let id = Preferences.shipId
        if id != 0 {
            var url_string : String = HttpRequestType.ShipStatus.rawValue
            url_string = url_string.replacingOccurrences(of: "XXXXX", with: String(id))
            guard let req_url = URL(string : url_string) else {
                Calling = false
                return
            }
            
            serverSession.getJson(req_url, completion: completion)
        }
    }
}
