//
//  StartResponse.swift 
//  EvidationCommonUI 
//  Copyright © 2021 Evidation Health Inc. All rights reserved.
//  

import Foundation

/// The StartResponse keeps track of an offer's start response to post
/// This must be posted before submitting any SectionResponses for the offer
class StartResponse: Submittable, CustomStringConvertible {
    let timestamp: Date
    let timezone: TimeZone
    
    // MARK: - Submittable
    
    var endpoint: Endpoint<SubmitResponse>?
    var parameters: JSONObject?
    
    public var description: String {
        return "\(type(of: self))(timestamp: \(timestamp), timezone: \(timezone), endpoint: \(String(describing: endpoint)), parameters: \(String(describing: parameters)))"
    }
    
    // MARK: - Init
    
    init(startUrl: String?) {
        let date = Date()
        let timezone = Calendar(identifier: .gregorian).timeZone
        self.timestamp = date
        self.timezone = timezone
        
        if let startUrl = startUrl {
            self.endpoint = Endpoint<SubmitResponse>(url: startUrl, method: .POST)
        } else {
            self.endpoint = nil
        }
        
        self.parameters = ["viewed_at": date.timeIntervalSince1970.json,
                           "timezone": timezone.identifier.json]
    }
}
