import browser from 'webextension-polyfill'
import { onError } from './seqta/utils/onError.js';
export const openDB = () => {
  return new Promise((resolve, reject) => {
    const request = indexedDB.open('MyDatabase', 1);

    request.onupgradeneeded = (event) => {
      const db = event.target.result;
      db.createObjectStore('backgrounds', { keyPath: 'id' });
    };

    request.onsuccess = () => {
      resolve(request.result);
    };

    request.onerror = (event) => {
      reject('Error opening database: ' + event.target.errorCode);
    };
  });
};

export const writeData = async (type, data) => {
  const db = await openDB();

  const tx = db.transaction('backgrounds', 'readwrite');
  const store = tx.objectStore('backgrounds');
  const request = await store.put({ id: 'customBackground', type, data });

  return request.result;
};

export const readData = () => {
  return new Promise((resolve, reject) => {
    openDB()
      .then(db => {
        const tx = db.transaction('backgrounds', 'readonly');
        const store = tx.objectStore('backgrounds');

        // Retrieve the custom background
        const getRequest = store.get('customBackground');

        // Attach success and error event handlers
        getRequest.onsuccess = function(event) {
          resolve(event.target.result);
        };

        getRequest.onerror = function(event) {
          console.error('An error occurred:', event);
          reject(event);
        };
      })
      .catch(error => {
        console.error('An error occurred:', error);
        reject(error);
      });
  });
};

function reloadSeqtaPages() {
  const result = browser.tabs.query({})
    function open (tabs) {
    for (let tab of tabs) {
      if (tab.title.includes('SEQTA Learn')) {
        browser.tabs.reload(tab.id);
      }
    }
  }
  result.then(open, onError)
}

// Helper function to handle setting permissions
const handleAddPermissions = () => {
  if (typeof browser.declarativeContent !== 'undefined') {
    browser.declarativeContent.onPageChanged.removeRules(undefined, () => {});
  }
  
  browser.permissions.request(
    { permissions: ['declarativeContent'], origins: ['*://*/*'] },
    (granted) => {
      if (granted) {
        const rules = [
          // Define your rules here
        ];
        
        rules.forEach(rule => {
          browser.declarativeContent.onPageChanged.addRules([rule]);
        });
        
        alert('Permissions granted. Reload SEQTA pages to see changes. If this workaround doesn\'t work, please contact the developer. It will be an easy fix');
      }
    }
  );
};

// Main message listener
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
  switch (request.type) {
  case 'reloadTabs':
    reloadSeqtaPages();
    break;
  
  case 'currentTab':
    browser.tabs.query({ active: true, currentWindow: true }).then(function (tabs) {
      browser.tabs.sendMessage(tabs[0].id, request).then(function (response) {
        sendResponse(response);
      });
    });
    return true;
  
  case 'githubTab':
    browser.tabs.create({ url: 'github.com/SethBurkart123/EvenBetterSEQTA' });
    break;
    
  case 'setDefaultStorage':
    SetStorageValue(DefaultValues);
    break;
    
  case 'addPermissions':
    handleAddPermissions();
    break;

  case 'sendNews':
    GetNews(sendResponse);
    return true;
      
  default:
    console.log('Unknown request type');
  }
});

function GetNews(sendResponse) {
  // Gets the current date
  const date = new Date();

  const from =
    date.getFullYear() +
    '-' +
    (date.getMonth() + 1) +
    '-' +
    (date.getDate() - 1);

  let url = `https://newsapi.org/v2/everything?domains=abc.net.au&from=${from}&apiKey=17c0da766ba347c89d094449504e3080`;

  fetch(url)
    .then((result) => result.json())
    .then((response) => {
      if (response.code == 'rateLimited') {
        url += '%00';
        GetNews();
      } else {
        sendResponse({ news: response });
      }
    });
}

