<div class="electro-mode-switcher">
  <div class="changecolor light" data-color="light"><span>{{ 'general.switcher-mode.light' | t }}</span></div>
  <div class="changecolor dark" data-color="dark"><span>{{ 'general.switcher-mode.dark' | t }}</span></div>
</div>

<script type="text/javascript">
  function setCookie(cname,cvalue,exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
  }

  function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ') {
        c = c.substring(1);
      }
      if (c.indexOf(name) == 0) {
        return c.substring(name.length, c.length);
      }
    }
    return "";
  }

  function checkCookieSwitcher() {
    var userd=getCookie("userdark");

    if (userd != "") {  
      document.querySelector("body").setAttribute('id', userd);

      if (userd = "dark"){ 
        document.getElementsByTagName("head")[0].insertAdjacentHTML(
         "beforeend",
         "<link rel=\"stylesheet\" href=\"{{ 'bg-dark.aio.min.css' | asset_url }}\" />");
      }

      let changecolor = document.querySelectorAll('.changecolor');

      changecolor.forEach(function (e) {
        e.addEventListener("click", function () {
          let _color = this.getAttribute('data-color');

          document.querySelector("body").setAttribute('id', _color);
          userd = document.querySelector("body").getAttribute('id');

          if (userd != "" && userd != null) {
            setCookie("userdark", userd, 30);
          }

        });
      });
      
    } else {
      
      let changecolor = document.querySelectorAll('.changecolor');

      changecolor.forEach(function (e) {
        e.addEventListener("click", function () {
          let _color = this.getAttribute('data-color');

          document.querySelector("body").setAttribute('id', _color);
          userd = document.querySelector("body").getAttribute('id');

          if(_color == 'dark'){
            document.getElementsByTagName("head")[0].insertAdjacentHTML(
              "beforeend",
              "<link rel=\"stylesheet\" href=\"{{ 'bg-dark.css' | asset_url }}\" />");
          }

          if (userd != "" && userd != null) {
            setCookie("userdark", userd, 30);
          }

        });
      });
    }
  }
  
  checkCookieSwitcher();
    
</script>