
export default function(changeIntervalSettingName, pauseIntervalSettingName, dataVariableName) {
    
    return {
        data() {
            return {
                updateData: '',
                changeIntervalListenerIds: []
            }
        },
        mounted() {
            this.loadData()
            this.updateNeededRefreshTimers()
            
            this.$watch('settings.' + changeIntervalSettingName, function() {
                this.updateNeededRefreshTimers()
            })

            this.$watch('settings.' + pauseIntervalSettingName, function() {
                this.updateNeededRefreshTimers()
                this.loadData()
            })
        },
        methods: {
            // PUBLIC

            getDataToRefresh(callback) {
                callback('')
            },
            forceRefreshData() {
                this.refreshData()
            },

            // PRIVATE

            loadData() {
                let lastUpdate = localStorage.getItem(this.createLocalStorageId('updatedAt'))

                // If the data doesn't exist, we need to get it
                if (!lastUpdate) {
                    this.refreshData()
                }

                // If data updates are not paused, check if the data needs to be changed
                else if (!this.settings[pauseIntervalSettingName]) {
                    lastUpdate = dayjs.unix(lastUpdate)
                    
                    // Has the current day changed?
                    if (this.settings[changeIntervalSettingName] == 'daily' && !dayjs().isSame(lastUpdate, 'day')) {
                        this.refreshData()
                    }
                    // Has the current hour changed?
                    else if (this.settings[changeIntervalSettingName] == 'hourly' && !dayjs().isSame(lastUpdate, 'hour')) {
                        this.refreshData()
                    }
                } 
                
                this.displayData()
            },
            displayData() {
                let data = localStorage.getItem(this.createLocalStorageId('updateData')) || '""'

                try {
                    data = JSON.parse(data)
                } catch (error) {}

                this[dataVariableName] = data
            },
            refreshData() {
                this.getDataToRefresh((newData) => {
                    localStorage.setItem(this.createLocalStorageId('updateData'), JSON.stringify(newData))
                    localStorage.setItem(this.createLocalStorageId('updatedAt'), dayjs().unix() - 1)
                    this.displayData()
                })
            },
            createLocalStorageId(name) {
                return this.widgetType + '-' + name
            },
            updateNeededRefreshTimers() {
                // Clear current change timers
                for (let id of this.changeIntervalListenerIds) {
                    Hologram.removeListener(this, id)
                }

                this.changeIntervalListenerIds = []

                // Should we pause updates?
                if (this.settings[pauseIntervalSettingName]) {
                    return
                }

                let intervalSetting = this.settings[changeIntervalSettingName]

                // Change data once a day
                if (intervalSetting == 'daily') {
                    this.changeIntervalListenerIds.push(Hologram.timer.repeatOnceADayOn(this, '0:0', () => {
                        this.loadData()
                    }))
                } 
                
                // Change data hourly
                else if (intervalSetting == 'hourly') {
                    this.changeIntervalListenerIds.push(Hologram.timer.everyHour(this, () => {
                        this.loadData()
                    }))
                }
            }
        }
    }
}