import getWeatherInfo from './libs/get-weather-info';
import fetchRandomPhoto from './libs/fetch-random-photo';
import loadNewData from './libs/load-new-data';
import { notifyCloudAuthenticationSuccessful } from './libs/notifications';
import { onedriveAuth, refreshOnedriveToken } from './libs/onedrive-auth';

chrome.runtime.onInstalled.addListener(details => {
  if (details.reason === 'install') {
    chrome.storage.local.set({
      cloudService: null,
      forecast: null,
      nextImage: null,
      history: null,
      coords: {
        latitude: '',
        longitude: '',
      },
    });

    chrome.storage.sync.set({
      imageSource: 'official',
      photoFrequency: 'newtab',
      temperatureFormat: 'metric',
    });
  }

  fetchRandomPhoto();
});

chrome.runtime.onMessage.addListener((request, sender) => {
  switch (request.command) {
    case 'close-tab': {
      chrome.tabs.remove(sender.tab.id);
      break;
    }

    case 'set-dropbox-token': {
      chrome.storage.local.set({ dropbox: request.token });

      notifyCloudAuthenticationSuccessful('Dropbox');

      chrome.runtime.sendMessage({ command: 'update-cloud-status' });
      break;
    }

    case 'code-flow': {
      chrome.storage.local.get('cloudService', result => {
        const { cloudService } = result;
        if (cloudService === 'onedrive') {
          chrome.tabs.remove(sender.tab.id);
          onedriveAuth(request.code);
        }
      });
      break;
    }

    case 'update-weather': {
      getWeatherInfo();
      break;
    }

    case 'set-onedrive-alarm': {
      chrome.alarms.create('refresh-onedrive-token', {
        periodInMinutes: request.expires_in / 60,
      });
      break;
    }

    case 'set-googledrive-alarm': {
      chrome.alarms.create('refresh-googledrive-token', {
        periodInMinutes: request.expires_in / 60,
      });
      break;
    }

    case 'load-data': {
      loadNewData();
      break;
    }

    default: {
      loadNewData();
    }
  }
});

chrome.alarms.onAlarm.addListener(alarm => {
  if (alarm.name === 'loadphoto') {
    fetchRandomPhoto();
    return;
  }

  if (alarm.name === 'loadweather') {
    chrome.runtime.sendMessage({ command: 'update-weather' });
    return;
  }

  if (alarm.name === 'refresh-onedrive-token') {
    refreshOnedriveToken();
  }
});

@@ -310,99 +310,51 @@
 ;%0A%0Ac
-hrome.runtime.onInstalled.addListener(details =%3E %7B%0A  if (details.reason === 'install') %7B%0A  
+onst setDefaultExtensionSettings = () =%3E %7B%0A
   ch
@@ -382,18 +382,16 @@
 t(%7B%0A    
-  
 cloudSer
@@ -398,26 +398,24 @@
 vice: null,%0A
-  
     forecast
@@ -426,18 +426,16 @@
 ll,%0A    
-  
 nextImag
@@ -439,26 +439,24 @@
 mage: null,%0A
-  
     history:
@@ -466,18 +466,16 @@
 ll,%0A    
-  
 coords: 
@@ -472,26 +472,24 @@
   coords: %7B%0A
-  
       latitu
@@ -502,18 +502,16 @@
 ,%0A      
-  
 longitud
@@ -525,26 +525,20 @@
     
-  
 %7D,%0A  
- 
- 
 %7D);%0A%0A  
- 
- 
 chro
@@ -563,18 +563,16 @@
 t(%7B%0A    
-  
 imageSou
@@ -592,18 +592,16 @@
 l',%0A    
- 
- 
 photoFre
@@ -618,18 +618,16 @@
 ewtab',%0A
-  
     temp
@@ -653,19 +653,146 @@
 ric',%0A  
-  %7D
+%7D);%0A%7D;%0A%0Achrome.runtime.onInstalled.addListener(details =%3E %7B%0A  if (details.reason === 'install') %7B%0A    setDefaultExtensionSettings(
 );%0A  %7D%0A%0A

Move default extension settings to separate function