const DefaultValues = {
  onoff: true,
  animatedbk: true,
  bksliderinput: 50,
  transparencyEffects: false,
  lessonalert: true,
  notificationcollector: true,
  defaultmenuorder: [],
  menuitems: {},
  menuorder: [],
  subjectfilters: {},
  selectedColor: 'linear-gradient(40deg, rgba(201,61,0,1) 0%, RGBA(170, 5, 58, 1) 100%)',
  DarkMode: true,
  shortcuts: [
    {
      name: 'YouTube',
      enabled: false,
    },
    {
      name: 'Outlook',
      enabled: true,
    },
    {
      name: 'Office',
      enabled: true,
    },
    {
      name: 'Spotify',
      enabled: false,
    },
    {
      name: 'Google',
      enabled: true,
    },
    {
      name: 'DuckDuckGo',
      enabled: false,
    },
    {
      name: 'Cool Math Games',
      enabled: false,
    },
    {
      name: 'SACE',
      enabled: false,
    },
    {
      name: 'Google Scholar',
      enabled: false,
    },
    {
      name: 'Gmail',
      enabled: false,
    },
    {
      name: 'Netflix',
      enabled: false,
    },
    {
      name: 'Education Perfect',
      enabled: false,
    },
  ],
  customshortcuts: [],
};

function SetStorageValue(object) {
  for (var i in object) {
    browser.storage.local.set({ [i]: object[i] });
  }
}

function UpdateCurrentValues() {
  const result = browser.storage.local.get()
  function open (items) {
    var CurrentValues = items;

    const NewValue = Object.assign({}, DefaultValues, CurrentValues);

    function CheckInnerElement(element) {
      for (let i in element) {
        if (typeof element[i] === 'object') {
          if (typeof DefaultValues[i].length == 'undefined') {
            NewValue[i] = Object.assign({}, DefaultValues[i], CurrentValues[i]);
          } else {
            // If the object is an array, turn it back after
            let length = DefaultValues[i].length;
            NewValue[i] = Object.assign({}, DefaultValues[i], CurrentValues[i]);
            let NewArray = [];
            for (let j = 0; j < length; j++) {
              NewArray.push(NewValue[i][j]);
            }
            NewValue[i] = NewArray;
          }
        }
      }
    }
    CheckInnerElement(DefaultValues);

    if (items['customshortcuts']) {
      NewValue['customshortcuts'] = items['customshortcuts'];
    }

    SetStorageValue(NewValue);
  }
  result.then(open, onError)
}

function migrateOldStorage() {
  const result = browser.storage.local.get()
  function open (items) {
    let shouldUpdate = false; // Flag to check if there is anything to update
    
    // Check for the old "Name" field and convert it to "name"
    if (items.shortcuts && items.shortcuts.length > 0 && 'Name' in items.shortcuts[0]) {
      shouldUpdate = true;
      items.shortcuts = items.shortcuts.map((shortcut) => {
        return {
          name: shortcut.Name,  // Convert "Name" to "name"
          enabled: shortcut.enabled // Keep the "enabled" field as is
        };
      });
    }

    // Check for "educationperfect" and convert it to "Education Perfect"
    if (items.shortcuts && items.shortcuts.length > 0) {
      for (let shortcut of items.shortcuts) {
        if (shortcut.name === 'educationperfect' || shortcut.name === 'Education Perfect') {
          shouldUpdate = true;
          shortcut.name = 'Education Perfect';
        }
      }
    }

    // If there"s something to update, set the new values in storage
    if (shouldUpdate) {
      const setting = browser.storage.local.set({ shortcuts: items.shortcuts })
      setting.then(() => console.log('Migration Completed.'))
    }
  }
  result.then(open, onError)
}

browser.runtime.onInstalled.addListener(function (event) {
  browser.storage.local.remove(['justupdated']);
  UpdateCurrentValues();
  if ( event.reason == 'install', event.reason == 'update' ) {
    browser.storage.local.set({ justupdated: true });
    migrateOldStorage();
  }
});