//
//  HFTBonusProgressElement.swift 
//  Pi 
//  Copyright © 2021 Evidation Health Inc. All rights reserved.
//  

import EvidationCommonUI

public class BonusProgressElement: Element {
    public let family = "presentation"
    public let type = "bonus"
    public let id: String?
    public let layout: Layout
    public let style: ServerStyle?
    public let dynamicViewFactory: DynamicViewFactory
    public let requirements: ElementRequirements
    
    /// Each Int in the array represents the index of a completed session (0 indexing)
    let completedSessions: [Int]
    let activeSession: Int?
    let nextSession: Int?
    
    private init(id: String?, layout: Layout, style: ServerStyle?,
                 dynamicViewFactory: DynamicViewFactory,
                 requirements: ElementRequirements,
                 completedSessions: [Int], activeSession: Int?, nextSession: Int?) {
        self.id = id
        self.layout = layout
        self.style = style
        self.dynamicViewFactory = dynamicViewFactory
        self.requirements = requirements
        self.completedSessions = completedSessions
        self.activeSession = activeSession
        self.nextSession = nextSession
    }
    
    public static func createElement(json: JSONObject, dynamicViewFactory: DynamicViewFactory) -> Element? {
        let id = json["id"]?.string ?? json["identifier"]?.string
        let layout = Layout(json: json["layout"]?.dictionary)
        let style = json["style"]?.dictionary
        let requirements = ElementRequirements.requirements(from: json)
        
        let completedSessions = json["completed_sessions"]?.arrayOfInts ?? []
        let activeSession = json["active_session"]?.int
        let nextSession = json["next_session"]?.int
        
        return BonusProgressElement(id: id, layout: layout, style: style,
                             dynamicViewFactory: dynamicViewFactory,
                             requirements: requirements, completedSessions: completedSessions, activeSession: activeSession, nextSession: nextSession)
    }
    
}

