var apiPath= location.pathname;//console.log(location);

window.onload = function()
{
      
      var app = new Vue(
        {
          el: '#app',
          data:
          { 
            showMessages:false,
            messages:[],
            isAdmin:false,
            tabindex:1,
            clients:[]
          },
          components: 
          {
            'header-component': httpVueLoader('js/header-component.vue'),
            'live-datagrid': httpVueLoader('js/live-datagrid.vue'),
            'users-datagrid': httpVueLoader('js/users-datagrid.vue'),          
            'distribution-administration': httpVueLoader('js/distribution-administration.vue'),
            'modal': httpVueLoader('js/modal.vue')
            
          },
          watch:
          {
            isAdmin:function(newv,oldv)
            {
              vm=this;
              interval=null;
              if(newv)
              {
                  interval=setInterval(function(){
                    number= parseInt(window.location.hash.substring(1,2));
                    vm.tabindex=number;
                    clearInterval(interval);
                  },1);            
              }
              else
              {
                interval=setInterval(function(){
                    vm.tabindex=0;
                    clearInterval(interval);
                  },1);    
              }
            }
          },
          created:function()
          {
            this.getClients();
            setInterval(this.getClients,5000);
          }, 
          mounted:function()
          {
            //console.log("mounted");
          },
          methods:
          {
            onIsAdminChange:function(newAdminStatus)
            {
              this.isAdmin=newAdminStatus;        
            },
            onCLientsChanged:function(foo)
            {
              this.getClients();
             // console.log("clients changed");
            },
            addMessages:function(msgs)
            {
              
              if(msgs!=null && msgs.constructor === Array)
              {
                
                this.messages=msgs;
                var errs = this.messages.filter(obj => {
                        return obj.type === "err"
                          }).length>0;
                
                this.showMessages=this.messages.length>0;
                
                if(this.showMessages)
                {
                  var vm=this;
                  clearInterval(vm.msgintervall);  
                  if(!errs)
                    vm.msgintervall=setInterval(function(){
                                  vm.showMessages=false;
                                  clearInterval(vm.msgintervall);                    
                                          },3000);
                }
              }
            },
            setTabIndex:function(index,event)
            {           
              vm=this;
              interval=null;

              if(event)
              {
                vm.tabindex=index;
                window.location.hash=vm.tabindex;
              }
            },
            getClients:function()
            {
                var vm = this
                axios.get(apiPath+"watcher/clients")
                    .then(function (response) {
                    if(response.data.data !==null)
                    { 
                        vm.updateClients(response.data.data);
                    }
                    })
                    .catch(function (error) {
                        console.log(error);
                    })
            },
            updateClients:function(newclients)
            {
                if(this.clients!=null && this.clients.constructor === Array)
                {
                // APLIKACE STAVU ROZKLIKNUTEHO DETAILU
                    this.clients.forEach(function(value, index) 
                                {
                                    if(value._showDetails)
                                        {
                                            found=newclients.find(function(element) {
                                                    if(element.ID==value.ID){
                                                        return element;}
                                                    else{
                                                        return null;}
                                                });
                                        if(found !=null)
                                          {
                                            //console.log("resetting client gui state after data refresh");console.log(found);console.log(value);
                                            found._showDetails=true;
                                            found.edit=value.edit;
                                            found.showDetails=value.showDetails;
                                          }
                                        }
                                }
                    ); 
                    this.clients=newclients;
                    this.$emit("clientsload",newclients);
                }
            }
              
          }
        });
}

function getReadableFileSizeString(fileSizeInBytes) {
    var i = -1;
    var byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'];
    do {
        fileSizeInBytes = fileSizeInBytes / 1024;
        i++;
    } while (fileSizeInBytes > 1024);

    return Math.max(fileSizeInBytes, 0.0).toFixed(1) + byteUnits[i];
  };
function getReadableDownloadSpeed(speed)
{
  return getReadableFileSizeString(speed)+"/s";
}

function getBase64(file) 
{
    // return atob(file);

    return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => resolve(reader.result);
      reader.onerror = error => reject(error);
    });
}
      
        