// localStorage.removeItem("userDetail");
// // indexedDB.deleteDatabase("wstracker_new_db");
// //window.location.href = "../index.html";
// // var request = indexedDB.deleteDatabase("wstracker_new_db");
// // request.onsuccess = function (e) {
// //   console.log("success");
// // };
// // request.onblocked = function (e) {
// //   console.log("blocked: " + e);
// //   // Close connections here
// // };
// // request.onerror = function (e) {
// //   console.log("error: " + e);
// // };

// function fireBlockedEvent(db) {
//   var req = indexedDB.open(db);

//   req.onblocked = function () {
//     // close the blocking connection:
//     db.close();
//   };
//   req.onsuccess = function (e) {
//     // close the formerly blocked connection:
//     e.target.result.close();

//     var req = indexedDB.deleteDatabase(db);
//     req.onsuccess = function () {
//      // window.location.href = "../index.html";

//     };
//     req.onblocked = function () {
//     };
//   };
// }
// fireBlockedEvent("wstracker_new_db");
function jsonBG(url) {
  return fetch(url).then((res) => res.json());
}

let localTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (localTimezone === "Asia/Calcutta") {
  localTimezone = "Asia/Kolkata";
}

jsonBG(endpointUrl + `getCurrentTime?timezone=${localTimezone}`).then(
  (timerdata) => {
    if (timerdata.response.code == 200) {
      ////////////////////// sync data to server /////////////
      let idb = indexedDB.open("wstracker_new_db");
      idb.onsuccess = () => {
        let res = idb.result;
        let tx;
        try {
          tx = res.transaction("timer_data", "readwrite");
        } catch (e) {
          res.close();
          chrome.storage.local.clear(function () {
            var error = chrome.runtime.lastError;
            if (error) {
              console.error(error);
            }
            // do something more
          });
          var request = indexedDB.deleteDatabase("wstracker_new_db");
          request.onsuccess = function (e) {
            localStorage.removeItem("userDetail");
            chrome.storage.local.set({ userDetailChrome: "" }).then(() => {
            });
            window.location.href = "../index.html";
          };
          request.onblocked = function (e) {
            // Close connections here
            // window.location.href = "error.html";
          };
          request.onerror = function (e) {
          };
        }
        let store = tx.objectStore("timer_data");
        let countRequest = store.count();
        countRequest.onsuccess = () => {
          let remaningdatacount = countRequest.result;
          for (let i = 0; i < countRequest.result; i++) {
            sendDatatoServer().then((res) => {
              // SyncedData(moment().format("YYYY-MM-DD"));
              ///////////////////////////////////////
            });
            remaningdatacount--;
          }

          if (remaningdatacount == 0) {
            chrome.storage.local.clear(function () {
              var error = chrome.runtime.lastError;
              if (error) {
                console.error(error);
              }
              // do something more
            });
            var request = indexedDB.deleteDatabase("wstracker_new_db");
            request.onsuccess = function (e) {
              localStorage.removeItem("userDetail");
              chrome.storage.local.set({ userDetailChrome: "" }).then(() => {
              });
              window.location.href = "../index.html";
            };
            request.onblocked = function (e) {
              // Close connections here
              // window.location.href = "error.html";
            };
            request.onerror = function (e) {
            };
          }
        };
      };
      ////////////////////////////////////////////////

      //////////////////////////////////////////////////////
    } else {
      // window.location.href = "error.html";
    }
  }
);
