localforage.setDriver(localforage.INDEXEDDB);

function saveChanges() {
  // Get a value.
  localforage.setItem("youtube_logrpc", document.getElementById("youtube").checked ? "y" : "n");
  localforage.setItem("netflix_logrpc", document.getElementById("netflix").checked ? "y" : "n");
  localforage.setItem("wikipedia_logrpc", document.getElementById("wikipedia").checked ? "y" : "n");
  localforage.setItem("instagram_logrpc", document.getElementById("instagram").checked ? "y" : "n");
  localforage.setItem("twitch_logrpc", document.getElementById("twitch").checked ? "y" : "n");
  localforage.setItem("dailymotion_logrpc", document.getElementById("dailymotion").checked ? "y" : "n");
  localforage.setItem("twitter_logrpc", document.getElementById("twitter").checked ? "y" : "n");
  localforage.setItem("disneyplus_logrpc", document.getElementById("disneyplus").checked ? "y" : "n");
  localforage.setItem("hulu_logrpc", document.getElementById("hulu").checked ? "y" : "n");

  chrome.runtime.sendMessage({
    'youtube_logrpc': document.getElementById("youtube").checked ? "y" : "n",
    'netflix_logrpc': document.getElementById("netflix").checked ? "y" : "n",
    'wikipedia_logrpc': document.getElementById("wikipedia").checked ? "y" : "n",
    'instagram_logrpc': document.getElementById("instagram").checked ? "y" : "n",
    'twitch_logrpc': document.getElementById("twitch").checked ? "y" : "n",
	'dailymotion_logrpc': document.getElementById("dailymotion").checked ? "y" : "n",
    'twitter_logrpc': document.getElementById("twitter").checked ? "y" : "n",
	'disneyplus_logrpc': document.getElementById("disneyplus").checked ? "y" : "n",
	'hulu_logrpc': document.getElementById("hulu").checked ? "y" : "n",
  });
}

document.addEventListener("DOMContentLoaded", () => {
  localforage.getItem("youtube_logrpc").then(function (enabled) {
    if (enabled != null) {
      if (enabled == 'y') {
		  document.getElementById("youtube").checked = true;
	  } else {
		  document.getElementById("youtube").checked = false;
	  }
    } else {
      document.getElementById("youtube").checked = true;
	  localforage.setItem("youtube_logrpc", "y");
    }
  });
  localforage.getItem("netflix_logrpc").then(function (enabled) {
    if (enabled != null) {
		if (enabled == 'y') {
			document.getElementById("netflix").checked = true;
		} else {
			document.getElementById("netflix").checked = false;
		}
    } else {
      document.getElementById("netflix").checked = true;
	  localforage.setItem("netflix_logrpc", "y");
    }
  });
  localforage.getItem("wikipedia_logrpc").then(function (enabled) {
    if (enabled != null) {
		if (enabled == 'y') {
			document.getElementById("wikipedia").checked = true;
		} else {
			document.getElementById("wikipedia").checked = false;
		}
    } else {
      document.getElementById("wikipedia").checked = true;
	  localforage.setItem("wikipedia_logrpc", "y");
    }
  });
  localforage.getItem("instagram_logrpc").then(function (enabled) {
    if (enabled != null) {
		if (enabled == 'y') {
			document.getElementById("instagram").checked = true;
		} else {
			document.getElementById("instagram").checked = false;
		}
    } else {
      document.getElementById("instagram").checked = true;
	  localforage.setItem("instagram_logrpc", "y");
    }
  });
  localforage.getItem("twitch_logrpc").then(function (enabled) {
    if (enabled != null) {
		if (enabled == 'y') {
			document.getElementById("twitch").checked = true;
		} else {
			document.getElementById("twitch").checked = false;
		}
    } else {
      document.getElementById("twitch").checked = true;
	  localforage.setItem("twitch_logrpc", "y");
    }
  });
  localforage.getItem("dailymotion_logrpc").then(function (enabled) {
    if (enabled != null) {
		if (enabled == 'y') {
			document.getElementById("dailymotion").checked = true;
		} else {
			document.getElementById("dailymotion").checked = false;
		}
    } else {
      document.getElementById("dailymotion").checked = true;
	  localforage.setItem("dailymotion_logrpc", "y");
    }
  });
  localforage.getItem("twitter_logrpc").then(function (enabled) {
    if (enabled != null) {
		if (enabled == 'y') {
			document.getElementById("twitter").checked = true;
		} else {
			document.getElementById("twitter").checked = false;
		}
    } else {
      document.getElementById("twitter").checked = true;
	  localforage.setItem("twitter_logrpc", "y");
    }
  });
  localforage.getItem("disneyplus_logrpc").then(function (enabled) {
    if (enabled != null) {
		if (enabled == 'y') {
			document.getElementById("disneyplus").checked = true;
		} else {
			document.getElementById("disneyplus").checked = false;
		}
    } else {
      document.getElementById("disneyplus").checked = true;
	  localforage.setItem("disneyplus_logrpc", "y");
    }
  });
  localforage.getItem("hulu_logrpc").then(function (enabled) {
    if (enabled != null) {
		if (enabled == 'y') {
			document.getElementById("hulu").checked = true;
		} else {
			document.getElementById("hulu").checked = false;
		}
    } else {
      document.getElementById("hulu").checked = true;
	  localforage.setItem("hulu_logrpc", "y");
    }
  });

  document.getElementById("youtube").onclick = function () {
    saveChanges();
  };
  document.getElementById("netflix").onclick = function () {
    saveChanges();
  };
  document.getElementById("wikipedia").onclick = function () {
    saveChanges();
  };
  document.getElementById("instagram").onclick = function () {
    saveChanges();
  };
  document.getElementById("twitch").onclick = function () {
    saveChanges();
  };
  document.getElementById("dailymotion").onclick = function () {
    saveChanges();
  };
  document.getElementById("twitter").onclick = function () {
    saveChanges();
  };
  document.getElementById("disneyplus").onclick = function () {
    saveChanges();
  };
  document.getElementById("hulu").onclick = function () {
    saveChanges();
  };

});
