
const Model = require("./Model")
const Stream = require("./Stream")
const Action = require("./Action")
const Settings = require("./Settings")

class Device extends Model {

    defaultFields() {
        return {
            userId: {
                type: String,
                required: true
            },
            id: {
                type: String,
                required: true
            },
            name: { type: String, required: true },
            description: String,
            properties: Object,
            settings: {
                type: Settings,
                default: () => new Settings()
            },
            streams: {
                listOf: Stream,
                transform: (stream, name) => {

                    if(!stream.channels && typeof stream === "object") {

                        if (stream["type"] === "dynamic") {
                            stream.channels = {}
                        }
                        else {
                            stream = {
                                channels: stream
                            }
                        }
                    }

                    if(name) {
                        stream.name = name
                    }

                    return stream
                }
            },
            actions: {
                listOf: Action,
                transform: (action, name) => {

                    if(typeof action === "string") {
                        action = {
                            name: action
                        }
                    }

                    if(name) {
                        action.name = name
                    }

                    return action
                }
            }
        }
    }

    fromJSON(json) {

        this.arrayToObject("actions", "name", json)
        this.arrayToObject("streams", "name", json)

        super.fromJSON(json)
    }

    afterConstructor() {

        Object.keys(this.json.streams).forEach((name) => {
            this.json.streams[name].setDevice(this)
        })

        Object.keys(this.json.actions).forEach((name) => {
            this.json.actions[name].setDevice(this)
        })

    }

    getStream(name) {
        return this.json.streams[name]  || null
    }

    setStream(stream) {
        this.json.streams[stream.name] = new Stream(stream)
    }

    getAction(name) {
        return this.actions[name] || null
    }

    setAction(action) {
        this.actions[action.name] = new Action(action)
    }

}

module.exports = Device

@@ -1952,24 +1952,61 @@
 ructor() %7B%0A%0A
+%0A        if(this.json.streams) %7B%0A    
         Obje
@@ -2056,32 +2056,36 @@
 %3E %7B%0A            
+    
 this.json.stream
@@ -2112,28 +2112,78 @@
 is)%0A        
+ 
-%7D)%0A%0A
+   %7D)%0A        %7D%0A%0A        if(this.json.actions) %7B%0A    
         Obje
@@ -2233,32 +2233,36 @@
 %3E %7B%0A            
+    
 this.json.action
@@ -2293,18 +2293,33 @@
         
+  
-%7D)
+  %7D)%0A        %7D%0A
 %0A%0A    %7D%0A

handle null streams